Skip to content

Commit ca1c19f

Browse files
authored
Update README.rst
Added Simple Test Example
1 parent 5f8915a commit ca1c19f

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

README.rst

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,92 @@ To install in a virtual environment in your current project:
6060
Usage Example
6161
=============
6262

63-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
63+
.. code-block:: shell
64+
65+
# Simple test for an RGB encoder. This tests incrementing, decrementing, button presses, RGB value changes, Fade setup, etc.
66+
67+
# Author: Ben Shockley
68+
69+
# imports
70+
import i2cEncoderLibV21
71+
import busio
72+
import board
73+
import time
74+
import digitalio
75+
import struct
76+
77+
# Setup the Inturrpt Pin from the encoder.
78+
INT = digitalio.DigitalInOut(board.A3)
79+
INT.direction = digitalio.Direction.INPUT
80+
INT.pull = digitalio.Pull.UP
81+
82+
# Initialize the device.
83+
i2c = busio.I2C(board.SCL, board.SDA)
84+
encoder = i2cEncoderLibV21.i2cEncoderLibV21(i2c, 0x21)
85+
86+
def EncoderChange():
87+
encoder.writeRGBCode(0x00FF00)
88+
valBytes = struct.unpack('>i', encoder.readCounter32())
89+
print ('Changed: {}'.format(valBytes[0]))
90+
91+
def EncoderPush():
92+
encoder.writeRGBCode(0x0000FF)
93+
print ('Encoder Pushed!')
94+
95+
def EncoderRelease():
96+
encoder.writeRGBCode(0x00FFFF)
97+
print ('Encoder Released!')
98+
99+
def EncoderDoublePush():
100+
encoder.writeRGBCode(0xFF00FF)
101+
print ('Encoder Double Push!')
102+
103+
def EncoderMax():
104+
encoder.writeRGBCode(0xFF0000)
105+
print ('Encoder max!')
106+
107+
def EncoderMin():
108+
encoder.writeRGBCode(0xFF0000)
109+
print ('Encoder min!')
110+
111+
def EncoderFade():
112+
encoder.writeRGBCode(0x000000)
113+
114+
def Encoder_INT(self):
115+
encoder.updateStatus()
116+
117+
# Start by resetting the encoder. Reset takes 400us , so let us give it time to settle.
118+
encoder.reset()
119+
time.sleep(.1)
120+
121+
# When the board was initialized, the default config was loaded. Here we can override that config if we want.
122+
encconfig = (i2cEncoderLibV21.INT_DATA | i2cEncoderLibV21.WRAP_DISABLE | i2cEncoderLibV21.DIRE_RIGHT | i2cEncoderLibV21.IPUP_ENABLE | i2cEncoderLibV21.RMOD_X1 | i2cEncoderLibV21.RGB_ENCODER)
123+
encoder.begin(encconfig)
124+
125+
# Setup other varibles
126+
encoder.writeCounter(0)
127+
encoder.writeMax(10)
128+
encoder.writeMin(-10)
129+
encoder.writeStep(1)
130+
encoder.writeAntiBouncePeriod(25)
131+
encoder.writeDoublePushPeriod(50)
132+
encoder.writeFadeRGB(2)
133+
134+
# Declare callbacks
135+
encoder.onChange = EncoderChange
136+
encoder.onButtonRelease = EncoderRelease
137+
encoder.onButtonPush = EncoderPush
138+
encoder.onButtonDoublePush = EncoderDoublePush
139+
encoder.onMax = EncoderMax
140+
encoder.onMin = EncoderMin
141+
encoder.onFadeProcess = EncoderFade
142+
143+
# Autoconfigure the interrupt register according to the callbacks declared.
144+
encoder.autoconfigInterrupt()
145+
146+
while True:
147+
if not INT.value: #If INT pin goes LOW - we know the encoder status changed.
148+
Encoder_INT(encoder)
64149
65150
Contributing
66151
============

0 commit comments

Comments
 (0)