|
1 | 1 | import gc |
2 | 2 |
|
| 3 | +import machine |
| 4 | +import micropython |
3 | 5 | import utime as time |
4 | 6 |
|
| 7 | +micropython.alloc_emergency_exception_buf(128) |
| 8 | + |
5 | 9 | for no in range(3, 0, -1): |
6 | 10 | print('%i main.py wait...' % no) |
7 | 11 | time.sleep(1) |
8 | 12 |
|
9 | | -import micropython |
10 | | - |
11 | | -micropython.alloc_emergency_exception_buf(128) |
| 13 | +from watchdog import watchdog # noqa isort:skip |
| 14 | +from leds import power_led # noqa isort:skip |
| 15 | +from ntp import ntp_sync # noqa isort:skip |
| 16 | +from wifi import wifi # noqa isort:skip |
12 | 17 |
|
13 | | -from leds import power_led |
14 | | -from ntp import ntp_sync |
15 | | -from watchdog import watchdog |
16 | | -from webswitch import main |
17 | | -from wifi import wifi |
18 | 18 |
|
| 19 | +print('watchdog:', watchdog) |
19 | 20 | print('wifi:', wifi) |
20 | 21 | print('ntp_sync:', ntp_sync) |
21 | 22 | print('power_led:', power_led) |
22 | | -print('watchdog:', watchdog) |
| 23 | + |
| 24 | + |
| 25 | +def get_debounced_value(pin): |
| 26 | + """get debounced value from pin by waiting for 20 msec for stable value""" |
| 27 | + cur_value = pin.value() |
| 28 | + stable = 0 |
| 29 | + while stable < 20: |
| 30 | + if pin.value() == cur_value: |
| 31 | + stable = stable + 1 |
| 32 | + else: |
| 33 | + stable = 0 |
| 34 | + cur_value = pin.value() |
| 35 | + time.sleep_ms(1) |
| 36 | + return cur_value |
| 37 | + |
| 38 | + |
| 39 | +def button_pressed(pin): |
| 40 | + print('button pressed...') |
| 41 | + cur_button_value = get_debounced_value(pin) |
| 42 | + if cur_button_value == 1: |
| 43 | + if relay_pin.value() == 1: |
| 44 | + print('turn off by button.') |
| 45 | + relay_pin.value(0) |
| 46 | + else: |
| 47 | + print('turn on by button.') |
| 48 | + relay_pin.value(1) |
| 49 | + |
| 50 | + garbage_collection() |
| 51 | + |
| 52 | + |
| 53 | +button_pin = machine.Pin(0, machine.Pin.IN) |
| 54 | +button_pin.irq(button_pressed) |
23 | 55 |
|
24 | 56 |
|
25 | 57 | print('gc.collect()') |
26 | 58 | gc.collect() |
27 | 59 |
|
28 | 60 |
|
| 61 | +from webswitch import main # noqa isort:skip |
29 | 62 | main() |
0 commit comments