|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Josef Gajdusek |
| 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 "timeutils.h" |
| 37 | +#include "modmachine.h" |
| 38 | + |
| 39 | +typedef struct _machine_rtc_obj_t { |
| 40 | + mp_obj_base_t base; |
| 41 | +} machine_rtc_obj_t; |
| 42 | + |
| 43 | + |
| 44 | +// singleton RTC object |
| 45 | +STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; |
| 46 | + |
| 47 | +// Sleep time |
| 48 | +uint64_t machine_rtc_expiry = 0; // in microseconds |
| 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 | + |
| 54 | + // return constant object |
| 55 | + return (mp_obj_t)&machine_rtc_obj; |
| 56 | +} |
| 57 | + |
| 58 | +STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { |
| 59 | + if (n_args == 1) { |
| 60 | + // Get time |
| 61 | + |
| 62 | + struct timeval tv; |
| 63 | + struct tm tm; |
| 64 | + |
| 65 | + gettimeofday(&tv, NULL); |
| 66 | + |
| 67 | + gmtime_r(&tv.tv_sec, &tm); |
| 68 | + |
| 69 | + mp_obj_t tuple[8] = { |
| 70 | + mp_obj_new_int(tm.tm_year), |
| 71 | + mp_obj_new_int(tm.tm_mon), |
| 72 | + mp_obj_new_int(tm.tm_mday), |
| 73 | + mp_obj_new_int(tm.tm_wday), |
| 74 | + mp_obj_new_int(tm.tm_hour), |
| 75 | + mp_obj_new_int(tm.tm_min), |
| 76 | + mp_obj_new_int(tm.tm_sec), |
| 77 | + mp_obj_new_int(tv.tv_usec / 1000) // microseconds --> milliseconds |
| 78 | + }; |
| 79 | + |
| 80 | + return mp_obj_new_tuple(8, tuple); |
| 81 | + } else { |
| 82 | + // Set time |
| 83 | + |
| 84 | + mp_obj_t *items; |
| 85 | + mp_obj_get_array_fixed_n(args[1], 8, &items); |
| 86 | + struct tm tm; |
| 87 | + struct timeval tv; |
| 88 | + |
| 89 | + tm.tm_year = mp_obj_get_int(items[0]); |
| 90 | + tm.tm_mon = mp_obj_get_int(items[1]); |
| 91 | + tm.tm_mday = mp_obj_get_int(items[2]); |
| 92 | + tm.tm_hour = mp_obj_get_int(items[4]); |
| 93 | + tm.tm_min = mp_obj_get_int(items[5]); |
| 94 | + tm.tm_sec = mp_obj_get_int(items[6]); |
| 95 | + |
| 96 | + tv.tv_sec = mktime(&tm); |
| 97 | + tv.tv_usec = mp_obj_get_int(items[7]) * 1000; |
| 98 | + |
| 99 | + settimeofday(&tv, NULL); |
| 100 | + |
| 101 | + return mp_const_none; |
| 102 | + } |
| 103 | +} |
| 104 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime); |
| 105 | + |
| 106 | +STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) { |
| 107 | + |
| 108 | + if (n_args == 1) { |
| 109 | + // read RTC memory |
| 110 | + |
| 111 | + // FIXME; need to update IDF |
| 112 | + |
| 113 | + return mp_const_none; |
| 114 | + } else { |
| 115 | + // write RTC memory |
| 116 | + |
| 117 | + // FIXME; need to update IDF |
| 118 | + |
| 119 | + return mp_const_none; |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_memory_obj, 1, 2, machine_rtc_memory); |
| 124 | + |
| 125 | +STATIC mp_obj_t machine_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time_in) { |
| 126 | + (void)self_in; // unused |
| 127 | + |
| 128 | + // check we want alarm0 |
| 129 | + if (mp_obj_get_int(alarm_id) != 0) { |
| 130 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm")); |
| 131 | + } |
| 132 | + |
| 133 | + // set expiry time (in microseconds) |
| 134 | + machine_rtc_expiry = (uint64_t)mp_obj_get_int(time_in) * 1000; |
| 135 | + printf("Setting machine_rtc_expiry to %lld\n", machine_rtc_expiry); |
| 136 | + |
| 137 | + return mp_const_none; |
| 138 | + |
| 139 | +} |
| 140 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(machine_rtc_alarm_obj, machine_rtc_alarm); |
| 141 | + |
| 142 | +STATIC mp_obj_t machine_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 143 | + // This function essentially does nothing, for backwards compatibility |
| 144 | + |
| 145 | + enum { ARG_trigger, ARG_wake }; |
| 146 | + static const mp_arg_t allowed_args[] = { |
| 147 | + { MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 148 | + { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 149 | + }; |
| 150 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 151 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 152 | + |
| 153 | + // check we want alarm0 |
| 154 | + if (args[ARG_trigger].u_int != 0) { |
| 155 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm")); |
| 156 | + } |
| 157 | + |
| 158 | + return mp_const_none; |
| 159 | +} |
| 160 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_rtc_irq_obj, 1, machine_rtc_irq); |
| 161 | + |
| 162 | +STATIC const mp_map_elem_t machine_rtc_locals_dict_table[] = { |
| 163 | + { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&machine_rtc_datetime_obj }, |
| 164 | + { MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&machine_rtc_memory_obj }, |
| 165 | + { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&machine_rtc_alarm_obj }, |
| 166 | + { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&machine_rtc_irq_obj}, |
| 167 | + { MP_OBJ_NEW_QSTR(MP_QSTR_ALARM0), MP_OBJ_NEW_SMALL_INT(0) }, |
| 168 | +}; |
| 169 | +STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); |
| 170 | + |
| 171 | +const mp_obj_type_t machine_rtc_type = { |
| 172 | + { &mp_type_type }, |
| 173 | + .name = MP_QSTR_RTC, |
| 174 | + .make_new = machine_rtc_make_new, |
| 175 | + .locals_dict = (mp_obj_t)&machine_rtc_locals_dict, |
| 176 | +}; |
0 commit comments