send rref data to salve and display
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user