File tree Expand file tree Collapse file tree 3 files changed +57
-31
lines changed
Expand file tree Collapse file tree 3 files changed +57
-31
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ The device will do:
3232Point you Webserver to the IP from the device.
3333
3434You can turn the switch on/off by the web page or the device button.
35+ Reset the device by long pressing the button.
3536
3637
3738## Screenshot
Original file line number Diff line number Diff line change 1+ import gc
2+ import sys
3+
4+ import machine
5+ import utime as time
6+ from leds import power_led , relay
7+ from pins import button_pin
8+
9+
10+ def get_debounced_value (pin ):
11+ """
12+ get debounced value from pin by waiting for stable value
13+ """
14+ cur_value = pin .value ()
15+ stable = 0
16+ while stable < 40 :
17+ if pin .value () == cur_value :
18+ stable += 1
19+ else :
20+ stable = 0
21+ cur_value = pin .value ()
22+ time .sleep_ms (1 )
23+ return cur_value
24+
25+
26+ class Button :
27+ down_start = None
28+
29+ def irq_handler (self , pin ):
30+ power_led .off ()
31+ button_value = get_debounced_value (pin )
32+ power_led .on ()
33+
34+ print ('button_value:' , button_value )
35+ if button_value == 0 :
36+ # button pressed
37+ self .down_start = time .ticks_ms ()
38+ elif button_value == 1 :
39+ # button released
40+ duration_ms = time .ticks_diff (time .ticks_ms (), self .down_start )
41+ print ('duration_ms:' , duration_ms )
42+ if duration_ms > 2000 :
43+ print ('reset after long press...' )
44+ power_led .flash (sleep = 0.1 , count = 20 )
45+ machine .reset ()
46+ sys .exit ()
47+
48+ print ('old state:' , relay )
49+ relay .toggle ()
50+ print ('new state:' , relay )
51+
52+ gc .collect ()
53+
54+
55+ button_pin .irq (Button ().irq_handler )
Original file line number Diff line number Diff line change 1414from wifi import wifi # noqa isort:skip
1515from ntp import ntp_sync # noqa isort:skip
1616from leds import power_led # noqa isort:skip
17+ import button_handler # noqa isort:skip
1718
1819print ('watchdog:' , watchdog )
1920print ('wifi:' , wifi )
2021print ('ntp_sync:' , ntp_sync )
2122print ('power_led:' , power_led )
2223
2324
24- def get_debounced_value (pin ):
25- """get debounced value from pin by waiting for 20 msec for stable value"""
26- cur_value = pin .value ()
27- stable = 0
28- while stable < 20 :
29- if pin .value () == cur_value :
30- stable = stable + 1
31- else :
32- stable = 0
33- cur_value = pin .value ()
34- time .sleep_ms (1 )
35- return cur_value
36-
37-
38- def button_pressed (pin ):
39- print ('button pressed...' )
40- cur_button_value = get_debounced_value (pin )
41- if cur_button_value == 1 :
42- if relay_pin .value () == 1 :
43- print ('turn off by button.' )
44- relay_pin .value (0 )
45- else :
46- print ('turn on by button.' )
47- relay_pin .value (1 )
48-
49- garbage_collection ()
50-
51-
52- button_pin = machine .Pin (0 , machine .Pin .IN )
53- button_pin .irq (button_pressed )
54-
5525
5626print ('gc.collect()' )
5727gc .collect ()
You can’t perform that action at this time.
0 commit comments