"""
A simple example showing how to read values from a rotary encoder
Requires the RotaryIRQ library from https://github.com/miketeachman/micropython-rotary
"""
from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd
from picozero import Button
from rotary_irq_rp2 import RotaryIRQ
## specify data and clock pins, in this case 0 and 1 with freq 400kHz
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# get i2c address
I2C_ADDR = i2c.scan()[0]
# create lcd using i2c addr, and specifying rows and columns in screen
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
## Setting rotary pins to 14 and 15
rotary = RotaryIRQ(14, 15)
# set button to 7
button = Pin(7, Pin.IN, Pin.PULL_UP)
current_val = 0 # init encoder val
current_display = 0; # reference
listOfOutputs = [
["Liam","What a stinker"],
["Sam","Uber smelly"],
["Nick","Sheep shagger"],
["Campany","Goat"]
]
# get menu length for limits
menu_length = len(listOfOutputs) - 1
# print first screen
lcd.putstr(str(listOfOutputs[0][0]))
# create menu level check
# 0 == first (names)
# 1 == second (remark)
menu_level = 0
def display_sub_menu():
print("DIsplaying sub menu")
lcd.clear()
lcd.putstr("You selected " + listOfOutputs[current_display][1])
global menu_level
menu_level= 1
print(str(menu_level) + " is the new menu level")
def display_main_menu():
print("Displaying main menu")
lcd.clear()
lcd.putstr(listOfOutputs[current_display][0])
global menu_level
menu_level = 0
rotary.setValue(current_display)
while True:
new_val = rotary.value() # check current val
if button.value() == 0:
print("Menu level is " + str(menu_level))
if menu_level == 0:
display_sub_menu();
else:
print("DIsplaying main")
display_main_menu();
if menu_level == 0:
# print("Menu level is " + str(menu_level))
if current_val != new_val:
print("Current is " + str(current_val))
print("New is " + str(new_val))
print('Encoder value:', new_val)
current_val = new_val # Track this change as the last know value
current_display = current_val
if current_val > menu_length:
print("Setting current_val back to 4")
current_val = menu_length
new_val = menu_length
rotary.setValue(menu_length)
print(str(rotary.value()))
continue
if current_val < 0:
print("Limiting to 0")
current_val = 0
new_val = 0
rotary.setValue(0)
continue
lcd.clear()
lcd.putstr(str(listOfOutputs[int(current_val)][0]))
print("And menu level is " + str(menu_level))
else:
continue