send rref data to salve and display

This commit is contained in:
2025-05-17 19:57:15 +02:00
parent d8fe68679a
commit 107297f162
3 changed files with 124 additions and 73 deletions

View File

@ -1,6 +1,7 @@
#define DATA_STOP_BYTE 0x00
#define DATA_INIT_BYTE_1 0x01
#define DATA_INIT_BYTE_2 0x02
#define DATA_BYTE 0x03
#define DATA_RESET_BYTE 0xFF
#define TRIGGER_BIT_1 1
#define HIGH_BIT_1 2

View File

@ -15,26 +15,52 @@
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
#define MQTT_SIM_LOG_TOPIC "/xplane/meta/log"
#define MQTT_LOG_TOPIC "/xplane/meta/log"
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
#define MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX "/xplane/rref/"
#define MQTT_SIM_VALUE_SUBSCRIBE_TOPIC "/xplane/rref"
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
struct device {
byte address; // I2C address of device
char *name; // name of this device
char *name; // name of this device
char name1[3]; // first name to send on init of device
char *highCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is set
char *lowCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is not set
int rrefRefresh1; // refresh rate of subscribing rref
char *rref1; // rref to subscribe to
char name2[3]; // second name to send on init of device
char *highCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is set
char *lowCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is not set
int rrefRefresh2; // refresh rate of subscribing rref
char *rref2; // rref to subscribe to
};
struct device devices[] = {
{ 0x08, "Heading Speed", {'H', 'D', 'G'}, HEADING_SIM_UP, HEADING_SIM_DOWN, {'S', 'P', 'D'}, AIRSPEED_SIM_UP, AIRSPEED_SIM_DOWN }
{
0x08,
"Heading Speed",
{'H', 'D', 'G'},
HEADING_SIM_UP,
HEADING_SIM_DOWN,
200,
"sim/cockpit/autopilot/heading_mag",
{'S', 'P', 'D'},
AIRSPEED_SIM_UP,
AIRSPEED_SIM_DOWN,
200,
"sim/cockpit/autopilot/airspeed"
}
};
int numDevices = -1; // gets calculated in setup()
const int numDevices = sizeof(devices) / sizeof(device);
struct rrefSubscription {
byte address;
byte triggerBit;
};
struct rrefSubscription rrefSubscriptions[numDevices * 2];
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
@ -42,20 +68,21 @@ EthernetClient client;
MqttClient mqttClient(client);
void onMqttMessage(int messageSize) {
// // we received a message, print out the topic and contents
// Serial.println("Received a message with topic '");
// Serial.print(mqttClient.messageTopic());
// Serial.print("', length ");
// Serial.print(messageSize);
// Serial.println(" bytes:");
//
// // use the Stream interface to print the contents
// while (mqttClient.available()) {
// Serial.print((char)mqttClient.read());
// }
// Serial.println();
//
// Serial.println();
// assume, that we always receive topic beginning with MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX
// otherwise, this will _HORRIBLY_ fail
char *rrefNoStr = &mqttClient.messageTopic()[13];
int rrefNo = atoi(rrefNoStr);
struct rrefSubscription rref = rrefSubscriptions[rrefNo - 1];
Wire.beginTransmission(rref.address);
Wire.write(DATA_BYTE);
Wire.write(rref.triggerBit);
Wire.write(messageSize);
while (mqttClient.available()) {
Wire.write((char)mqttClient.read());
}
Wire.endTransmission(rref.address);
}
void sendMqttMessage(char *topic, int n, ...) {
@ -70,7 +97,7 @@ void sendMqttMessage(char *topic, int n, ...) {
}
void sendI2CInit(struct device d) {
sendMqttMessage(MQTT_SIM_LOG_TOPIC, 2, "Sending init to ", d.name);
sendMqttMessage(MQTT_LOG_TOPIC, 2, "Sending init to ", d.name);
Wire.beginTransmission(d.address);
Wire.write(DATA_RESET_BYTE);
Wire.write(DATA_INIT_BYTE_1);
@ -80,10 +107,28 @@ void sendI2CInit(struct device d) {
Wire.endTransmission(d.address);
}
void setup() {
// count devices for loop
numDevices = sizeof(devices) / sizeof(device);
void subscribeRrefs(struct device *d, int deviceNo) {
char subscribeMsg[256];
// subscribe no 1
sprintf(subscribeMsg, "%03d %03d %s", deviceNo + 1, d->rrefRefresh1, d->rref1); // subscription no is index-1 based
sendMqttMessage(MQTT_SIM_VALUE_SUBSCRIBE_TOPIC, 1, subscribeMsg);
rrefSubscriptions[deviceNo] = { d->address, TRIGGER_BIT_1 };
// subscribe no 2
sprintf(subscribeMsg, "%03d %03d %s", deviceNo + 2, d->rrefRefresh2, d->rref2); // subscription no is index-1 based
sendMqttMessage(MQTT_SIM_VALUE_SUBSCRIBE_TOPIC, 1, subscribeMsg);
rrefSubscriptions[deviceNo + 1] = { d->address, TRIGGER_BIT_2 };
}
void initializeDevices() {
for (int i = 0; i < numDevices; i++) {
sendI2CInit(devices[i]);
subscribeRrefs(&devices[i], i);
}
}
void setup() {
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
@ -106,7 +151,7 @@ void setup() {
while (1);
} else {
Serial.println("MQTT connected");
sendMqttMessage(MQTT_SIM_LOG_TOPIC, 1, "Master online");
sendMqttMessage(MQTT_LOG_TOPIC, 2, "Master online ", localIP);
}
// subscribe to MQTT topic
@ -117,16 +162,7 @@ void setup() {
// start I2C
Wire.begin();
// for debugging
pinMode(LED_TX, OUTPUT);
digitalWrite(LED_TX, HIGH);
pinMode(LED_RX, OUTPUT);
digitalWrite(LED_RX, HIGH);
// initialize devices
for (int i = 0; i < numDevices; i++) {
sendI2CInit(devices[i]);
}
initializeDevices();
}
void loop() {
@ -142,7 +178,7 @@ void loop() {
continue;
} else if (data == DATA_RESET_BYTE) {
// device needs initialization
sendMqttMessage(MQTT_SIM_LOG_TOPIC, 2, "got reset byte from ", devices[i].name);
sendMqttMessage(MQTT_LOG_TOPIC, 2, "got reset byte from ", devices[i].name);
sendI2CInit(devices[i]);
} else {
// handle as payload

View File

@ -20,7 +20,7 @@
#define VALUE_BUFFER 30
#define SKIP_ROTARY_INPUTS 50
#define SKIP_ROTARY_INPUTS 40
#define INTERNAL_STATE_UNKNOWN 0
#define INTERNAL_STATE_INITIALIZED 1
@ -160,6 +160,26 @@ void i2cReceive(int bytes) {
for (int i = 0; i < 3 && Wire.available(); i++) {
name2[i] = Wire.read();
}
} else if (data == DATA_BYTE) {
// next byte will be trigger bit
byte triggerBit = Wire.read();
// next byte will be length of data
byte dataLength = Wire.read();
// next byte(s) will be the data itself
char dataBytes[dataLength];
for (int i = 0; i < dataLength && Wire.available(); i++) {
dataBytes[i] = Wire.read();
}
if (triggerBit & TRIGGER_BIT_1) {
updateDisplay1(dataBytes, dataLength);
} else if (triggerBit & TRIGGER_BIT_2) {
updateDisplay2(dataBytes, dataLength);
} else {
Serial.println("Unknown trigger bit");
}
} else {
// not yet implemented?
Serial.println("Received unknown");
@ -177,6 +197,35 @@ void displayTestScreen(Adafruit_SSD1306 display) {
display.display();
}
void updateDisplay1(char *data, byte len) {
updateDisplay(&display1, name1, data, len);
}
void updateDisplay2(char *data, byte len) {
updateDisplay(&display2, name2, data, len);
}
void updateDisplay(Adafruit_SSD1306 *display, char *name, char *data, byte len) {
display->clearDisplay();
display->setTextColor(SSD1306_WHITE); // Draw white text
display->cp437(true); // Use full 256 char 'Code Page 437' font
display->setTextSize(1); // Normal 1:1 pixel scale
display->setCursor(0, 8); // Start at top-left corner
display->setFont(NULL);
display->write(name[0]);
display->write(name[1]);
display->write(name[2]);
display->setTextSize(2); // Normal 1:1 pixel scale
display->setCursor(0, 56); // Start at top-left corner
display->setFont(&FreeMonoBold18pt7b);
for (byte i = 0; i < len; i++) {
display->write(data[i]);
}
display->display();
}
uint8_t address = I2C_ADDRESS_START;
void setup() {
// https://support.arduino.cc/hc/en-us/articles/4839084114460-If-your-board-runs-the-sketch-twice
@ -219,46 +268,11 @@ void setup() {
displayTestScreen(display2);
// make sure, test screen is at least displayed 2 seconds
delay(2000);
updateDisplay1("---", 3);
updateDisplay2("---", 3);
}
void loop() {
display1.clearDisplay();
display1.setTextColor(SSD1306_WHITE); // Draw white text
display1.cp437(true); // Use full 256 char 'Code Page 437' font
display1.setTextSize(1); // Normal 1:1 pixel scale
display1.setCursor(0, 8); // Start at top-left corner
display1.setFont(NULL);
display1.write(name1[0]);
display1.write(name1[1]);
display1.write(name1[2]);
display1.setTextSize(2); // Normal 1:1 pixel scale
display1.setCursor(0, 56); // Start at top-left corner
display1.setFont(&FreeMonoBold18pt7b);
display1.write('8');
display1.write('9');
display1.write('0');
display1.display();
display2.clearDisplay();
display2.setTextColor(SSD1306_WHITE); // Draw white text
display2.cp437(true); // Use full 256 char 'Code Page 437' font
display2.setTextSize(1); // Normal 1:1 pixel scale
display2.setCursor(0, 8); // Start at top-left corner
display2.setFont(NULL);
display2.write(name2[0]);
display2.write(name2[1]);
display2.write(name2[2]);
display2.setTextSize(2); // Normal 1:1 pixel scale
display2.setCursor(0, 56); // Start at top-left corner
display2.setFont(&FreeMonoBold18pt7b);
display2.write('1');
display2.write('2');
display2.write('3');
display2.display();
delay(1000);
}