@ajb2k3 Sorry for the delay, I didn't receive a notification.
Yes, I'm following the documentation and guidance on ports. Although I'm finding the Ext-Encoder documentation a bit lacking as far as what the functions do / return.
I have the Ext-Encoder working in Port C and started with both the limit and optical switch in Port B as they list the GPIO port on the label, but started trying other options when that failed to work. I haven't been able to get the optical switch to work and the only way I got the limit switch to work was using the pin directly.
Loving the system, just not sure what I'm doing wrong. I appreciate the help!
Here's my current functional (outside of the non-zero reset bug) code. It's really just an encoder (port C), limit switch (port B), ui elements and minor logic.
import os, sys, io
import M5
from M5 import *
from boot_option import *
from hardware import *
from unit import ExtEncoderUnit
import time
import m5utils
label0 = None
label1 = None
charging = None
Title = None
EncoderValue = None
batteryPerc = None
rect0 = None
buttonState = None
image0 = None
i2c0 = None
pin8 = None
Encoder = None
encoderposition = None
ButtonState = None
Located = None
SliderPosition = None
LocTimer = None
displayTimer = None
def setup():
global label0, label1, charging, Title, EncoderValue, batteryPerc, rect0, buttonState, image0, i2c0, pin8, Encoder, encoderposition, ButtonState, Located, SliderPosition, LocTimer, displayTimer
M5.begin()
Widgets.fillScreen(0x222222)
label0 = Widgets.Label("Value:", 26, 36, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
label1 = Widgets.Label("Limit:", 31, 76, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
charging = Widgets.Circle(252, 34, 4, 0x222222, 0x222222)
Title = Widgets.Title("Slider", 5, 0xffffff, 0x285c4d, Widgets.FONTS.DejaVu18)
EncoderValue = Widgets.Label("null", 95, 36, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
batteryPerc = Widgets.Label("100%", 263, 25, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
rect0 = Widgets.Rectangle(0, 138, 20, 100, 0x285c4d, 0x285c4d)
buttonState = Widgets.Circle(102, 86, 7, 0xffffff, 0x222222)
image0 = Widgets.Image("res/img/logo_cropped.png", 177, 64, scale_x=.07, scale_y=.07)
set_boot_option(1)
i2c0 = I2C(0, scl=Pin(18), sda=Pin(17), freq=100000)
Encoder = ExtEncoderUnit(i2c0, 0x59)
time.sleep_ms(500)
Encoder.reset_rotary_value()
Encoder.set_rotary_value(0)
Located = False
pin8 = Pin(8, mode=Pin.IN)
def loop():
global label0, label1, charging, Title, EncoderValue, batteryPerc, rect0, buttonState, image0, i2c0, pin8, Encoder, encoderposition, ButtonState, Located, SliderPosition, LocTimer, displayTimer
M5.update()
batteryPerc.setText(str((str((Power.getBatteryLevel())) + str('%'))))
if Power.isCharging():
charging.setColor(color=0x222222, fill_c=0x33cc00)
else:
charging.setColor(color=0x222222, fill_c=0x222222)
encoderposition = Encoder.get_rotary_value()
EncoderValue.setText(str(encoderposition))
ButtonState = (pin8.value() ^ (0x01 << 0))
print(encoderposition)
if ButtonState == 1:
Located = True
buttonState.setColor(color=0xFFFFFF, fill_c=0xFFFFFF)
Encoder.reset_rotary_value()
Encoder.set_rotary_value(0)
else:
buttonState.setColor(color=0xFFFFFF, fill_c=0x222222)
if Located == False and (time.ticks_diff((time.ticks_ms()), LocTimer)) > 500:
print('Location unknown')
LocTimer = time.ticks_ms()
SliderPosition = min(max(m5utils.remap(encoderposition, 0, 45140, 298, 0), 0), 298)
if (time.ticks_diff((time.ticks_ms()), displayTimer)) > 15:
rect0.setCursor(x=(int(SliderPosition)), y=138)
displayTimer = time.ticks_ms()
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")