From 7f9ad7e00d4bb2effd431651eed1fb7abd02fb01 Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Fri, 23 May 2025 21:36:18 -0700 Subject: [PATCH] basic implementation of analog inputs and test script --- analog_input.h | 55 +++++++++++++++ .../calibrate_analog/calibrate_analog.ino | 70 +++++++++++++++++++ gravity.cpp | 5 ++ gravity.h | 3 + 4 files changed, 133 insertions(+) create mode 100644 analog_input.h create mode 100644 examples/calibrate_analog/calibrate_analog.ino diff --git a/analog_input.h b/analog_input.h new file mode 100644 index 0000000..d05cefe --- /dev/null +++ b/analog_input.h @@ -0,0 +1,55 @@ +/** + * @file analog_input.h + * @author Adam Wonak (https://github.com/awonak) + * @brief Class for interacting with analog inputs. + * @version 0.1 + * @date 2025-05-23 + * + * @copyright Copyright (c) 2025 + * + */ +#ifndef ANALOG_INPUT_H +#define ANALOG_INPUT_H + +const int MAX_INPUT = (1 << 10) - 1; // Max 10 bit analog read resolution. +const int CALIBRATION_OFFSET = 15; + +class AnalogInput { + public: + AnalogInput() {} + ~AnalogInput() {} + + /** + * @brief Initializes a analog input object. + * + * @param pin gpio pin for the analog input. + */ + void Init(uint8_t pin) { + pinMode(pin, INPUT); + pin_ = pin; + } + + /** + * @brief Read the value of the analog input. + * + */ + void Process() { + old_read_ = read_; + int raw = analogRead(pin_); + read_ = map(raw, CALIBRATION_OFFSET, MAX_INPUT, 0, MAX_INPUT); + } + + /** + * @brief Get the current value of the analog input. + * + * @return InputState + */ + inline uint16_t Read() { return read_; } + + private: + uint8_t pin_; + uint16_t read_; + uint16_t old_read_; +}; + +#endif diff --git a/examples/calibrate_analog/calibrate_analog.ino b/examples/calibrate_analog/calibrate_analog.ino new file mode 100644 index 0000000..148c152 --- /dev/null +++ b/examples/calibrate_analog/calibrate_analog.ino @@ -0,0 +1,70 @@ +#include "gravity.h" + +byte idx = 0; +bool reversed = false; +bool freeze = false; +byte selected_param = 0; + +// Initialize the gravity library and attach your handlers in the setup method. +void setup() { + // Initialize Gravity. + gravity.Init(); + + // Attach handlers. + gravity.clock.AttachIntHandler(IntClock); +} + +// The loop method must always call `gravity.Process()` to read any peripherial state changes. +void loop() { + gravity.Process(); + + UpdateDisplay(); +} + +// The rest of the code is your apps logic! + +void IntClock(uint32_t tick) { + if (tick % 12 == 0 && ! freeze) { + gravity.outputs[idx].Low(); + if (reversed) { + idx = (idx == 0) ? OUTPUT_COUNT - 1 : idx - 1; + } else { + idx = (idx + 1) % OUTPUT_COUNT; + } + gravity.outputs[idx].High(); + } +} + + +void UpdateDisplay() { + gravity.display.clearDisplay(); + + int cv1 = gravity.cv1.Read(); + int cv2 = gravity.cv2.Read(); + + gravity.display.setCursor(10, 10); + gravity.display.print(F("CV1: ")); + gravity.display.print(cv1); + + gravity.display.drawRect(10, 22, 100, 10, 1); + if (cv1 >= 512) { + int x = (float(cv1 - 512) / 512.0) * 50; + gravity.display.fillRect(60, 22, x, 10, 1); + } else { + int x = (float(512 - cv1) / 512.0) * 50; + gravity.display.fillRect(60-x, 22, x, 10, 1); + } + + gravity.display.setCursor(10, 42); + gravity.display.print(F("CV2: ")); + gravity.display.print(cv2); + if (cv2 >= 512) { + int x = (float(cv2 - 512) / 512.0) * 50; + gravity.display.fillRect(60, 42, x, 10, 1); + } else { + int x = (float(512 - cv2) / 512.0) * 50; + gravity.display.fillRect(60-x, 42, x, 10, 1); + } + + gravity.display.display(); +} \ No newline at end of file diff --git a/gravity.cpp b/gravity.cpp index 7a0df8a..2cfbb8b 100644 --- a/gravity.cpp +++ b/gravity.cpp @@ -26,6 +26,9 @@ void Gravity::InitInputs() { shift_button.Init(SHIFT_BTN_PIN); play_button.Init(PLAY_BTN_PIN); + cv1.Init(CV1_PIN); + cv2.Init(CV2_PIN); + // Pin Change Interrupts for Encoder. // Thanks to https://dronebotworkshop.com/interrupts/ @@ -62,6 +65,8 @@ void Gravity::Process() { shift_button.Process(); play_button.Process(); encoder.Process(); + cv1.Process(); + cv2.Process(); // Update Output states. for (int i; i < OUTPUT_COUNT; i++) { diff --git a/gravity.h b/gravity.h index 70f67d4..698ebcb 100644 --- a/gravity.h +++ b/gravity.h @@ -5,6 +5,7 @@ #include #include +#include "analog_input.h" #include "button.h" #include "clock.h" #include "digital_output.h" @@ -33,6 +34,8 @@ class Gravity { EncoderDir encoder; // Rotary encoder with button instance Button shift_button; Button play_button; + AnalogInput cv1; + AnalogInput cv2; private: void InitClock();