Rewrote the code to not be shit

This commit is contained in:
Benjamyn Love 2022-02-06 21:31:38 +11:00
parent ffce93357e
commit fabccd62bc
2 changed files with 116 additions and 24 deletions

View File

@ -28,9 +28,71 @@ enc_btn.direction = digitalio.Direction.INPUT
# Start the main loop # Start the main loop
last_position = None last_position = None
colour = [0,0,0] colour = [128,128,32]
menu_item = 0 menu_item = 0
sub_menu = False sub_menu = False
while True:
if menu_item == 0:
menu = "[R] G B"
if menu_item == 1:
menu = "R [G] B"
if menu_item == 2:
menu = "R G [B]"
# Get neopixel colour value
current_r, current_g, current_b = neopixel[0]
# Init the oled
oled.fill(0)
# Draw the menu
oled.text(menu, 0, 0, 1)
oled.text(f"{current_r}:{current_g}:{current_b}", 0, 10, 1)
# Show the oled
oled.show()
if last_position is not None:
# If we went right
if enc.position > last_position:
# Check if we are in the sub_menu
if not sub_menu:
if not (menu_item + 1) > 3:
menu_item += 1
else:
if menu_item == 0:
if not (current_r + 1) > 255:
current_r += 1
elif menu_item == 1:
if not (current_g + 1) > 255:
current_g += 1
elif menu_item == 2:
if not (current_b + 1) > 255:
current_b += 1
# If we went left
elif enc.position < last_position:
# Check if we are in the sub_menu
if not sub_menu:
if not (menu_item - 1) < 0:
menu_item -= 1
else:
if menu_item == 0:
if not (current_r - 1) < 0:
current_r -= 1
elif menu_item == 1:
if not (current_g - 1) < 0:
current_g -= 1
elif menu_item == 2:
if not (current_b - 1) < 0:
current_b -= 1
if not enc_btn.value:
sub_menu = not sub_menu
time.sleep(0.1)
neopixel.fill((current_r, current_g, current_b))
last_position = enc.position
while True: while True:
if menu_item == 0: if menu_item == 0:
menu = "[R] G B" menu = "[R] G B"
@ -49,6 +111,7 @@ while True:
if not enc_btn.value: if not enc_btn.value:
sub_menu = not sub_menu sub_menu = not sub_menu
enc.position = 0 enc.position = 0
time.sleep(0.1)
if not sub_menu: if not sub_menu:
menu_item = position % 3 menu_item = position % 3
else: else:

69
code.py
View File

@ -31,6 +31,7 @@ last_position = None
colour = [128,128,32] colour = [128,128,32]
menu_item = 0 menu_item = 0
sub_menu = False sub_menu = False
while True: while True:
if menu_item == 0: if menu_item == 0:
menu = "[R] G B" menu = "[R] G B"
@ -38,29 +39,57 @@ while True:
menu = "R [G] B" menu = "R [G] B"
if menu_item == 2: if menu_item == 2:
menu = "R G [B]" menu = "R G [B]"
# Get neopixel colour value
current_r, current_g, current_b = neopixel[0]
# Init the oled
oled.fill(0) oled.fill(0)
# Draw the menu
oled.text(menu, 0, 0, 1) oled.text(menu, 0, 0, 1)
current_value = neopixel[0] oled.text("*" if sub_menu else " ", 64, 0, 1)
oled.text(f"{current_value[0]}:{current_value[1]}:{current_value[2]}", 0, 10, 1) oled.text(f"{current_r}:{current_g}:{current_b}", 0, 10, 1)
position = enc.position
if last_position == None or position != last_position: # Show the oled
print(position) oled.show()
last_position = position if last_position is not None:
# If we went right
if enc.position > last_position:
# Check if we are in the sub_menu
if not sub_menu:
if not (menu_item + 1) > 3:
menu_item += 1
else:
if menu_item == 0:
if not (current_r + 1) > 255:
current_r += 1
elif menu_item == 1:
if not (current_g + 1) > 255:
current_g += 1
elif menu_item == 2:
if not (current_b + 1) > 255:
current_b += 1
# If we went left
elif enc.position < last_position:
# Check if we are in the sub_menu
if not sub_menu:
if not (menu_item - 1) < 0:
menu_item -= 1
else:
if menu_item == 0:
if not (current_r - 1) < 0:
current_r -= 1
elif menu_item == 1:
if not (current_g - 1) < 0:
current_g -= 1
elif menu_item == 2:
if not (current_b - 1) < 0:
current_b -= 1
if not enc_btn.value: if not enc_btn.value:
sub_menu = not sub_menu sub_menu = not sub_menu
enc.position = 0
time.sleep(0.1) time.sleep(0.1)
if not sub_menu:
menu_item = position % 3
else:
colour[menu_item] = position
if enc.position < 0:
position = 0
enc.position = 0
if enc.position > 255:
position = 255
enc.position = 255
neopixel.fill((colour[0],colour[1],colour[2])) neopixel.fill((current_r, current_g, current_b))
# oled.text(f'Position: {position}', 0, 0, 1) last_position = enc.position
oled.show()