def __init__(self): self.pixels = neopixel.NeoPixel( board.D2, self.pixel_count, brightness=0.1, auto_write=False, # only write when I say to pixel_order=neopixel.GRB) self.clk = digitalio.DigitalInOut(board.D0) self.clk.switch_to_input(pull=digitalio.Pull.DOWN) self.analog_out = analogio.AnalogOut(board.D1) self.knob0_in = analogio.AnalogIn(board.D3) self.knob1_in = analogio.AnalogIn(board.D4) # button is on knob1 too self.button_time = 0 self.button_press = 0 # debounced state self.button_held = False self.scale_idx = 0 self.notes = [0] * 8 # max 8 note array self.notes_len = 8 #len(self.notes) self.note_idx = 0 self.note_root = 0 self.mode = 0 self.clk_external = external_trigger # false = internal clock self.clk_last_value = False self.tempo = 120 self.tempo_secs = 60.0 / self.tempo self.last_time = 0 self.populate_notes() print("machine is online. clock: {}\n notes:{}".format( "ext" if self.clk_external else "int", self.notes))
def test_dual(pair1, pair2): # verifies that the DACs can be set independently print(f"Running pair test\n") pin1_in = analogio.AnalogIn(pair1[0]) pin1_out = analogio.AnalogOut(pair1[1]) pin2_in = analogio.AnalogIn(pair2[0]) pin2_out = analogio.AnalogOut(pair2[1]) for v in range(0, 65536, 4096): v2 = 65535 - v pin1_out.value = v pin2_out.value = v2 print(f"Pair1: Value {v} read as {pin1_in.value}") print(f"Pair2: Value {v2} read as {pin2_in.value}") pin1_in.deinit() pin1_out.deinit() pin2_in.deinit() pin2_out.deinit()
def test_dac_analog(p_in, p_out): print( f"Running dac analog test with pin {p_in} as input and {p_out} as output\n" ) pin_in = analogio.AnalogIn(p_in) pin_out = analogio.AnalogOut(p_out) for v in range(0, 65536, 4096): pin_out.value = v print(f"Value {v} read as {pin_in.value}") pin_in.deinit() pin_out.deinit()
def test_dac_digital(p_in, p_out): print( f"Running dac digital test with pin {p_in} as input and {p_out} as output" ) pin_in = digitalio.DigitalInOut(p_in) pin_out = analogio.AnalogOut(p_out) for v in range(0, 65536, 4096): pin_out.value = v print(f"Value {v} read as {pin_in.value}") pin_in.deinit() pin_out.deinit()
while True: yield -1 def repeat(value, cnt): for _ in range(cnt): yield value ### A2 is GP28 on Raspberry Pi Pico (RP2040) input_pin = board.A2 if SYSNAME == "rp2040" else board.A5 output_pin = board.A0 if SYSNAME == "samd21" else None OUTPUT_DAC_BITS = 10 input = analogio.AnalogIn(input_pin) output = analogio.AnalogOut(output_pin) if output_pin else None try: _ = time.monotonic_ns() time_fn = time.monotonic_ns time_mul = 1e-9 except (AttributeError, NotImplementedError): time_fn = time.monotonic time_mul = 1 SAMPLE_SIZES = (1, 8, 50) output_step = 1 << (16 - OUTPUT_DAC_BITS) ### The SAMD21 is about 22ms for the loop anyway interval = 0.025
import board import neopixel import math import time import digitalio import analogio led = analogio.AnalogOut(board.A0) #sets the A0 pin as an analog Output holder = 0 #this is the value that the LED is associated with changer = 3000 #This is the value that the LED changes by while True: led.value = holder #Sets the LED to a value time.sleep(0.1) holder += changer #adds brightness (or subtracts) print(holder) #prints to serial monitor if holder >= 60000: changer *= -1 #if the led is at the threshold, it makes the changer negative if holder <= 2000: changer *= -1
# Lissajous version 2 import array, math import board, analogio import time length = 1000 samples_x = array.array("H", [0] * length) samples_y = array.array("H", [0] * length) for idx in range(length): samples_x[idx] = round( math.sin(math.pi * 2 * idx / length) * 10000 + 10000) samples_y[idx] = round( math.sin(math.pi * 2 * 3 * idx / length + math.pi / 2) * 10000 + 10000) dac_a0 = analogio.AnalogOut(board.A0) dac_a1 = analogio.AnalogOut(board.A1) while True: for x, y in zip(samples_x, samples_y): dac_a0.value = x dac_a1.value = y #time.sleep(.001)
# Trinket Trigger 1.0 # todbot import time import board import digitalio import touchio import analogio import adafruit_dotstar as dotstar # On-board DotStar for boards including Gemma, Trinket, and ItsyBitsy dots = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.5) touchA = touchio.TouchIn(board.D3) touchB = touchio.TouchIn(board.D4) dac = analogio.AnalogOut(board.D1) gate = digitalio.DigitalInOut(board.D2) gate.direction = digitalio.Direction.OUTPUT gate.value = False t0 = time.monotonic() # set start time def map_range(x, in_min, in_max, out_min, out_max): in_range = in_max - in_min in_delta = x - in_min if in_range != 0: mapped = in_delta / in_range elif in_delta != 0: mapped = in_delta else:
import time import board import random import analogio import neopixel import digitalio import adafruit_fancyled.adafruit_fancyled as fancy import button console = neopixel.NeoPixel(board.D0, 3, brightness=0.2, auto_write=False) omnitool = neopixel.NeoPixel(board.D2, 1, brightness=0.3, auto_write=False) button = button.Button(board.D4, digitalio.Pull.UP) analog_out = analogio.AnalogOut(board.A0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) ORANGE = (255, 50, 0) palette = [fancy.CRGB(*ORANGE), fancy.CRGB(*RED), fancy.CRGB(*BLUE)] def buzz_till_button(): """ Buzz until until a button press. """
def make_analog_pin(pin, direction): if direction is digitalio.Direction.INPUT: p = analogio.AnalogIn(pin) else: p = analogio.AnalogOut(pin) return p
def from_pin(cls, pin): return cls(analogio.AnalogOut(pin))
from adafruit_midi.note_off import NoteOff #from adafruit_midi.control_change import ControlChange #from adafruit_midi.pitch_bend import PitchBend # Turn the speaker on speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) speaker_enable.direction = digitalio.Direction.OUTPUT speaker_on = True speaker_enable.value = speaker_on # Approach is to write to the DAC directly rather than play # samples as the audioio library doesn't work well for # various reasons with looping samples # NEED to try a long sample (2 seconds?) which terminates early # on note off dac_out = analogio.AnalogOut(board.SPEAKER) # same as board.A0 for CPX midi_channel = 10 midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=midi_channel - 1) # Check how long a loop takes to assign to DAC # about 2 seconds for 3k on a CPX with 4.0.0 beta 7 timed_run_length = 3000 start_t = time.monotonic() for rep in range(timed_run_length): dac_out.value = random.randint(32767, 32769) dac_out.value = random.randint(32767, 32769) dac_out.value = random.randint(32767, 32769) dac_out.value = random.randint(32767, 32769)
# Owen McKenney # LED fade # This makes and LED fade from off to on import board import neopixel import digitalio import analogio import time import simpleio led = analogio.AnalogOut(board.A0) # LED setup while True: for i in range(31000, 42000, 5): # for loop to go between 31k and 42k by 5 led.value = i # sets LED brightness to i print(i) # prints value in serial monitor time.sleep(.1) for i in range(42000, 31000, -5): # for loop to go between 42k and 31k by -5 led.value = i # sets LED brightness to i print(i) # prints value in serial monitor time.sleep(.1)
mult_y = dac_y_max / range_y ### https://github.com/adafruit/circuitpython/issues/1992 print("length of rawdata", len(rawdata)) use_wav = True poor_wav_bug_workaround = False leave_wav_looping = True ### A0 will be x, A1 will be y if use_wav: print("Using audiocore.RawSample for DACs") dacs = audioio.AudioOut(board.A0, right_channel=board.A1) else: print("Using analogio.AnalogOut for DACs") a0 = analogio.AnalogOut(board.A0) a1 = analogio.AnalogOut(board.A1) ### 10Hz is about ok for AudioOut, optimistic for AnalogOut frame_t = 1 / 10 prev_t = time.monotonic() angle = 0 ### in radians frame = 1 while True: ##print("Transforming data for frame:", frame, "at", prev_t) ### Rotate the points of the vector graphic around its centre idx = 0 sine = math.sin(angle) cosine = math.cos(angle) for px, py in display_data:
# Lissajous Real Time import math import board,analogio lChan = analogio.AnalogOut(board.A0) rChan = analogio.AnalogOut(board.A1) length = 1000 while True: for idx in range(length): left = round(math.sin(math.pi * 2 * idx / length) * 10000 + 10000) right = round(math.sin(math.pi * 2 * 3 * idx / length + math.pi / 2) * 10000 + 10000) lChan.value = left rChan.value = right pass
import analogio import board import time led = analogio.AnalogOut(board.A0) B = 65000 amount = 1000 ceiling = 50000 floor = 20000 #pin.deinit() #led.direction = analogio.Direction.OUTPUT while True: led.value = B B -= amount time.sleep(.01) if B < floor: amount = -1000 elif B > ceiling: amount = 1000 time.sleep(.01)
import board import analogio import digitalio # Pins GREEN_LED_PIN = board.D3 PWM_PIN = board.D9 RED_LED_PIN = board.D13 DAC_PIN = board.A0 # Leds redLed = digitalio.DigitalInOut(RED_LED_PIN) greenLed = digitalio.DigitalInOut(GREEN_LED_PIN) # AnalogOut dac = analogio.AnalogOut(DAC_PIN)