22import json
33import machine
44import neopixel
5+ import urandom
56
67try :
78 import ssd1306
1011 print ("Seems like there is no such module" , error )
1112 blndisplay = False
1213
14+ def load_config ():
15+ """Load configuration from the config.json file.
1316
14- def display_msg (message , line , start = 0 , show = 0 ):
17+ Returns:
18+ json object with configuration.
19+ """
20+ with open ("./config.json" , "r" ) as conf_file :
21+ return json .load (conf_file )
22+
23+ config = load_config ()
24+
25+ def slow_fill_color (neostrip , colors , pstrColor , pixel_count ):
26+ for i in range (0 , pixel_count ):
27+ neostrip [i ] = colors [pstrColor ]
28+ time .sleep_ms (25 )
29+ neostrip .write ()
30+
31+ def get_random_int ():
32+ """Get a random integer 0-255 for color generation.
33+
34+ Returns:
35+ integer 0-255
1536 """
16- Display a message on the OLED screen
37+ int_return = urandom .getrandbits (8 )
38+ if int_return < 256 :
39+ return int_return
40+ else :
41+ return 0
42+
43+ def display_msg (message , line , start = 0 , show = 0 ):
44+ """Display a message on the OLED screen
1745
1846 Args:
1947 message: String with the data to be written on the OLED screen.
@@ -25,15 +53,16 @@ def display_msg(message, line, start=0, show=0):
2553 None.
2654 """
2755 lines = [0 , 8 , 16 , 24 , 32 , 40 , 48 ]
56+
2857 if line in lines :
2958 display .text (message , start , line )
59+
3060 if show :
3161 display .show ()
3262
3363
3464def clear_display (fill = 0 , show = 0 ):
35- """
36- This can be use either to clear the screen will all pixels activated or deactivated by changing
65+ """This can be use either to clear the screen will all pixels activated or deactivated by changing
3766 the blnFill and if the show is set to one it will immediately shown on the OLED screen
3867
3968 Args:
@@ -44,13 +73,13 @@ def clear_display(fill=0, show=0):
4473 None.
4574 """
4675 display .fill (fill )
76+
4777 if show :
4878 display .show ()
4979
5080
5181def print_text (text ):
52- """
53- This will print a text center on the OLED screen.
82+ """This will print a text center on the OLED screen.
5483
5584 Args:
5685 text: String with data to show.
@@ -67,9 +96,8 @@ def print_text(text):
6796 clear_display (0 , 1 )
6897
6998
70- def lapse_time (minutes , color , message = '' ):
71- """
72- Countdown timer to execute on the code that will use other functions to clear the display
99+ def lapse_time (neostrip , minutes , color , message = '' ):
100+ """Countdown timer to execute on the code that will use other functions to clear the display
73101 add other data like string on the screen with the countdown timer.
74102
75103 If no message it will not show anything but the countdown timer.
@@ -82,22 +110,25 @@ def lapse_time(minutes, color, message=''):
82110 None.
83111 """
84112 t_mins = minutes - 1
113+
85114 for mins in range (t_mins , - 1 , - 1 ):
86115 for secs in range (60 , 0 , - 1 ):
87116 data = '{:02d} : {:02d}' .format (mins , secs )
88117 print ("Remaining time:" , data )
118+
89119 for milisecs in range (40 , 0 , - 1 ):
90- set_pixel_color (milisecs % 12 , color )
120+ set_pixel_color (neostrip , milisecs % 12 , color )
91121 time .sleep_ms (25 )
122+
92123 if blndisplay :
93124 clear_display ()
94125 display_msg (data , 24 , 32 )
95126 display_msg ('{:^16}' .format (message ), 0 )
96127 display .show ()
97128
98129
99- def set_pixel_color (neopixel , color ):
100- """
130+ def set_pixel_color (neostrip , neopixel , color ):
131+ """Fill a single pixel color on the neostrip.
101132
102133 Args:
103134 neopixel: Pixel to be fill with color.
@@ -108,15 +139,15 @@ def set_pixel_color(neopixel, color):
108139 """
109140 if neostrip [neopixel ] != (0 , 0 , 0 ):
110141 neostrip [neopixel ] = (0 , 0 , 0 )
142+
111143 else :
112144 if color in config ['colors' ]:
113145 neostrip [neopixel ] = config ['colors' ][color ]
114146 neostrip .write ()
115147
116148
117149def color_neostrip (neostrip , color , pixel_count ):
118- """
119- Set a color to the neopixel.
150+ """Set a color to all neopixels on the neostrip.
120151
121152 Args:
122153 neostrip: neostrip.
@@ -133,8 +164,7 @@ def color_neostrip(neostrip, color, pixel_count):
133164
134165
135166def clear_neostrip (neostrip ):
136- """
137- Shutdown the neostrip or set the value to 0 for all Neopixel on the neostrip.
167+ """Shutdown the neostrip or set the value to 0 for all Neopixel on the neostrip.
138168
139169 Args:
140170 neostrip: neostrip.
@@ -145,41 +175,59 @@ def clear_neostrip(neostrip):
145175 neostrip .fill = (config ['colors' ]['nocolor' ])
146176 neostrip .write ()
147177
148-
149- def load_config ():
150- """
151- Load configuration from the config.json file.
152-
153- Returns:
154- json object with configuration.
155- """
156- with open ("./config.json" , "r" ) as conf_file :
157- return json .load (conf_file )
158-
159-
160- config = load_config ()
161-
162- if blndisplay :
163- try :
164- i2c = machine .I2C (scl = machine .Pin (config ['screen' ]['sda' ]), sda = machine .Pin (config ['screen' ]['scl' ]))
165- display = ssd1306 .SSD1306_I2C (config ['screen' ]['width' ], config ['screen' ]['height' ], i2c )
166- clear_display ()
167- except OSError :
168- print ("Error trying to initialize the code for the OLED" )
169- blndisplay = False
170-
171- button = machine .Pin (config ['button' ]['pin' ], machine .Pin .IN , machine .Pin .PULL_UP )
172- PIXEL_COUNT = config ['neopixel' ]['count' ]
173- neostrip = neopixel .NeoPixel (machine .Pin (config ['neopixel' ]['pin' ]), PIXEL_COUNT )
174- clear_neostrip (neostrip )
175-
176-
177- while True :
178- if (button .value () == 0 ):
179- # Add animation here
180- # time.sleep_ms(3000)
181- # Clear neostrip
182- lapse_time (25 , "red" , "POMODORO" )
183- clear_neostrip (neostrip )
184- lapse_time (5 , "green" , "FREE TIME" )
185- clear_neostrip (neostrip )
178+ def main_logic ():
179+ global display
180+ global blndisplay
181+
182+ if blndisplay :
183+ try :
184+ i2c = machine .I2C (scl = machine .Pin (config ['screen' ]['sda' ]), sda = machine .Pin (config ['screen' ]['scl' ]))
185+ display = ssd1306 .SSD1306_I2C (config ['screen' ]['width' ], config ['screen' ]['height' ], i2c )
186+ clear_display ()
187+ except OSError :
188+ print ("Error trying to initialize the code for the OLED" )
189+ blndisplay = False
190+ display = None
191+
192+ button_ul = machine .Pin (config ['button_ul' ]['pin' ], machine .Pin .IN , machine .Pin .PULL_UP )
193+ button_ur = machine .Pin (config ['button_ur' ]['pin' ], machine .Pin .IN , machine .Pin .PULL_UP )
194+ button_dl = machine .Pin (config ['button_dl' ]['pin' ], machine .Pin .IN , machine .Pin .PULL_UP )
195+ button_dr = machine .Pin (config ['button_dr' ]['pin' ], machine .Pin .IN , machine .Pin .PULL_UP )
196+ neostrip = neopixel .NeoPixel (machine .Pin (config ['neopixel' ]['pin' ]), config ['neopixel' ]['count' ])
197+ clear_neostrip (neostrip )
198+
199+ colors = config ["colors" ]
200+
201+ while True :
202+
203+ if (button_ul .value () == 0 ):
204+ print ("Got 'button_ul' pressed." )
205+ lapse_time (neostrip , 25 , "red" , "POMODORO" )
206+ lapse_time (neostrip , 5 , "green" , "FREE TIME" )
207+
208+ elif (button_ur .value () == 0 ):
209+ print ("Got 'button_ur' pressed." )
210+ for color in colors :
211+ color_neostrip (neostrip , color , config ['neopixel' ]['count' ])
212+ time .sleep (0.5 )
213+ color_neostrip (neostrip , 'nocolor' , config ['neopixel' ]['count' ])
214+
215+ elif (button_dl .value () == 0 ):
216+ print ("Got 'button_dl' pressed." )
217+
218+ for color in colors :
219+ slow_fill_color (neostrip , colors , color , config ['neopixel' ]['count' ])
220+ time .sleep_ms (100 )
221+ color_neostrip (neostrip , 'nocolor' , config ['neopixel' ]['count' ])
222+
223+ elif (button_dr .value () == 0 ):
224+ print ("Got 'button_dr' pressed." )
225+
226+ for i in range (0 , config ['neopixel' ]['count' ]):
227+ color = [get_random_int (), get_random_int (), get_random_int ()]
228+ print (color )
229+ neostrip [i ] = color
230+ time .sleep_ms (25 )
231+ neostrip .write ()
232+ time .sleep_ms (100 )
233+ color_neostrip (neostrip , 'nocolor' , config ['neopixel' ]['count' ])
0 commit comments