# youknow.py # # Library for use with EduShields YouKnow board for layering on top of # MrYsLab's PyMata API for SJSU ME30. # Provides interfaces to each of the board's components using objects for each # rather than the lower-level Arduino-like API. # Version history: # 1.1.1 09-Oct-2018 Added optional parameter "activeLevel" to Led constructor # 1.0 01-Jan-2018 Initial Revision (eric@edushields.com) # from pymata_aio.constants import Constants # Digital pin assignments PIN_SW0 = 12 PIN_SW1 = 8 PIN_SW2 = 7 PIN_SW3 = 4 PIN_LED0 = 11 PIN_LED1 = 9 PIN_LED2 = 6 PIN_LED3 = 3 PIN_SPEAKER = 5 PIN_SERVO = 10 PIN_LASER = 13 PIN_ENC_CH1 = 17 PIN_ENC_CH2 = 18 PIN_ENC_SW = 19 PIN_RGB_RED = 11 PIN_RGB_GREEN = 9 PIN_RGB_BLUE = 6 PIN_RGB_RED_V3 = 3 PIN_TEMP = 0 PIN_POT = 1 PIN_ALS = 2 class Button: def __init__(self, board, pin, activeLevel=1, internalPull=None): self.board = board self.pin = pin self.activeLevel = activeLevel board.set_pin_mode(pin, Constants.INPUT) if internalPull == None: return if internalPull: if activeLevel == 0: board.digital_write(pin, 1) else: raise valueError("no internal ATmega328 pulldown") # Detect (software-debounced) button press # If a button is first detected not to be pressed, wait BUTTON_DEBOUNCE_DELAYMSECS_QUICK # just in case it's in the middle of a bounce. # NB: This function always blocks for at least BUTTON_DEBOUNCE_QUICK_DELAY seconds # FIXME: Consider making both bounce wait durations settable def isPressed(self): sample1 = self.board.digital_read(self.pin) self.board.sleep(0.002) sample2 = self.board.digital_read(self.pin) if (sample1 != self.activeLevel) and (sample2 != self.activeLevel): return False if (sample1 == self.activeLevel) and (sample2 == self.activeLevel): return True # we know that the samples were different, so wait longer self.board.sleep(0.020) return (self.board.digital_read(self.pin) == self.activeLevel) class Led: def __init__(self, board, pin, activeLevel=1): self.board = board self.pin = pin self.activeLevel = activeLevel board.set_pin_mode(pin, Constants.OUTPUT) def on (self, val=True): if (self.activeLevel == 0): self.board.digital_write(self.pin, 0) else: self.board.digital_write(self.pin, 1) def off(self): if (self.activeLevel == 0): self.board.digital_write(self.pin, 1) else: self.board.digital_write(self.pin, 0) class LedPWM: def __init__(self, board, pin, analogOutBits): self.board = board self.pin = pin self.analogOutMax = (2**analogOutBits)-1 board.set_pin_mode(pin, Constants.PWM) def on(self, val=True): if val: self.board.analog_write(self.pin, self.analogOutMax) else: self.board.analog_write(self.pin, 0) def off(self): self.board.analog_write(self.pin, 0) def fraction(self, fract): self.board.analog_write(self.pin, int(self.analogOutMax*fract)) class Encoder: def __init__(self, board, enc1, enc2): self.board = board self.ch1 = enc1 self.ch2 = enc2 self.cnt = 0 board.encoder_config(enc1, enc2, self.cb_encoder, Constants.CB_TYPE_DIRECT) def cb_encoder(self, data): if (type(data) != list): # strangely returns list at start self.cnt = data def count(self): return self.cnt class Pot: def __init__(self, board, pin, analogInBits): self.board = board self.pin = pin self.analogInMax = (2**analogInBits)-1 board.set_pin_mode(pin, Constants.ANALOG) def fraction(self): return self.board.analog_read(self.pin)/self.analogInMax class ALS: def __init__(self, board, pin, analogInBits): self.board = board self.pin = pin self.analogInMax = (2**analogInBits)-1 board.set_pin_mode(pin, Constants.ANALOG) def fraction(self): return self.board.analog_read(self.pin)/self.analogInMax class Servo: def __init__(self, board, pin): self.board = board self.pin = pin board.servo_config(pin) def write(self, degrees): self.board.analog_write(self.pin, int(degrees)) class LM34: def __init__(self, board, pin, volts_per_adc_count): self.board = board self.pin = pin self.v_per_adc = volts_per_adc_count board.set_pin_mode(pin, Constants.ANALOG) def degreesF(self): adc = self.board.analog_read(self.pin) return (adc*self.v_per_adc)/.010 # 0.010V/degF class Speaker: def __init__(self, board, pin): self.board = board self.pin = pin def play(self, freq, duration=None): self.board.play_tone(self.pin, Constants.TONE_TONE, freq, duration) def playStop(self): self.board.play_tone(self.pin, Constants.TONE_NO_TONE, 0)