def main(): # Instantiate a Grove Rotary Encoder, using signal pins D2 and D3 myRotaryEncoder = upmRotaryEncoder.RotaryEncoder(2, 3); ## Exit handlers ## # This function stops python from printing a stacktrace when you hit control-C def SIGINTHandler(signum, frame): raise SystemExit # This function lets you run code on exit, including functions from myRotaryEncoder def exitHandler(): print "Exiting" sys.exit(0) # Register exit handlers atexit.register(exitHandler) signal.signal(signal.SIGINT, SIGINTHandler) # Read the value every second and detect motion while(1): print "Position: {0}".format(myRotaryEncoder.position()) time.sleep(.1)
def getEncoder(self, pins=(2, 3)): if self.encoder is None: # Instantiate a Grove Rotary Encoder, using signal pins D2 and D3 self.encoder = upmRotaryEncoder.RotaryEncoder(*pins) return self.getProxyObject(self.encoder)
# The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import time, sys, signal, atexit import pyupm_rotaryencoder as upmRotaryEncoder # Instantiate a Grove Rotary Encoder, using signal pins D2 and D3 myRotaryEncoder = upmRotaryEncoder.RotaryEncoder(2, 3) ## Exit handlers ## # This function stops python from printing a stacktrace when you hit control-C def SIGINTHandler(signum, frame): raise SystemExit # This function lets you run code on exit, including functions from myRotaryEncoder def exitHandler(): print "Exiting" sys.exit(0) # Register exit handlers
### Se desea cambiar el color del display usando boton, encoder #Cambia de color rojo a color amarillo utilizando el encoder #Biblioteca LCD RGB import pyupm_i2clcd as lcd #Biblioteca Encoder import pyupm_rotaryencoder as myencoder import time #Se inicializa el encoder en el puerto 4 y 5 encoder = myencoder.RotaryEncoder(4,5) #Inicializan los colores c1 = 0 c2 = 0 c0 = 255 #Direccion del LCD RGB mylcd = lcd.Jhd1313m1(0, 0x3E, 0x62) #Operaciones del LCD RGB mylcd.clear() mylcd.setColor(c0, c1, c2) mylcd.setCursor(0,0) mylcd.write("Encoder y LCD") valorencoder = 0 aux = 1 while 1: valorencoder = abs(encoder.position()) #Si el valor del encoder rebasa 255 vuelve a 0 if valorencoder > 255: valorencoder = valorencoder - 255
#Biblioteca del encoder import pyupm_rotaryencoder as encoder import time #Inicializacion del encoder ejemploEncoder = encoder.RotaryEncoder(4, 5) while 1: print str(ejemploEncoder.position()) time.sleep(0.5)