Compare commits

...

2 Commits

Author SHA1 Message Date
5cea555e77 i2c communication
use optic rotary encoder
2025-05-11 16:01:57 +02:00
311c9ecdbb counter not needed, just the events 2025-01-03 09:51:49 +01:00
2 changed files with 137 additions and 123 deletions

62
master/master.ino Normal file
View File

@ -0,0 +1,62 @@
#include <Wire.h>
#define READ_BYTES_FROM_WIRE 1
// needs global define
#define DATA_STOP_BYTE 0x00
#define HEADING_TRIGGER_BIT 1
#define HEADING_HIGH_BIT 2
#define ALTITUDE_TRIGGER_BIT 4
#define ALTITUDE_HIGH_BIT 8
struct device {
byte address;
String name;
};
struct device devices[] = {
{0x01, "althead"}
};
int numDevices = -1;
void setup() {
numDevices = sizeof(devices) / sizeof(device);
Wire.begin();
pinMode(LED_TX, OUTPUT);
digitalWrite(LED_TX, HIGH);
pinMode(LED_RX, OUTPUT);
digitalWrite(LED_RX, HIGH);
}
void loop() {
// loop through devices and ask for data
for (int i = 0; i < numDevices; i++) {
Wire.requestFrom(devices[i].address, READ_BYTES_FROM_WIRE);
while (Wire.available()) {
// device has data, ask as long for data as it sends
// if it never stops sending, we are fucked :)
byte data = Wire.read();
if (data != DATA_STOP_BYTE) {
if (data & ALTITUDE_TRIGGER_BIT) {
if (data & ALTITUDE_HIGH_BIT) {
Serial.println("Altitude up");
} else {
Serial.println("Altitude down");
}
} else if (data & HEADING_TRIGGER_BIT) {
if (data & HEADING_HIGH_BIT) {
Serial.println("Heading right");
} else {
Serial.println("Heading left");
}
} else {
// who are you?
}
Wire.requestFrom(devices[i].address, READ_BYTES_FROM_WIRE);
}
}
}
}

View File

@ -1,160 +1,112 @@
#include <Wire.h>
#define PIN_HEADING_CLK D7
#define PIN_HEADING_DT D5
#define PIN_HEADING_SW D6
#define PIN_HEADING_WHITE PD3
#define PIN_HEADING_RED PD6
#define PIN_ALTITUDE_CLK D1
#define PIN_ALTITUDE_DT D2
#define PIN_ALTITUDE_SW D3
#define PIN_LED D4
#define PIN_ALTITUDE_WHITE PD2
#define PIN_ALTITUDE_RED PD5
#define VALUE_BUFFER 30
#define HEADING_DT_TRIGGER 1
#define HEADING_DT_HIGH 2
#define HEADING_SW_TRIGGER 4
#define SKIP_ROTARY_INPUTS 50
#define ALTITUDE_DT_TRIGGER 8
#define ALTITUDE_DT_HIGH 16
#define ALTITUDE_SW_TRIGGER 32
// needs global define
#define DATA_STOP_BYTE 0x00
#define HEADING_TRIGGER_BIT 1
#define HEADING_HIGH_BIT 2
#define ALTITUDE_TRIGGER_BIT 4
#define ALTITUDE_HIGH_BIT 8
#define MIN_DT_INPUT_DELAY 1000 // 1ms
#define MIN_SW_INPUT_DELAY 500000 // 500ms
#define MIN_ALTITUDE 1
#define MAX_ALTITUDE 60000
#define MIN_HEADING 1
#define MAX_HEADING 360
uint8_t lastClk = HIGH;
uint16_t heading = 360;
uint16_t altitude = 0;
int lastClk = HIGH;
// ringbuffer of trigger and direction values
uint8_t valueBuffer[VALUE_BUFFER] = { 0 };
byte valueBuffer[VALUE_BUFFER] = { 0 };
uint8_t readerPos, writerPos = 0;
long lastInput = 0;
void addValue(uint8_t value) {
valueBuffer[writerPos++] = value;
if (writerPos >= VALUE_BUFFER) {
writerPos = 0;
}
}
// prevent bouncing input
bool useInput(long minInputDelay) {
long now = micros();
long diff = now - lastInput;
void falling(uint8_t pin, byte triggerBit, byte highBit) {
uint8_t dt = digitalRead(pin);
byte value = triggerBit;
// < 0 because of long overruns
bool ret = diff < 0 || diff > minInputDelay;
if (ret) {
lastInput = now;
// read direction of pin
if (dt) {
value |= highBit;
} else {
// value is already "lowBit"
}
return ret;
}
void addValue(uint8_t value) {
valueBuffer[writerPos++] = value;
if (writerPos >= VALUE_BUFFER) {
writerPos = 0;
}
}
void clkFalling(uint8_t PIN_DT, uint8_t DT_TRIGGER, uint8_t DT_HIGH) {
if (useInput(MIN_DT_INPUT_DELAY)) {
uint8_t dt = digitalRead(PIN_DT);
uint8_t value = DT_TRIGGER;
if (dt) {
value |= DT_HIGH;
} else {
// value is already "DT_LOW"
}
if (useInput(value)) {
addValue(value);
}
}
void swFalling(uint8_t SW_TRIGGER) {
if (useInput(MIN_SW_INPUT_DELAY)) {
addValue(SW_TRIGGER);
}
void headingFalling() {
falling(PIN_HEADING_RED, HEADING_TRIGGER_BIT, HEADING_HIGH_BIT);
}
void ICACHE_RAM_ATTR headingClkFalling();
void headingClkFalling() {
clkFalling(PIN_HEADING_DT, HEADING_DT_TRIGGER, HEADING_DT_HIGH);
void altitudeFalling() {
falling(PIN_ALTITUDE_RED, ALTITUDE_TRIGGER_BIT, ALTITUDE_HIGH_BIT);
}
void ICACHE_RAM_ATTR altitudeClkFalling();
void altitudeClkFalling() {
clkFalling(PIN_ALTITUDE_DT, ALTITUDE_DT_TRIGGER, ALTITUDE_DT_HIGH);
}
void ICACHE_RAM_ATTR headingSwFalling();
void headingSwFalling() {
swFalling(HEADING_SW_TRIGGER);
}
void ICACHE_RAM_ATTR altitudeSwFalling();
void altitudeSwFalling() {
swFalling(ALTITUDE_SW_TRIGGER);
}
void dtTriggered(uint16_t &cnt, uint8_t high, uint8_t fast, uint16_t miV, uint16_t miVR, uint16_t maV, uint16_t mavR) {
if (high) {
cnt++;
int eventCount = 0;
byte lastValue = 0;
bool useInput(byte value) {
if (lastValue == value) {
// same event as last event
// check if already SKIP_ROTARY_INPUTS happend
if (eventCount > SKIP_ROTARY_INPUTS) {
eventCount = 0;
return true;
} else {
eventCount++;
return false;
}
} else {
cnt--;
}
if (cnt > maV) {
cnt = mavR;
}
if (cnt < miV) {
cnt = miVR;
// not same event as last event
// reset counter
lastValue = value;
eventCount = 0;
return false;
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(PIN_HEADING_SW, INPUT_PULLUP);
pinMode(PIN_HEADING_DT, INPUT_PULLUP);
pinMode(PIN_HEADING_CLK, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_HEADING_CLK), headingClkFalling, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_HEADING_SW), headingSwFalling, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_ALTITUDE_CLK), altitudeClkFalling, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_ALTITUDE_SW), altitudeSwFalling, FALLING);
}
void loop() {
while (writerPos != readerPos) {
uint8_t value = valueBuffer[readerPos++];
void i2cRequest() {
// if write is ahead, send data
if (writerPos != readerPos) {
byte value = valueBuffer[readerPos++];
if (readerPos >= VALUE_BUFFER) {
readerPos = 0;
}
if (value & HEADING_DT_TRIGGER) {
uint8_t dtHigh = value & HEADING_DT_HIGH;
dtTriggered(heading, dtHigh, 5, MIN_HEADING, MAX_HEADING, MAX_HEADING, MIN_HEADING);
Serial.print("Heading: ");
Serial.println(heading);
} else if (value & HEADING_SW_TRIGGER) {
Serial.println("Heading: pushed");
} else if (value & ALTITUDE_DT_TRIGGER) {
uint8_t dtHigh = value & ALTITUDE_DT_HIGH;
dtTriggered(altitude, dtHigh, 5, MIN_ALTITUDE, MIN_ALTITUDE, MAX_ALTITUDE, MAX_ALTITUDE);
Serial.print("Altitude: ");
Serial.println(altitude);
} else if (value & ALTITUDE_SW_TRIGGER) {
Serial.println("Altitude: pushed");
} else {
// never happen error...
}
Wire.write(value);
} else {
Wire.write(DATA_STOP_BYTE);
}
}
void setup() {
Serial.begin(115200);
pinMode(PIN_HEADING_WHITE, INPUT_PULLUP);
pinMode(PIN_HEADING_RED, INPUT_PULLUP);
pinMode(PIN_ALTITUDE_WHITE, INPUT_PULLUP);
pinMode(PIN_ALTITUDE_RED, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_ALTITUDE_WHITE), altitudeFalling, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_HEADING_WHITE), headingFalling, FALLING);
Wire.begin(0x01);
Wire.onRequest(i2cRequest);
}
void loop() {
delay(10000);
}