From: brentru Date: Mon, 20 May 2024 19:22:19 +0000 (-0400) Subject: add square wave generation X-Git-Tag: 8.39.2^2~14 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/a925dfa21f5a3483efe8f648c1db9cd04d98e949 add square wave generation --- diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py index 9ad9905..fbe74ec 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py @@ -4,7 +4,7 @@ """generic_agnostic_board pin interface""" import random -# Values for sine wave output +# Values for sine wave analog output # (data points = 20, amplitude=100, frequency=1) sine_wave = [ 0, @@ -29,6 +29,30 @@ sine_wave = [ -31, ] +# Values for a square wave analog output +# (data points = 20, amplitude=100) +square_wave_int = [ + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100, + 100, + -100 +] class Pin: """A basic Pin class for use with generic_agnostic_board""" @@ -72,11 +96,17 @@ class Pin: if self._wave_idx is None: self._wave_idx = 0 else: - self._wave_idx += 1 - if self._wave_idx >= len(sine_wave): - self._wave_idx = 0 + self._wave_idx = (self._wave_idx + 1) % len(sine_wave) return sine_wave[self._wave_idx] + def return_square_wave(self): + """Returns the next value in the square wave""" + if self._wave_idx is None: + self._wave_idx = 0 + else: + self._wave_idx = (self._wave_idx + 1) % len(square_wave_int) + return square_wave_int[self._wave_idx] + def __init__(self, pin_id=None): self.id = pin_id self._mode = None @@ -92,13 +122,11 @@ class Pin: 2: self.return_true, # Dx_INPUT_TRUE_PULL_UP 3: self.return_true, # Dx_INPUT_TRUE_PULL_DOWN 4: self.return_true, # Dx_OUTPUT - 6: self.return_true, # NEOPIXEL 7: self.return_random_int, # Ax_INPUT_RAND_INT 8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI 9: self.return_sine_wave, # Ax_OUTPUT_WAVE_SINE - 10: self.return_true, # Ax_OUTPUT_WAVE_SAWTOOTH - 11: self.return_toggle, # Dx_INPUT_TOGGLE - # Add other mappings as needed + 10: self.return_square_wave, # Ax_OUTPUT_WAVE_SAWTOOTH + 11: self.return_toggle # Dx_INPUT_TOGGLE } def init(self, mode=IN, pull=None):