class NightLight(nonblocking_timer):
    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 stop(self):
        print('stop')
        self._irSensor.deinit()
        self._irInput.deinit()

    def next(self):
        # print("ir: %d mic: %d" % (self._irSensor.value, self._irOutput.value))

        self._animator.next()
Beispiel #2
0
 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 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()
Beispiel #4
0
class AnalogIn(object):
    """Basic analog input."""
    def __init__(self, pin):
        self._pin = AnalogIn_(pin)

    def deinit(self):
        self._pin.deinit()

    @property
    def value(self):
        return self._pin.value

    @property
    def max_adc(self):
        return MAX_ADC
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 _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)
Beispiel #7
0
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)
    def _get_linear_force(self):
        self._wiper.deinit()
        l_wiper = AnalogIn(self._wiper_pin)
        self._ref.value = 0
        self._v1.switch_to_output(value=True)
        self._v2.switch_to_input()
        time.sleep(0.001)
        wiper_1 = l_wiper.value

        self._v2.switch_to_output(value=True)
        self._v1.switch_to_input()
        time.sleep(0.001)
        wiper_2 = l_wiper.value

        l_wiper.deinit()
        self._wiper = DigitalInOut(self._wiper_pin)
        self._wiper.direction = Direction.OUTPUT
        self._wiper.value = 0
        self._v1.direction = Direction.OUTPUT
        self._v1.value = 0
        self._v2.value = 0

        return ((
            (wiper_1 + wiper_2) / 2) * self._VOLTAGE) / self._ANALOG_RESOLUTION
Beispiel #9
0
class PiperJoystickAxis:
    def __init__(self, pin, outputScale=20.0, deadbandCutoff=0.1, weight=0.2):
        self.pin = AnalogIn(pin)
        self.outputScale = outputScale
        self.deadbandCutoff = deadbandCutoff
        self.weight = weight
        self.alpha = self._Cubic(self.deadbandCutoff)

    def deinit(self):
        self.pin.deinit()

    # Cubic function to map input to output in such a way as to give more precision
    # for lower values
    def _Cubic(self, x):
        return self.weight * x**3 + (1.0 - self.weight) * x

    # Eliminate the jump present in the deadband, but use the cubic function to give
    # more precision to lower values
    #
    def _cubicScaledDeadband(self, x):
        if abs(x) < self.deadbandCutoff:
            return 0
        else:
            return (self._Cubic(x) -
                    (copysign(1, x)) * self.alpha) / (1.0 - self.alpha)

    # The analog joystick output is an unsigned number 0 to 2^16, which we
    # will scale to -1 to +1 for compatibility with the cubic scaled
    # deadband article. This will then remap and return a value
    # still in the range -1 to +1. Finally we multiply by the requested scaler
    # an return an integer which can be used with the mouse HID.
    #
    def readJoystickAxis(self):
        return int(
            self._cubicScaledDeadband((self.pin.value / 2**15) - 1) *
            self.outputScale)
Beispiel #10
0
class Peripherals:
    """Peripherals Helper Class for the MagTag Library"""

    # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
    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 play_tone(self, frequency, duration):
        """Automatically Enable/Disable the speaker and play
        a tone at the specified frequency for the specified duration
        It will attempt to play the sound up to 3 times in the case of
        an error.
        """
        if frequency <= 0:
            raise ValueError("The frequency has to be greater than 0.")
        self._speaker_enable.value = True
        attempt = 0
        # Try up to 3 times to play the sound
        while attempt < 3:
            try:
                simpleio.tone(board.SPEAKER, frequency, duration)
                break
            except NameError:
                pass
            attempt += 1
        self._speaker_enable.value = False

    def deinit(self):
        """Call deinit on all resources to free them"""
        self.neopixels.deinit()
        self._neopixel_disable.deinit()
        self._speaker_enable.deinit()
        for button in self.buttons:
            button.deinit()
        self._batt_monitor.deinit()
        self._light.deinit()

    @property
    def battery(self):
        """Return the voltage of the battery"""
        return (self._batt_monitor.value / 65535.0) * 3.3 * 2

    @property
    def neopixel_disable(self):
        """
        Enable or disable the neopixels for power savings
        """
        return self._neopixel_disable.value

    @neopixel_disable.setter
    def neopixel_disable(self, value):
        self._neopixel_disable.value = value

    @property
    def speaker_disable(self):
        """
        Enable or disable the speaker for power savings
        """
        return not self._speaker_enable.value

    @speaker_disable.setter
    def speaker_disable(self, value):
        self._speaker_enable.value = not value

    @property
    def button_a_pressed(self):
        """
        Return whether Button A is pressed
        """
        return not self.buttons[0].value

    @property
    def button_b_pressed(self):
        """
        Return whether Button B is pressed
        """
        return not self.buttons[1].value

    @property
    def button_c_pressed(self):
        """
        Return whether Button C is pressed
        """
        return not self.buttons[2].value

    @property
    def button_d_pressed(self):
        """
        Return whether Button D is pressed
        """
        return not self.buttons[3].value

    @property
    def any_button_pressed(self):
        """
        Return whether any button is pressed
        """
        return False in [self.buttons[i].value for i in range(0, 4)]

    @property
    def light(self):
        """
        Return the value of the light sensor. The neopixel_disable property
        must be false to get a value.

        .. code-block:: python

            import time
            from adafruit_magtag.magtag import MagTag

            magtag = MagTag()

            while True:
                print(magtag.peripherals.light)
                time.sleep(0.01)

        """
        return self._light.value
Beispiel #11
0
wave_type = 0  # 0 = square wave, 1 = triangle wave
value_frame = 1  # start-of-frame value
value_pixel = 2  # pixel-to-pixel value
inc_frame = 3  # frame-to-frame increment
inc_pixel = 4  # pixel-to-pixel inc

wave_h = 0  # hue
wave_s = 1  # saturation
wave_v = 2  # brightness

# 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()


# generate a non-zero random number for frame and pixel increments
def nz_random():
    random_number = 0

    while random_number <= 0:
        random_number = random.randint(0, 15) - 7

    return random_number


while True:

    w = i = n = s = v = r = g = b = v1 = s1 = 0
    speaker.frequency = 444
    time.sleep(.5)

    # tone 3
    speaker.frequency = 494
    time.sleep(.5)

    speaker.duty_cycle = OFF


######################### MAIN LOOP ##############################

randomSeedPin = AnalogIn(board.A0)
random.seed(randomSeedPin.value)
randomSeedPin.deinit()

# randomize the starting button
activeButtonId = random.randint(0, 4)

i = 0
while True:
    # spin internal neopixel LED around for pretty lightssss
    dot[0] = wheel(i & 255)

    activeButton = buttons[activeButtonId]
    activeButtonLed = buttonLeds[activeButtonId]

    if i < 125:
        activeButtonLed.value = True
    else:
  time.sleep(.5)

  speaker.frequency = 444
  time.sleep(.5)

  # tone 3
  speaker.frequency = 494
  time.sleep(.5)

  speaker.duty_cycle = OFF

######################### MAIN LOOP ##############################

randomSeedPin = AnalogIn(board.A0)
random.seed(randomSeedPin.value)
randomSeedPin.deinit()

# randomize the starting button
activeButtonId = random.randint(0,4)

i = 0
while True:
  # spin internal neopixel LED around for pretty lightssss
  dot[0] = wheel(i & 255)

  activeButton = buttons[activeButtonId]
  activeButtonLed = buttonLeds[activeButtonId]

  if i < 125:
    activeButtonLed.value = True
  else:
class Peripherals:
    """Peripherals Helper Class for the FunHouse Library"""

    # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
    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

    @staticmethod
    def play_tone(frequency, duration):
        """Automatically Enable/Disable the speaker and play
        a tone at the specified frequency for the specified duration
        It will attempt to play the sound up to 3 times in the case of
        an error.
        """
        if frequency <= 0:
            raise ValueError("The frequency has to be greater than 0.")
        attempt = 0
        # Try up to 3 times to play the sound
        while attempt < 3:
            try:
                simpleio.tone(board.SPEAKER, frequency, duration)
                break
            except NameError:
                pass
            attempt += 1

    def set_dotstars(self, *values):
        """Set the dotstar values to the provided values"""
        for i, value in enumerate(values[:len(self.dotstars)]):
            self.dotstars[i] = value

    def deinit(self):
        """Call deinit on all resources to free them"""
        self.dotstars.deinit()
        for button in self._buttons:
            button.deinit()
        for ctp in self._ctp:
            ctp.deinit()
        self._light.deinit()
        self._led.deinit()
        self._pir.deinit()

    @property
    def button_down(self):
        """
        Return whether Down Button is pressed
        """
        return self._buttons[0].value

    @property
    def button_sel(self):
        """
        Return whether Sel Button is pressed
        """
        return self._buttons[1].value

    @property
    def button_up(self):
        """
        Return whether Up Button is pressed
        """
        return self._buttons[2].value

    @property
    def any_button_pressed(self):
        """
        Return whether any button is pressed
        """
        return True in [button.value for button in enumerate(self._buttons)]

    @property
    def captouch6(self):
        """
        Return whether CT6 Touch Pad is touched
        """
        return self._ctp[0].value

    @property
    def captouch7(self):
        """
        Return whether CT7 Touch Pad is touched
        """
        return self._ctp[1].value

    @property
    def captouch8(self):
        """
        Return whether CT8 Touch Pad is touched
        """
        return self._ctp[2].value

    @property
    def slider(self):
        """
        Return the slider position value in the range of 0.0-1.0 or None if not touched
        """
        val = 0
        cap_map = b"\x01\x03\x02\x05\x04\x0c\x08\x18\x10"
        for cap in range(5):
            raw = self._ctp[cap + 3].raw_value
            if raw > 15000:
                val += 1 << (cap)
        for i, pos in enumerate(tuple(cap_map)):
            if val == pos:
                print(i, len(cap_map) - 1)
                return round(i / (len(cap_map) - 1), 1)
        return None

    @property
    def light(self):
        """
        Return the value of the light sensor. The neopixel_disable property
        must be false to get a value.

        .. code-block:: python

            import time
            from adafruit_funhouse import FunHouse

            funhouse = FunHouse()

            while True:
                print(funhouse.peripherals.light)
                time.sleep(0.01)

        """
        return self._light.value

    @property
    def led(self):
        """
        Return or set the value of the LED
        """
        return self._led.value

    @led.setter
    def led(self, value):
        self._led.value = bool(value)

    @property
    def pir_sensor(self):
        """
        Return the value of the PIR Sensor
        """
        return self._pir.value
def is_dry():
    analog_in = AnalogIn(board.A3)
    global last_moisure_value
    last_moisure_value = get_moisture_value(analog_in)
    analog_in.deinit()
    return last_moisure_value <= 400