Initial commit of mostly working library.

This commit is contained in:
2025-05-04 10:43:15 -07:00
parent fe9ef561fe
commit 32a23b7bf9
9 changed files with 623 additions and 1 deletions

97
gravity.cpp Normal file
View File

@ -0,0 +1,97 @@
/**
* @file gravity.cpp
* @author Adam Wonak (https://github.com/awonak)
* @brief Library for building custom scripts for the Sitka Instruments Gravity module.
* @version 0.1
* @date 2025-04-19
*
* @copyright Copyright (c) 2025
*
*/
#include "gravity.h"
void Gravity::Init() {
InitClock();
InitInputs();
InitOutputs();
InitDisplay();
}
void Gravity::InitClock() {
clock.Init();
}
void Gravity::InitInputs() {
shift_button.Init(SHIFT_BTN_PIN);
play_button.Init(PLAY_BTN_PIN);
// Pin Change Interrupts for Encoder.
// Thanks to https://dronebotworkshop.com/interrupts/
// Enable both PCIE2 Bit3 (Port D), and PCIE1 Bit2 (Port C).
PCICR |= B00000110;
// Select PCINT23 Bit4 = 1 (Pin D4)
PCMSK2 |= B00010000;
// Select PCINT11 Bit3 (Pin D17/A3)
PCMSK1 |= B00001000;
}
void Gravity::InitOutputs() {
// Initialize each of the outputs with it's GPIO pins and probability.
outputs[0].Init(OUT_CH1);
outputs[1].Init(OUT_CH2);
outputs[2].Init(OUT_CH3);
outputs[3].Init(OUT_CH4);
outputs[4].Init(OUT_CH5);
outputs[5].Init(OUT_CH6);
}
void Gravity::InitDisplay() {
// OLED Display configuration.
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS);
delay(1000);
display.setRotation(2); // rotates text on OLED 1=90 degrees, 2=180 degrees
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.display();
}
void Gravity::Process() {
// Read peripherials for changes.
shift_button.Process();
play_button.Process();
encoder.Process();
// Call button state change handlers.
if (play_button.Change() == Button::CHANGE_PRESSED) {
play_button.OnPress();
}
if (shift_button.Change() == Button::CHANGE_PRESSED) {
shift_button.OnPress();
}
// Update Output states.
for (int i; i < OUTPUT_COUNT; i++) {
outputs[i].Process();
}
}
void ReadEncoder() {
gravity.encoder.UpdateEncoder();
}
// Define Encoder pin ISR.
// Pin Change Interrupt on Port C (D17/A3).
ISR(PCINT2_vect) {
ReadEncoder();
};
// Pin Change Interrupt on Port D (D4).
ISR(PCINT1_vect) {
ReadEncoder();
};
// Singleton
Gravity gravity;