separate display control from knobs

This commit is contained in:
damage 2024-02-01 23:55:25 +01:00
parent 40bdd23478
commit e15547ce65
3 changed files with 127 additions and 44 deletions

1
display/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.h

125
display/display.ino Normal file
View File

@ -0,0 +1,125 @@
#include <U8g2lib.h>
#include <ArduinoMqttClient.h>
#include <ESP8266WiFi.h>
#include "config.h"
WiFiClient client;
MqttClient mqttClient(client);
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C headingDisplay(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // High speed I2C
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C speedDisplay(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C headingDisplay(U8G2_R0); // High speed I2C
U8G2_SSD1306_128X64_NONAME_F_HW_I2C speedDisplay(U8G2_R0);
const char broker[] = "mqtt.sugarland.lan";
int port = 1883;
const char topic[] = "/xplane/rref/#";
void onMqttMessage(int messageSize) {
if (mqttClient.messageTopic() == "/xplane/rref/1") {
char buf[3] = "";
char c = '\0';
for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) {
c = (char)mqttClient.read();
if (c != '.') {
buf[i] = c;
}
}
if (buf[1] == '\0') {
buf[1] = buf[0];
buf[0] = '0';
}
if (buf[2] == '\0') {
buf[2] = buf[1];
buf[1] = buf[0];
buf[0] = '0';
}
headingDisplay.clearBuffer();
headingDisplay.setFont(u8g2_font_fub11_tf);
headingDisplay.drawStr(0, 11, "HDG");
headingDisplay.setFont(u8g2_font_fub42_tn);
headingDisplay.drawStr(10, 63, buf);
headingDisplay.sendBuffer();
} else if (mqttClient.messageTopic() == "/xplane/rref/2") {
char buf[3] = "";
char c = '\0';
for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) {
c = (char)mqttClient.read();
if (c != '.') {
buf[i] = c;
}
}
if (buf[1] == '\0') {
buf[1] = buf[0];
buf[0] = '0';
}
if (buf[2] == '\0') {
buf[2] = buf[1];
buf[1] = buf[0];
buf[0] = '0';
}
speedDisplay.clearBuffer();
speedDisplay.setFont(u8g2_font_fub11_tf);
speedDisplay.drawStr(0, 11, "SPD");
speedDisplay.setFont(u8g2_font_fub42_tn);
speedDisplay.drawStr(10, 63, buf);
speedDisplay.sendBuffer();
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
mqttClient.setUsernamePassword(MQTT_USER3, MQTT_PASS);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
} else {
Serial.println("MQTT connected");
}
headingDisplay.begin();
speedDisplay.setI2CAddress(0x7A);
speedDisplay.begin();
mqttClient.onMessage(onMqttMessage);
mqttClient.subscribe(topic);
mqttClient.beginMessage("/xplane/meta/rref");
mqttClient.print("001 200 sim/cockpit/autopilot/heading_mag");
mqttClient.endMessage();
mqttClient.beginMessage("/xplane/meta/rref");
mqttClient.print("002 200 sim/cockpit/autopilot/airspeed");
mqttClient.endMessage();
}
void loop() {
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alives which avoids being disconnected by the broker
mqttClient.poll();
}

View File

@ -1,5 +1,3 @@
#include <U8g2lib.h>
#include <MCP23017.h> #include <MCP23017.h>
#include <ArduinoMqttClient.h> #include <ArduinoMqttClient.h>
#include <Ethernet.h> #include <Ethernet.h>
@ -30,7 +28,6 @@ MqttClient mqttClient(client);
byte lastRotInput_1A; byte lastRotInput_1A;
MCP23017 mcp1 = MCP23017(ROT_ADDR_1, 100); MCP23017 mcp1 = MCP23017(ROT_ADDR_1, 100);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // High speed I2C
void setup() { void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
@ -49,7 +46,7 @@ void setup() {
// print your local IP address: // print your local IP address:
Serial.println(Ethernet.localIP()); Serial.println(Ethernet.localIP());
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS); mqttClient.setUsernamePassword(MQTT_USER2, MQTT_PASS);
if (!mqttClient.connect(broker, port)) { if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = "); Serial.print("MQTT connection failed! Error code = ");
@ -75,15 +72,6 @@ void setup() {
mcp1.setPortPullUp(0b00000000, ROT_PORT_A); mcp1.setPortPullUp(0b00000000, ROT_PORT_A);
lastRotInput_1A = mcp1.getPort(ROT_PORT_A); lastRotInput_1A = mcp1.getPort(ROT_PORT_A);
u8g2.begin();
mqttClient.beginMessage("/xplane/meta/rref");
mqttClient.print("001 010 sim/cockpit/autopilot/heading_mag");
mqttClient.endMessage();
mqttClient.beginMessage("/xplane/meta/rref");
mqttClient.print("002 010 sim/cockpit/autopilot/airspeed");
mqttClient.endMessage();
} }
bool isBitDown(byte input, byte reference) { bool isBitDown(byte input, byte reference) {
@ -149,37 +137,6 @@ void loop() {
} }
void onMqttMessage(int messageSize) { void onMqttMessage(int messageSize) {
if (mqttClient.messageTopic() == "/xplane/rref/1") {
char buf[3] = "";
char c = '\0';
for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) {
c = (char)mqttClient.read();
if (c != '.') {
buf[i] = c;
}
}
if (buf[1] == '\0') {
buf[1] = buf[0];
buf[0] = '0';
}
if (buf[2] == '\0') {
buf[2] = buf[1];
buf[1] = buf[0];
buf[0] = '0';
}
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_fub11_tf);
u8g2.drawStr(0, 11, "HDG");
u8g2.setFont(u8g2_font_fub42_tn);
u8g2.drawStr(10, 63, buf);
u8g2.sendBuffer();
}
// TODO: speed display
// // we received a message, print out the topic and contents // // we received a message, print out the topic and contents
// Serial.println("Received a message with topic '"); // Serial.println("Received a message with topic '");
// Serial.print(mqttClient.messageTopic()); // Serial.print(mqttClient.messageTopic());