mirror of
https://codeberg.org/mclemens/hr50-remote-display.git
synced 2024-09-28 00:25:55 -04:00
144 lines
4.2 KiB
C++
144 lines
4.2 KiB
C++
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include "Arduino.h"
|
|
#include "heltec.h"
|
|
#include <ArduinoJson.h>
|
|
#include "hr50_fonts.h"
|
|
|
|
#define ESP_INTR_FLAG_DEFAULT 0
|
|
#define PRG_BUTTON 0
|
|
|
|
|
|
const char* ssid = "<WIFI-SSID>";
|
|
const char* password = "<WIFI-PASSWORD>";
|
|
String api_base_url = "http://<IP_OF_HR50_API>:5000";
|
|
//String api_button_url = api_base_url + "/?cmd=hrtu1";
|
|
String api_button_url = api_base_url + "/exec";
|
|
String api_status_url = api_base_url + "/status";
|
|
int first_row = 0;
|
|
int second_row = 24;
|
|
int third_row = 48;
|
|
int left = 0;
|
|
int right = 128;
|
|
String cmd_status = "";
|
|
|
|
SemaphoreHandle_t semaphore = nullptr;
|
|
|
|
void IRAM_ATTR handler(void* arg) {
|
|
xSemaphoreGiveFromISR(semaphore, NULL);
|
|
}
|
|
|
|
void button_task(void* arg) {
|
|
for (;;) {
|
|
if (xSemaphoreTake(semaphore, portMAX_DELAY) == pdTRUE) {
|
|
cmd_status = send_http_request(api_button_url);
|
|
}
|
|
}
|
|
}
|
|
|
|
String send_http_request(String url) {
|
|
HTTPClient http;
|
|
String response = "";
|
|
http.begin(url.c_str());
|
|
// Send HTTP GET request
|
|
int httpResponseCode = http.GET();
|
|
if (httpResponseCode > 0) {
|
|
response = http.getString();
|
|
}
|
|
else {
|
|
printError("HTTPError: " + (String)httpResponseCode);
|
|
}
|
|
http.end();
|
|
return response;
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, false /*Serial Enable*/);
|
|
Heltec.display->flipScreenVertically();
|
|
Heltec.display->setFont(DialogInput_plain_16);
|
|
|
|
WiFi.begin(ssid, password);
|
|
delay(4000);
|
|
|
|
// Configure PRG button of the Heltec
|
|
semaphore = xSemaphoreCreateBinary();
|
|
// Setup the button GPIO pin
|
|
gpio_pad_select_gpio(PRG_BUTTON );
|
|
// Quite obvious, a button is a input
|
|
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT);
|
|
// Trigger the interrupt when going from HIGH -> LOW ( == pushing button)
|
|
gpio_set_intr_type(GPIO_NUM_0, GPIO_INTR_NEGEDGE);
|
|
// Associate button_task method as a callback
|
|
xTaskCreate(button_task, "prg_button_task", 4096, NULL, 10, NULL);
|
|
// Install ISR service
|
|
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
|
|
// Add button handler to the ISR
|
|
gpio_isr_handler_add(GPIO_NUM_0, handler, NULL);
|
|
|
|
}
|
|
|
|
void printError(String err) {
|
|
Heltec.display->clear();
|
|
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
|
|
Heltec.display->drawString(left, second_row, err );
|
|
Heltec.display->display();
|
|
}
|
|
|
|
|
|
void print_status_screen(JsonObject status_json) {
|
|
|
|
if ( status_json != NULL and status_json["PTT"].as<String>() != "ERR") {
|
|
String band = status_json["BND"].as<String>();
|
|
String pep = status_json["PEP"].as<String>();
|
|
String avg = status_json["AVG"].as<String>();
|
|
String swr = status_json["SWR"].as<String>();
|
|
String voltage = status_json["VLT"].as<String>();
|
|
String power = pep + "W/" + avg + "W";
|
|
String ptt = status_json["PTT"].as<String>();
|
|
String temp = status_json["TMP"].as<String>();
|
|
|
|
// clear the display
|
|
Heltec.display->clear();
|
|
// print left column
|
|
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
|
|
Heltec.display->drawString(left, first_row, band );
|
|
Heltec.display->drawString(left, second_row, power );
|
|
if ( cmd_status == "" ) {
|
|
Heltec.display->drawString(left, third_row, voltage );
|
|
} else {
|
|
Heltec.display->drawString(left, third_row, cmd_status );
|
|
}
|
|
// print right column
|
|
Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
|
|
Heltec.display->drawString(right, first_row, swr );
|
|
Heltec.display->drawString(right, second_row, ptt );
|
|
Heltec.display->drawString(right, third_row, temp );
|
|
Heltec.display->display();
|
|
}
|
|
else
|
|
{
|
|
// print a "(!)" centered in the third row
|
|
// this happens when the TRX/PA is transmitting
|
|
// or otherwise an empty response was send by the API
|
|
Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
|
|
Heltec.display->drawString(58, third_row, "(!)" );
|
|
Heltec.display->display();
|
|
}
|
|
|
|
}
|
|
|
|
void loop() {
|
|
//Check WiFi connection status
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
String response = send_http_request(api_status_url);
|
|
StaticJsonDocument<200> doc;
|
|
deserializeJson(doc, response);
|
|
JsonObject obj = doc.as<JsonObject>();
|
|
print_status_screen(obj);
|
|
|
|
}
|
|
else {
|
|
printError("No WiFi!");
|
|
}
|
|
} |