def bistmon(self, quadno, monsig=False): min = 0 if quadno < 4 else 3 max = 3 if quadno < 4 else 6 for i in range(min, max): self.gp.bist(i, True) if quadno == i: self.gp.quad[i].get_pin(1).value = 1 self.gp.quad[i].get_pin(3).value = monsig else: self.gp.quad[i].get_pin(1).value = 0 self.gp.quad[i].get_pin(3).value = 0 time.sleep(0.2) if quadno < 3: p = AnalogIn(board.LQ) else: p = AnalogIn(board.RQ) val = p.value self.gp.quad[quadno].get_pin(1).value = 0 self.gp.quad[quadno].get_pin(3).value = 0 for i in range(min, max): self.gp.bist(i, False) p.deinit() del p del i del min del max return val
def __init__(self): # Neopixels self.neopixels = neopixel.NeoPixel(board.NEOPIXEL, 4, brightness=0.3) self._neopixel_disable = DigitalInOut(board.NEOPIXEL_POWER) self._neopixel_disable.direction = Direction.OUTPUT self._neopixel_disable.value = False # Battery Voltage self._batt_monitor = AnalogIn(board.BATTERY) # Speaker Enable self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE) self._speaker_enable.direction = Direction.OUTPUT self._speaker_enable.value = False # Light Sensor self._light = AnalogIn(board.LIGHT) # Buttons self.buttons = [] for pin in (board.BUTTON_A, board.BUTTON_B, board.BUTTON_C, board.BUTTON_D): switch = DigitalInOut(pin) switch.direction = Direction.INPUT switch.pull = Pull.UP self.buttons.append(switch)
def __init__(self, enButtons=True): self._buttonA = None self._buttonB = None self._enbuttons = False if enButtons: self._enbuttons = True self._buttonA = DigitalInOut(board.IO43) self._buttonA.switch_to_input(pull=Pull.UP) self._db_buttonA = Debouncer(self._buttonA, 0.01, True) self._buttonB = DigitalInOut(board.IO44) self._buttonB.switch_to_input(pull=Pull.UP) self._db_buttonB = Debouncer(self._buttonB, 0.01, True) # 3x Neopixels RGB pixels: IO0 self._pixels = neopixel.NeoPixel( board.IO0, 3, \ brightness=0.5, auto_write=False, pixel_order=neopixel.GRB ) self._colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] for i in range(3): self._pixels[i] = self._colors[i] self._pixels.show() self._pixelsRotationDt = 0.2 self._pixelsRotationSt = time.monotonic() # Sense color Sensor (颜色传感器): IO7 self._colorSensor = AnalogIn(board.IO7) # Lighting Sensor (光强度传感器): IO8--leftLightSensor, IO9--rightLightSensor self._leftLightSensor = AnalogIn(board.IO8) self._rightLightSensor = AnalogIn(board.IO9) # left and right head-LED: IO17--rightHeadLED, IO18--leftHeadLED self._leftHeadLED = DigitalInOut(board.IO18) self._leftHeadLED.direction = Direction.OUTPUT self._leftHeadLED.value = 0 self._rightHeadLED = DigitalInOut(board.IO17) self._rightHeadLED.direction = Direction.OUTPUT self._rightHeadLED.value = 0 # Ultrasonic module (超声波模块): IO11--trig, IO10--echo self._timeout = 0.1 self._trig = DigitalInOut(board.IO11) self._trig.direction = Direction.OUTPUT self._trig.value = 0 if _USE_PULSEIO: self._echo = PulseIn(board.IO10) self._echo.pause() self._echo.clear() else: self._echo = DigitalInOut(board.IO10) self._echo.direction = Direction.INPUT # Tracking sensors (循迹传感器): IO4--rightTracker, IO3--rightTracker self._leftTracker = DigitalInOut(board.IO4) self._leftTracker.direction = Direction.INPUT self._rightTracker = DigitalInOut(board.IO3) self._rightTracker.direction = Direction.INPUT # Motor (Right): IO41--rightMotor1IN, IO42--rightMotor2IN self._rightMotor1IN = PWMOut(board.IO41, frequency=100, duty_cycle=0) self._rightMotor2IN = PWMOut(board.IO42, frequency=100, duty_cycle=0) # Motor (Left): IO12--leftMotor1IN, IO40--leftMotor2IN self._leftMotor1IN = PWMOut(board.IO12, frequency=100, duty_cycle=0) self._leftMotor2IN = PWMOut(board.IO40, frequency=100, duty_cycle=0)
def __init__(self): # Define i2c self._i2c = busio.I2C(board.SCL, board.SDA, frequency=100000) self._cutebot = 0x10 self._i2c_rest = 0.1 self._error_thresh = 12 # Define sound self._buzzer = pulseio.PWMOut(board.P0, variable_frequency=True) # Define headlights self._RGB_RIGHT_HEADLIGHT = 0x04 self._RGB_LEFT_HEADLIGHT = 0X08 # Define neopixels self._rainbow_pixels = neopixel.NeoPixel(board.D15, 2) # Define motor states self._LEFT_MOTOR = 0x01 self._RIGHT_MOTOR = 0x02 self._BACKWARDS = 0x01 self._FORWARDS = 0x02 # Define servo self._servoMaxAngleInDegrees = 180 self._SERVO_S1 = 0x05 self._SERVO_S2 = 0x06 # Define expansion pins self._p1 = AnalogIn(board.P1) self._p2 = AnalogIn(board.P2) # Define ultrasound sonar self._sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D8, echo_pin=board.D12) # Define line tracking sensors # Left sensor self._leftLineTracking = DigitalInOut(board.D13) self._leftLineTracking.direction = Direction.INPUT self._leftLineTracking.pull = None # Right sensor self._rightLineTracking = DigitalInOut(board.D14) self._rightLineTracking.direction = Direction.INPUT self._rightLineTracking.pull = None # Reset cutebot self.motorsOff() self.lightsOff()
def new_game(skill_level): # Seed the random function with noise a4 = AnalogIn(board.A4) a5 = AnalogIn(board.A5) a6 = AnalogIn(board.A6) a7 = AnalogIn(board.A7) seed = a4.value seed += a5.value seed += a6.value seed += a7.value random.seed(seed) # Populate the game sequence return [random.randint(1, 4) for i in range(SEQUENCE_LENGTH[skill_level])]
def __init__(self): self._up = DigitalInOut(board.UP_BUTTON) self._up.switch_to_input(pull=Pull.UP) self._down = DigitalInOut(board.DOWN_BUTTON) self._down.switch_to_input(pull=Pull.UP) self._left = DigitalInOut(board.LEFT_BUTTON) self._left.switch_to_input(pull=Pull.UP) self._right = DigitalInOut(board.RIGHT_BUTTON) self._right.switch_to_input(pull=Pull.UP) self._action = DigitalInOut(board.ACTION_BUTTON) self._action.switch_to_input(pull=Pull.UP) self._battery = AnalogIn(board.BATTERY_SENSE) self._led = DigitalInOut(board.LED) self._led.switch_to_output(value=True) self._vibration = DigitalInOut(board.VIBRATION_MOTOR) self._vibration.switch_to_output() self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 4) self._i2c = None self._lis3dh = None self._spi = None self._display_bus = None self._display = None self._sound = None self._midi = None self._gamepad = None
def _init_sensors(self): i2c = board.I2C() if self.debug: self._scan_bus(i2c) self.bme280 = Adafruit_BME280_I2C(i2c, address=0x76) self.bme280.sea_level_pressure = 1026 try: self.sgp30 = Adafruit_SGP30(i2c) self.sgp30.iaq_init() self.sgp30.set_iaq_baseline(0x8973, 0x8AAE) except ValueError as err: self.logger.warning("SGP30 not found") self.sgp30 = None # self._pm_reset = DigitalInOut(board.D6) # self._pm_enable = DigitalInOut(board.D5) # self._pm_enable.direction = Direction.OUTPUT # self._pm_enable.value = True # uart = board.UART() # uart.baudrate = 9600 # uart.timeout = 4 # self.pms5003 = PM25_UART(uart, self._pm_reset) self.pms5003 = None self.ltr559 = LTR559(i2c_dev=i2c) self.battery = AnalogIn(board.VOLTAGE_MONITOR) self.divider_ratio = 2
def __init__(self, *args, **kwargs): if "type" in kwargs: self._type = kwargs["type"] else: self._type = 0 self._ref = DigitalInOut(args[1]) self._ANALOG_RESOLUTION = 65536 self._VOLTAGE = 3.3 if self._type == 0: # this is a round sensor self._ZERO_OFFSET = 800 self._READ_RANGE = 1450 self._wiper = AnalogIn(args[0]) self._d0 = DigitalInOut(args[2]) self._d120 = DigitalInOut(args[3]) self._d240 = DigitalInOut(args[4]) self._ZERO_OFFSET = 800 self._READ_RANGE = 1450 self._LENGTH = 120 else: # this is a linear sensor self._wiper_pin = args[0] self._wiper = DigitalInOut(args[0]) self._v1 = DigitalInOut(args[2]) self._v2 = DigitalInOut(args[3]) if self._type == 1: self._ZERO_OFFSET = 200 self._READ_RANGE = 2600 self._LENGTH = 100 else: self._ZERO_OFFSET = 500 self._READ_RANGE = 1900 self._LENGTH = 55
def __init__(self, pgpin, analogpin=None): self.pg = DigitalInOut(pgpin) self.pg.switch_to_input(pull=Pull.UP) if analogpin: self.mon = AnalogIn(analogpin) else: self.mon = None
def touch_point(self): # pylint: disable=too-many-locals """A tuple that represents the x, y and z (touch pressure) coordinates of a touch. Or, None if no touch is detected""" z_1 = z_2 = z = None with DigitalInOut(self._xp_pin) as x_p: x_p.switch_to_output(False) with DigitalInOut(self._ym_pin) as y_m: y_m.switch_to_output(True) with AnalogIn(self._xm_pin) as x_m: z_1 = x_m.value with AnalogIn(self._yp_pin) as y_p: z_2 = y_p.value # print(z_1, z_2) z = 65535 - (z_2 - z_1) if z > self._zthresh: with DigitalInOut(self._yp_pin) as y_p: with DigitalInOut(self._ym_pin) as y_m: with AnalogIn(self._xp_pin) as x_p: y_p.switch_to_output(True) y_m.switch_to_output(False) for i in range( # pylint: disable=consider-using-enumerate len(self._xsamples)): self._xsamples[i] = x_p.value x = sum(self._xsamples) / len(self._xsamples) x_size = 65535 if self._size: x_size = self._size[0] x = int( map_range(x, self._calib[0][0], self._calib[0][1], 0, x_size)) with DigitalInOut(self._xp_pin) as x_p: with DigitalInOut(self._xm_pin) as x_m: with AnalogIn(self._yp_pin) as y_p: x_p.switch_to_output(True) x_m.switch_to_output(False) for i in range( # pylint: disable=consider-using-enumerate len(self._ysamples)): self._ysamples[i] = y_p.value y = sum(self._ysamples) / len(self._ysamples) y_size = 65535 if self._size: y_size = self._size[1] y = int( map_range(y, self._calib[1][0], self._calib[1][1], 0, y_size)) return (x, y, z) return None
def battery_check_state(): battery_voltage_pin = AnalogIn(board.AD0) battery_voltage = get_voltage(battery_voltage_pin) print(battery_voltage) if battery_voltage < 2.2: ledBlinkCount = 10 while ledBlinirection = Direction.OUTPUT
def __init__(self, pin, name, type='Digital'): if type == 'Digital': self.pin = DigitalInOut(pin) self.debounced = Debouncer(self.pin) self.debounceRose = Debouncer(self.pin) self.debounceFell = Debouncer(self.pin) elif type == 'Analog': self.pin = AnalogIn(pin) self.name = name
def setup(): # fingers corssed, the seeding makes sense to really get random colors... apin = AnalogIn(analog_input) random.seed(apin.value) apin.deinit() # let's go! nextcolor() strip.write()
def __init__(self): Debug.begin(debug_level=Debug.DEBUG) self.force_sensor = AnalogIn(board.A2) self.current_sensor = AnalogIn(board.A3) self.switch = digitalio.DigitalInOut(board.D10) self.switch.direction = digitalio.Direction.INPUT self.switch.pull = digitalio.Pull.UP self.running = False self.force_times = [] self.force_data = [] self.current_times = [] self.current_data = [] self.start_time = millis() self.chrono = chronometer() self.chrono_time = 1 # THIS IS THE DELAY THAT CAN BE CHANGED AND CONFIGURED self.chrono.set(self.chrono_time) self.chrono.start()
def touch_point(self): with DigitalInOut(self._yp_pin) as yp: with DigitalInOut(self._ym_pin) as ym: with AnalogIn(self._xp_pin) as xp: yp.switch_to_output(True) ym.switch_to_output(False) for i in range(len(self._xsamples)): self._xsamples[i] = xp.value x = sum(self._xsamples) / len(self._xsamples) x_size = 65535 if self._size: x_size = self._size[0] x = int(map_range(x, self._calib[0][0], self._calib[0][1], 0, x_size)) with DigitalInOut(self._xp_pin) as xp: with DigitalInOut(self._xm_pin) as xm: with AnalogIn(self._yp_pin) as yp: xp.switch_to_output(True) xm.switch_to_output(False) for i in range(len(self._ysamples)): self._ysamples[i] = yp.value y = sum(self._ysamples) / len(self._ysamples) y_size = 65535 if self._size: y_size = self._size[1] y = int(map_range(y, self._calib[1][0], self._calib[1][1], 0, y_size)) z1 = z2 = z = None with DigitalInOut(self._xp_pin) as xp: xp.switch_to_output(False) with DigitalInOut(self._ym_pin) as ym: ym.switch_to_output(True) with AnalogIn(self._xm_pin) as xm: z1 = xm.value with AnalogIn(self._yp_pin) as yp: z2 = yp.value #print(z1, z2) z = 65535 - (z2 - z1) if z > self._zthresh: return (x, y, z) return None
def __init__(self, pin_number): self.lightLevel = AnalogIn(pin_number) self.lastLevel = 0 self.minRecorded = 65535 self.maxRecorded = 0 self.samplingStartTime = 0 self.lightLevelBright = 56054 self.lightLevelMedium = 32031 self.lightLevelDim = 14414 self.firstDay = True self.lightLevelSamplingStartTime = time.monotonic() self.lastLightReadTime = 0
def __init__(self, pin, id, pullup_resistor=3300, probe_offset=0.0, loop_time=500): self.pullup_resistor = pullup_resistor self.pin = AnalogIn(pin) self._data_str = "Thermistor " + str(id) + " temp" self.chrono = chronometer() self.chrono.setAndGo(loop_time) self._temp_offset = probe_offset
def read_buttons(): with AnalogIn(board.A3) as ain: reading = ain.value / 65535 if reading > 0.75: return None if reading > 0.4: return 4 if reading > 0.25: return 3 if reading > 0.13: return 2 return 1
def __init__(self, pin, name, outputScale=20.0, deadbandCutoff=0.1, weight=0.2): self.name = name self.pin = AnalogIn(pin) self.outputScale = outputScale self.deadbandCutoff = deadbandCutoff self.weight = weight self.alpha = self._Cubic(self.deadbandCutoff)
def __init__(self): self.vbatt = AnalogIn(board.IO17) LED = digitalio.DigitalInOut(board.LED) LED.switch_to_output(True) self.spi = board.SPI() self.R1_CS = digitalio.DigitalInOut(board.D5) self.R2_CS = digitalio.DigitalInOut(board.D20) self.R3_CS = digitalio.DigitalInOut(board.D12) self.R1_CS.switch_to_output(True) self.R2_CS.switch_to_output(True) self.R3_CS.switch_to_output(True) self._BUFFER = bytearray(256)
def read_buttons(): btn = 1 with AnalogIn(board.A3) as ain: reading = ain.value / 65535 if reading > 0.75: btn = None if reading > 0.4: btn = 4 if reading > 0.25: btn = 3 if reading > 0.13: btn = 2 btn = 1 return btn
def __init__(self, board): if type == 'i2c': self.button = digitalio.DigitalInOut(board.A0) self.upDown = AnalogIn(board.A3) self.rightLeft = AnalogIn(board.A4) elif type == 'spi': self.button = digitalio.DigitalInOut(board.A3) self.upDown = AnalogIn(board.A4) self.rightLeft = AnalogIn(board.A5) self.up_dir = 1 self.right_dir = 1 self.button.direction = digitalio.Direction.INPUT self.button.pull = digitalio.Pull.UP up_sum = 0.0 right_sum = 0.0 for i in range(0, 1000, 1): up_sum += ((self.upDown.value / 65356) - 0.5) right_sum += ((self.rightLeft.value / 65536) - 0.5) self.up_cal = up_sum / 1000 self.right_cal = right_sum / 1000
def battery_check_state(): ledBlinkCount = 10 battery_voltage_pin = AnalogIn(board.AD0) battery_voltage = get_voltage(battery_voltage_pin) print(battery_voltage) if battery_voltage < 3.4: while ledBlinkCount > 10: blue.value = True time.sleep(0.25) blue.value = False green.value = True time.sleep(0.25) green.value = False ledBlinkCount -= 1
def init(): LED = namedtuple('LED', ('power', 'mac', 'ip')) led = LED((DigitalInOut(board.D1), DigitalInOut(board.D4)), (DigitalInOut(board.D18), DigitalInOut(board.D19)), (DigitalInOut(board.D12), DigitalInOut(board.D11))) led.power[0].direction = Direction.OUTPUT led.power[1].direction = Direction.OUTPUT led.mac[0].direction = Direction.OUTPUT led.mac[1].direction = Direction.OUTPUT led.ip[0].direction = Direction.OUTPUT led.ip[1].direction = Direction.OUTPUT voltage = AnalogIn(board.A3) return (led, lambda: voltage.value * 2 * 3.27 / 65536)
def __init__(self, debug=False): """its the init :P.""" from analogio import AnalogIn self._BAT = AnalogIn(board.VOLTAGE_MONITOR) self._PIN_LIST = dir(board) self.debug = debug # self.NeoPixPin = board.NEOPIXEL #turn off the neopixel # NeoPix = neopixel.NeoPixel(board.NEOPIXEL,1) # NeoPix.brightness = 0 neopixel.NeoPixel(board.NEOPIXEL, 1).brightness = 0 if self.debug: print('C4H base class initialised')
def setup(): global fade # Random number generator is seeded from an unused 'floating' # analog input - this helps ensure the random color choices # aren't always the same order. pin = AnalogIn(board.A0) random.seed(pin.value) pin.deinit() for he in range(n_horns): for wi in range(n_waves): random_wave(he, wi) fade = 233 + n_leds / 2 if fade > 233: fade = 233
def __init__(self): super(NightLight, self).__init__() pixels = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=1, brightness=1.0) pixels.fill((0, 0, 0)) pixels.show() self._on = False self._animator = PixelAnimator(pixels) self._irSensor = AnalogIn(IR_PROXIMITY) self._irInput = DigitalInOut(REMOTEIN) self._irInput.direction = Direction.INPUT self._irOutput = DigitalInOut(REMOTEOUT) self._irOutput.direction = Direction.OUTPUT self._microphone = DigitalInOut(MICROPHONE_DATA) self._irOutput.direction = Direction.INPUT
def __init__(self): # Dotstars self.dotstars = adafruit_dotstar.DotStar(board.DOTSTAR_CLOCK, board.DOTSTAR_DATA, 5, brightness=0.3) # Light Sensor self._light = AnalogIn(board.LIGHT) # Buttons self._buttons = [] for pin in (board.BUTTON_DOWN, board.BUTTON_SELECT, board.BUTTON_UP): switch = DigitalInOut(pin) switch.direction = Direction.INPUT switch.pull = Pull.DOWN self._buttons.append(switch) # Cap Tocuh Pads self._ctp = [] for pin in ( board.CAP6, board.CAP7, board.CAP8, board.CAP13, board.CAP12, board.CAP11, board.CAP10, board.CAP9, ): cap = touchio.TouchIn(pin) self._ctp.append(cap) # LED self._led = DigitalInOut(board.LED) self._led.direction = Direction.OUTPUT # PIR Sensor self._pir = DigitalInOut(board.PIR_SENSE) self._pir.direction = Direction.INPUT
def _get_linear_position(self, tail_to_tip=True): self._wiper.deinit() l_wiper = AnalogIn(self._wiper_pin) self._ref.switch_to_input(pull=Pull.DOWN) if tail_to_tip: # Read from tail end self._v1.value = 1 time.sleep(0.001) value = self._get_voltage(l_wiper) self._v1.value = 0 else: self._v2.value = 1 time.sleep(0.001) value = self._get_voltage(l_wiper) self._v2.value = 0 l_wiper.deinit() self._wiper = DigitalInOut(self._wiper_pin) self._wiper.direction = Direction.OUTPUT self._wiper.value = 0 self._ref.switch_to_output(value=False) return self._get_millimeters(value)
def battery_check_state(): global red_led_pwm, green_led_pwm, blue_led_pwm, keypad_rows, masterLEDDutyCycle led_blink_count = 0 # de-init A0 pin from keypad temp keypad_rows[0].deinit() # give the pin a bit to fully de-initialize time.sleep(0.075) battery_voltage_pin = AnalogIn(board.A0) battery_voltage = get_voltage(battery_voltage_pin) * 2 print("Battery Voltage: ", battery_voltage) # if voltage drops below 5 then blink LEDs 3 times if battery_voltage < 5: while led_blink_count < 3: red_led_pwm.duty_cycle = masterLEDDutyCycle time.sleep(0.20) red_led_pwm.duty_cycle = 0 blue_led_pwm.duty_cycle = masterLEDDutyCycle time.sleep(0.20) blue_led_pwm.duty_cycle = 0 green_led_pwm.duty_cycle = masterLEDDutyCycle time.sleep(0.20) green_led_pwm.duty_cycle = 0 led_blink_count += 1 # de-init battery_voltage_pin battery_voltage_pin.deinit() # give the pin a bit to fully de-initialize time.sleep(0.075) # re-init keyboard pin for key on keypad keypad_rows[0] = digitalio.DigitalInOut(board.A0)