"""
Test the MicroPython driver for M5Stack U135, I2C Encoder unit, I2C grove.

Test the common features of the I2C encoder.

* Author(s):
   03 aug 2022: Meurisse D. (shop.mchobby.be) - Initial Writing
"""

from machine import I2C
from i2cenc import I2CEncoder
from time import sleep

# Pico - I2C(0) - sda=GP8, scl=GP9
i2c = I2C(0)
# M5Stack core
# i2c = I2C( sda=Pin(21), scl=Pin(22) )

enc = I2CEncoder(i2c)

print( "Testing the LEDs" )
enc.color = (255,0,0) # Red
sleep( 0.5 )
enc.color = (0,255,0) # Green
sleep( 0.5 )
enc.color = (0,0,255) # Blue
sleep( 1 )
enc.color = (0,0,0) # Off

print( "Press the button and rotate the switch")

last_v = 0
while True:
	if enc.button: # Is the button currently pressed ?
		print( 'Button PRESSED')
		enc.position = 0 # reset the counter - not working!

	v = enc.position # -32768 <= v <= 32767
	if v != last_v: # Only display if value changes
		print( v )
		last_v = v
	sleep( 0.05 )

d2defd63929ea4755b31906fb39833781b6d2a74
<mask token>

<mask token>
print('Testing the LEDs')
<mask token>
sleep(0.5)
<mask token>
sleep(0.5)
<mask token>
sleep(1)
<mask token>
print('Press the button and rotate the switch')
<mask token>
while True:
    if enc.button:
        print('Button PRESSED')
        enc.position = 0
    v = enc.position
    if v != last_v:
        print(v)
        last_v = v
    sleep(0.05)

<mask token>
i2c = I2C(0)
enc = I2CEncoder(i2c)
print('Testing the LEDs')
enc.color = 255, 0, 0
sleep(0.5)
enc.color = 0, 255, 0
sleep(0.5)
enc.color = 0, 0, 255
sleep(1)
enc.color = 0, 0, 0
print('Press the button and rotate the switch')
last_v = 0
while True:
    if enc.button:
        print('Button PRESSED')
        enc.position = 0
    v = enc.position
    if v != last_v:
        print(v)
        last_v = v
    sleep(0.05)

<mask token>
from machine import I2C
from i2cenc import I2CEncoder
from time import sleep
i2c = I2C(0)
enc = I2CEncoder(i2c)
print('Testing the LEDs')
enc.color = 255, 0, 0
sleep(0.5)
enc.color = 0, 255, 0
sleep(0.5)
enc.color = 0, 0, 255
sleep(1)
enc.color = 0, 0, 0
print('Press the button and rotate the switch')
last_v = 0
while True:
    if enc.button:
        print('Button PRESSED')
        enc.position = 0
    v = enc.position
    if v != last_v:
        print(v)
        last_v = v
    sleep(0.05)

"""
Test the MicroPython driver for M5Stack U135, I2C Encoder unit, I2C grove.

Test the common features of the I2C encoder.

* Author(s):
   03 aug 2022: Meurisse D. (shop.mchobby.be) - Initial Writing
"""

from machine import I2C
from i2cenc import I2CEncoder
from time import sleep

# Pico - I2C(0) - sda=GP8, scl=GP9
i2c = I2C(0)
# M5Stack core
# i2c = I2C( sda=Pin(21), scl=Pin(22) )

enc = I2CEncoder(i2c)

print( "Testing the LEDs" )
enc.color = (255,0,0) # Red
sleep( 0.5 )
enc.color = (0,255,0) # Green
sleep( 0.5 )
enc.color = (0,0,255) # Blue
sleep( 1 )
enc.color = (0,0,0) # Off

print( "Press the button and rotate the switch")

last_v = 0
while True:
	if enc.button: # Is the button currently pressed ?
		print( 'Button PRESSED')
		enc.position = 0 # reset the counter - not working!

	v = enc.position # -32768 <= v <= 32767
	if v != last_v: # Only display if value changes
		print( v )
		last_v = v
	sleep( 0.05 )
