basic implementation of analog inputs and test script

This commit is contained in:
2025-05-23 21:36:18 -07:00
parent 9451f987b5
commit 7f9ad7e00d
4 changed files with 133 additions and 0 deletions

55
analog_input.h Normal file
View File

@ -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

View File

@ -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();
}

View File

@ -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++) {

View File

@ -5,6 +5,7 @@
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#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();