This repository has been archived on 2023-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
old-monorepo/modules/console/Firmware/src/main.cpp

93 lines
2 KiB
C++
Raw Normal View History

2020-12-27 16:55:38 +01:00
#include <Arduino.h>
2020-12-27 19:18:30 +01:00
#include <Wire.h>
2021-03-27 16:50:07 +01:00
//General
2020-12-27 19:18:30 +01:00
const int bjtCount = 4;
const int btnPin[bjtCount] = {7, 8, 9, 10};
byte btnState[bjtCount];
2021-03-27 16:50:07 +01:00
byte lastBtnState[bjtCount] = {HIGH, HIGH, HIGH, HIGH};
unsigned long lastDebounceTime[bjtCount] = {0, 0, 0, 0};
unsigned long debounceDelay = 50;
2020-12-27 16:55:38 +01:00
2021-06-28 17:59:00 +02:00
//I2C
const int SLAVE_ADDR = 9;
//Rotary Encoder
const int encClkPin[1] = {6};
const int encDtPin[1] = {5};
2020-12-27 16:55:38 +01:00
void setup()
2020-12-27 19:18:30 +01:00
{
Serial.begin(9600);
Wire.begin();
for (int i = 0; i < bjtCount; i++)
{
pinMode(btnPin[i], INPUT_PULLUP);
2021-06-28 17:59:00 +02:00
pinMode(encClkPin[i], INPUT_PULLUP);
pinMode(encDtPin[i], INPUT_PULLUP);
2020-12-27 19:18:30 +01:00
}
}
2021-03-27 16:50:07 +01:00
void transmit(String data)
2020-12-27 19:18:30 +01:00
{
Wire.beginTransmission(SLAVE_ADDR);
2021-03-27 16:50:07 +01:00
Wire.write(data.c_str(), 2);
Serial.println(data.c_str());
2020-12-27 19:18:30 +01:00
Wire.endTransmission();
}
void loop()
{
2021-03-27 16:50:07 +01:00
//Buttons
byte btnReading[bjtCount];
for (int i = 0; i < bjtCount; i++)
{
btnReading[i] = digitalRead(btnPin[i]);
if (btnReading[i] != lastBtnState[i])
{
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay)
{
if (btnReading[i] != btnState[i])
{
btnState[i] = btnReading[i];
if (btnState[i] == LOW)
{
transmit(String(i) + "t");
}
}
}
lastBtnState[i] = btnReading[i];
}
//Rotary Encoder
2021-06-28 17:59:00 +02:00
static uint16_t state[1] = {0};
2020-12-27 19:18:30 +01:00
2021-06-28 17:59:00 +02:00
//delayMicroseconds(100); // Simulate doing somehing else as well.
2020-12-27 19:18:30 +01:00
2021-06-28 17:59:00 +02:00
for (int i = 0; i < 1; i++)
2021-01-01 23:10:04 +01:00
{
2021-06-28 17:59:00 +02:00
state[i] = (state[i] << 1) | digitalRead(encClkPin[i]) | 0xe000;
if (state[i] == 0xf000)
2021-01-01 23:10:04 +01:00
{
2021-06-28 17:59:00 +02:00
state[i] = 0x0000;
if (digitalRead(encDtPin[i]))
//transmit(String(i) + "i");
2021-01-01 23:10:04 +01:00
transmit("2i");
2021-06-28 17:59:00 +02:00
else
//transmit(String(i) + "d");
2021-01-01 23:10:04 +01:00
transmit("2d");
}
}
2020-12-27 19:18:30 +01:00
}