|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Noralf Trønnes |
| 7 | + * Copyright (c) 2013, 2014 Damien P. George |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "py/obj.h" |
| 31 | +#include "py/objproperty.h" |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "lib/timeutils/timeutils.h" |
| 34 | +#include "shared-bindings/rtc/__init__.h" |
| 35 | +#include "shared-bindings/rtc/RTC.h" |
| 36 | +#include "shared-bindings/time/__init__.h" |
| 37 | + |
| 38 | +void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) { |
| 39 | + mp_raise_NotImplementedError("RTC is not supported on this board"); |
| 40 | +} |
| 41 | + |
| 42 | +void MP_WEAK common_hal_rtc_set_time(timeutils_struct_time_t *tm) { |
| 43 | + mp_raise_NotImplementedError("RTC is not supported on this board"); |
| 44 | +} |
| 45 | + |
| 46 | +int MP_WEAK common_hal_rtc_get_calibration(void) { |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +void MP_WEAK common_hal_rtc_set_calibration(int calibration) { |
| 51 | + mp_raise_NotImplementedError("RTC calibration is not supported on this board"); |
| 52 | +} |
| 53 | + |
| 54 | +const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}}; |
| 55 | + |
| 56 | +//| .. currentmodule:: rtc |
| 57 | +//| |
| 58 | +//| :class:`RTC` --- Real Time Clock |
| 59 | +//| -------------------------------- |
| 60 | +//| |
| 61 | +//| .. class:: RTC() |
| 62 | +//| |
| 63 | +//| This class represents the onboard Real Time Clock. It is a singleton and will always return the same instance. |
| 64 | +//| |
| 65 | +STATIC mp_obj_t rtc_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 66 | + // No arguments |
| 67 | + mp_arg_check_num(n_args, n_kw, 0, 0, false); |
| 68 | + |
| 69 | + // return constant object |
| 70 | + return (mp_obj_t)&rtc_rtc_obj; |
| 71 | +} |
| 72 | + |
| 73 | +//| .. attribute:: datetime |
| 74 | +//| |
| 75 | +//| Get or set the date and time of the RTC. |
| 76 | +//| |
| 77 | +STATIC mp_obj_t rtc_rtc_obj_get_datetime(mp_obj_t self_in) { |
| 78 | + timeutils_struct_time_t tm; |
| 79 | + common_hal_rtc_get_time(&tm); |
| 80 | + return struct_time_from_tm(&tm); |
| 81 | +} |
| 82 | +MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_datetime_obj, rtc_rtc_obj_get_datetime); |
| 83 | + |
| 84 | +STATIC mp_obj_t rtc_rtc_obj_set_datetime(mp_obj_t self_in, mp_obj_t datetime) { |
| 85 | + timeutils_struct_time_t tm; |
| 86 | + struct_time_to_tm(datetime, &tm); |
| 87 | + common_hal_rtc_set_time(&tm); |
| 88 | + return mp_const_none; |
| 89 | +} |
| 90 | +MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_datetime_obj, rtc_rtc_obj_set_datetime); |
| 91 | + |
| 92 | +const mp_obj_property_t rtc_rtc_datetime_obj = { |
| 93 | + .base.type = &mp_type_property, |
| 94 | + .proxy = {(mp_obj_t)&rtc_rtc_get_datetime_obj, |
| 95 | + (mp_obj_t)&rtc_rtc_set_datetime_obj, |
| 96 | + (mp_obj_t)&mp_const_none_obj}, |
| 97 | +}; |
| 98 | + |
| 99 | +//| .. attribute:: calibration |
| 100 | +//| |
| 101 | +//| The RTC calibration value. |
| 102 | +//| A positive value speeds up the clock and a negative value slows it down. |
| 103 | +//| Range and value is hardware specific, but one step is often approx. 1 ppm. |
| 104 | +//| |
| 105 | +STATIC mp_obj_t rtc_rtc_obj_get_calibration(mp_obj_t self_in) { |
| 106 | + int calibration = common_hal_rtc_get_calibration(); |
| 107 | + return mp_obj_new_int(calibration); |
| 108 | +} |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_calibration_obj, rtc_rtc_obj_get_calibration); |
| 110 | + |
| 111 | +STATIC mp_obj_t rtc_rtc_obj_set_calibration(mp_obj_t self_in, mp_obj_t calibration) { |
| 112 | + common_hal_rtc_set_calibration(mp_obj_get_int(calibration)); |
| 113 | + return mp_const_none; |
| 114 | +} |
| 115 | +MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_calibration_obj, rtc_rtc_obj_set_calibration); |
| 116 | + |
| 117 | +const mp_obj_property_t rtc_rtc_calibration_obj = { |
| 118 | + .base.type = &mp_type_property, |
| 119 | + .proxy = {(mp_obj_t)&rtc_rtc_get_calibration_obj, |
| 120 | + (mp_obj_t)&rtc_rtc_set_calibration_obj, |
| 121 | + (mp_obj_t)&mp_const_none_obj}, |
| 122 | +}; |
| 123 | + |
| 124 | +STATIC const mp_rom_map_elem_t rtc_rtc_locals_dict_table[] = { |
| 125 | + { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&rtc_rtc_datetime_obj) }, |
| 126 | + { MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&rtc_rtc_calibration_obj) }, |
| 127 | +}; |
| 128 | +STATIC MP_DEFINE_CONST_DICT(rtc_rtc_locals_dict, rtc_rtc_locals_dict_table); |
| 129 | + |
| 130 | +const mp_obj_type_t rtc_rtc_type = { |
| 131 | + { &mp_type_type }, |
| 132 | + .name = MP_QSTR_RTC, |
| 133 | + .make_new = rtc_rtc_make_new, |
| 134 | + .locals_dict = (mp_obj_dict_t*)&rtc_rtc_locals_dict, |
| 135 | +}; |
0 commit comments