|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 "Krzysztof Adamski" <k@japko.eu> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include <time.h> |
| 31 | +#include <sys/time.h> |
| 32 | + |
| 33 | +#include "py/nlr.h" |
| 34 | +#include "py/obj.h" |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "py/mphal.h" |
| 37 | +#include "py/mperrno.h" |
| 38 | +#include "lib/timeutils/timeutils.h" |
| 39 | +#include "hardware/rtc.h" |
| 40 | +#include "pico/util/datetime.h" |
| 41 | +#include "modmachine.h" |
| 42 | + |
| 43 | +typedef struct _machine_rtc_obj_t { |
| 44 | + mp_obj_base_t base; |
| 45 | +} machine_rtc_obj_t; |
| 46 | + |
| 47 | +// singleton RTC object |
| 48 | +STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; |
| 49 | + |
| 50 | +STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 51 | + // check arguments |
| 52 | + mp_arg_check_num(n_args, n_kw, 0, 0, false); |
| 53 | + bool r = rtc_running(); |
| 54 | + |
| 55 | + if (!r) { |
| 56 | + // This shouldn't happen as rtc_init() is already called in main so |
| 57 | + // it's here just in case |
| 58 | + rtc_init(); |
| 59 | + datetime_t t = { .month = 1, .day = 1 }; |
| 60 | + rtc_set_datetime(&t); |
| 61 | + } |
| 62 | + // return constant object |
| 63 | + return (mp_obj_t)&machine_rtc_obj; |
| 64 | +} |
| 65 | + |
| 66 | +STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { |
| 67 | + if (n_args == 1) { |
| 68 | + bool ret; |
| 69 | + datetime_t t; |
| 70 | + |
| 71 | + ret = rtc_get_datetime(&t); |
| 72 | + if (!ret) { |
| 73 | + mp_raise_OSError(MP_EIO); |
| 74 | + } |
| 75 | + |
| 76 | + mp_obj_t tuple[8] = { |
| 77 | + mp_obj_new_int(t.year), |
| 78 | + mp_obj_new_int(t.month), |
| 79 | + mp_obj_new_int(t.day), |
| 80 | + mp_obj_new_int(t.dotw), |
| 81 | + mp_obj_new_int(t.hour), |
| 82 | + mp_obj_new_int(t.min), |
| 83 | + mp_obj_new_int(t.sec), |
| 84 | + mp_obj_new_int(0) |
| 85 | + }; |
| 86 | + |
| 87 | + return mp_obj_new_tuple(8, tuple); |
| 88 | + } else { |
| 89 | + mp_obj_t *items; |
| 90 | + |
| 91 | + mp_obj_get_array_fixed_n(args[1], 8, &items); |
| 92 | + |
| 93 | + datetime_t t = { |
| 94 | + .year = mp_obj_get_int(items[0]), |
| 95 | + .month = mp_obj_get_int(items[1]), |
| 96 | + .day = mp_obj_get_int(items[2]), |
| 97 | + .dotw = mp_obj_get_int(items[3]), |
| 98 | + .hour = mp_obj_get_int(items[4]), |
| 99 | + .min = mp_obj_get_int(items[5]), |
| 100 | + .sec = mp_obj_get_int(items[6]), |
| 101 | + }; |
| 102 | + |
| 103 | + rtc_set_datetime(&t); |
| 104 | + |
| 105 | + } |
| 106 | + return mp_const_none; |
| 107 | +} |
| 108 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime); |
| 109 | + |
| 110 | +STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { |
| 111 | + { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) }, |
| 112 | +}; |
| 113 | +STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); |
| 114 | + |
| 115 | +const mp_obj_type_t machine_rtc_type = { |
| 116 | + { &mp_type_type }, |
| 117 | + .name = MP_QSTR_RTC, |
| 118 | + .make_new = machine_rtc_make_new, |
| 119 | + .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, |
| 120 | +}; |
0 commit comments