some code refactors

This commit is contained in:
2026-03-19 22:30:32 +01:00
parent 731f69ffa1
commit 780d52c8d8
11 changed files with 136 additions and 380 deletions
+2 -3
View File
@@ -10,6 +10,7 @@
#include "rtc.hpp"
#include "core.h"
#include "gui.h"
#include "logbook.h"
void ShowAdvancedRDS();
void readRds();
@@ -21,6 +22,4 @@ void showRadioText();
void ShowAFEON();
void showCT();
void ShowErrors();
void ShowRDSStatistics();
extern bool isDST(time_t t);
void ShowRDSStatistics();
+2
View File
@@ -1,3 +1,5 @@
// This is ancient
#pragma once
#include "globals.h"
+1 -2
View File
@@ -25,8 +25,7 @@ void RdsPiBuffer::clear() {
}
RdsPiBuffer::State RdsPiBuffer::getState(uint16_t value) {
uint8_t count = 0;
uint8_t correctCount = 0;
uint8_t count = 0, correctCount = 0;
for (uint8_t i = 0; i < this->fill; i++) {
if (this->buff[i] == value) {
+2 -2
View File
@@ -128,14 +128,13 @@ void TEF6686::init(byte TEF) {
// Start the firmware
devTEF_Set_Cmd(TEF_INIT, 0, 0);
while(devTEF_APPL_Get_Operation_Status() != 1) delay(1); // Wait for it to load
while(devTEF_APPL_Get_Operation_Status() != 1) delay(2); // Wait for it to load
if(clock != 9216000) devTEF_Set_Cmd(TEF_APPL, Cmd_Set_ReferenceClock, 3, (clock >> 16) & 0xffff, clock & 0xffff, (clock == 55466670) ? 1 : 0);
devTEF_Set_Cmd(TEF_APPL, Cmd_Set_Activate, 1, 1); // Setup done, start radio
while(devTEF_APPL_Get_Operation_Status() != 2) delay(1); // Wait for it to start
Wire.setClock(old_clock);
devTEF_Set_Cmd(TEF_FM, Cmd_Set_LevelStep, 7, 0xffff, 0xffff, 0xffff, 0xffff, 0xfffc, 0xfff8, 0x0);
devTEF_Set_Cmd(TEF_FM, Cmd_Set_Highcut_Mph, 3, 0, 360, 300);
devTEF_Set_Cmd(TEF_FM, Cmd_Set_Highcut_Max, 2, 0, 4000);
@@ -148,6 +147,7 @@ void TEF6686::init(byte TEF) {
devTEF_Set_Cmd(TEF_FM, Cmd_Set_StHiBlend_Max, 2, 0, 4000);
devTEF_Set_Cmd(TEF_AUDIO, Cmd_Set_Ana_Out, 2, 128, 1);
devTEF_Set_Cmd(TEF_AUDIO, Cmd_Set_Output_Source, 2, 128, 224);
Wire.setClock(old_clock);
uint16_t device = getIdentification(NULL, NULL);
+1 -1
View File
@@ -29,7 +29,7 @@ bool devTEF_Get_Cmd(TEF_MODULE module, uint8_t cmd, uint8_t *receive, uint16_t l
uint8_t devTEF_APPL_Get_Operation_Status() {
uint8_t buf[2];
while(!devTEF_Get_Cmd(TEF_APPL, Cmd_Get_Operation_Status, buf, sizeof(buf))) delay(1);
while(!devTEF_Get_Cmd(TEF_APPL, Cmd_Get_Operation_Status, buf, sizeof(buf))) delay(2);
return Convert8bto16b(buf);
}
+2 -3
View File
@@ -2282,7 +2282,7 @@ void Seek(bool mode) {
if (!screenmute) tft.drawBitmap(249, 4, Speaker, 28, 24, PrimaryColor);
if (!mode) TuneDown(); else TuneUp();
delay(50);
delay(55);
ShowFreq(0);
if (XDRGTKUSB || XDRGTKTCP) {
@@ -2375,8 +2375,7 @@ uint8_t doAutoMemory(uint16_t startfreq, uint16_t stopfreq, uint8_t startmem, ui
error = 1;
stopScanning = true;
break;
}
dostore = false;
} dostore = false;
}
}
+81 -294
View File
File diff suppressed because it is too large Load Diff
+26 -43
View File
@@ -37,7 +37,7 @@ void read_encoder() {
static uint8_t old_AB = 3;
static int8_t encval = 0;
static const int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
constexpr int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
old_AB <<= 2;
if (digitalRead(ROTARY_PIN_A)) old_AB |= 0x02;
@@ -124,10 +124,8 @@ void later_setup_periph() {
Wire.endTransmission();
} else console.print("XL9555 found not found on address " + String(XL9555_ADDRESS, HEX) + ". Numpad will not work");
if(tef_found) {
// The tuner being missing here, would cause a infinite loop with no exit and no error, as it resets and polls the chip if it reset, if no response then we try again, and now, you see?
radio.init(TEF);
}
// The tuner being missing here, would cause a infinite loop with no exit and no error, as it resets and polls the chip if it reset, if no response then we try again, and now, you see?
if(tef_found) radio.init(TEF);
uint16_t device, hw, sw;
device = radio.getIdentification(&hw, &sw);
@@ -172,8 +170,7 @@ void later_setup_periph() {
if(fmsi) radio.setFMSI(2);
if(rx_rtc_avail) {
bool reset = init_rtc();
if(reset) console.print("RX8010SJ was reset, no time");
if(init_rtc()) console.print("RX8010SJ was reset, no time");
else {
rtcset = true;
console.print("RX8010SJ is used as a time source");
@@ -187,7 +184,7 @@ void later_setup_periph() {
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
analogWriteFrequency(6700); // six seven
analogWriteFrequency(6700); // sick in the head seven
EEPROM.begin(EE_TOTAL_CNT);
@@ -254,7 +251,7 @@ void setup() {
break;
}
tft.init();
tft.init(); // have to reinit display because of how the reset pin works: toggling it from 1 to 0 makes it reset, when the esp32 stops, the pin goes off, and when we start we bring it up and accidentally restart the display: thanks very much
tft.loadFont(FONT16_CHS, 0);
tft.loadFont(FONT48, 1);
@@ -587,8 +584,7 @@ void doLog() {
delay(DELAY_UI_UPDATE_MS);
ShowFreq(0);
}
autologged = true;
} autologged = true;
}
}
@@ -736,8 +732,8 @@ void ShowSignalLevel() {
int SStatusprint = 0;
if (unit == 0) SStatusprint = SStatus;
if (unit == 1) SStatusprint = ((SStatus * 100) + 10875) / 100;
if (unit == 2) SStatusprint = round((float(SStatus) / 10.0 - 10.0 * log10(75) - 90.0) * 10.0);
else if (unit == 1) SStatusprint = ((SStatus * 100) + 10875) / 100;
else if (unit == 2) SStatusprint = round((float(SStatus) / 10.0 - 10.0 * log10(75) - 90.0) * 10.0);
static int DisplayedSignalSegments = 0;
if (SStatusprint > (SStatusold + 3) || SStatusprint < (SStatusold - 3)) {
@@ -831,8 +827,7 @@ void ShowRSSI() {
} else if (rssi < -70) {
tft.drawBitmap(WIFI_ICON_X, WIFI_ICON_Y, WiFi4, WIFI_ICON_WIDTH, WIFI_ICON_HEIGHT, GreyoutColor);
tft.drawBitmap(WIFI_ICON_X, WIFI_ICON_Y, WiFi1, WIFI_ICON_WIDTH, WIFI_ICON_HEIGHT, WifiColorLow);
}
rssiold = rssi;
} rssiold = rssi;
}
}
@@ -844,12 +839,12 @@ void ShowBW() {
if(BW == 0) panic("BW is 0");
else if(BW > 311) panic("BW larger than 311");
if(BWset == 0) tftReplace(ARIGHT, String(BWOld, DEC), String(BW, DEC), 203, 4, BWAutoColor, BWAutoColorSmooth, BackgroundColor, 28); else tftReplace(ARIGHT, String (BWOld, DEC), String(BW, DEC), 203, 4, PrimaryColor, PrimaryColorSmooth, BackgroundColor, 28);
if(BWset == 0) tftReplace(ARIGHT, String(BWOld), String(BW), 203, 4, BWAutoColor, BWAutoColorSmooth, BackgroundColor, 28); else tftReplace(ARIGHT, String(BWOld), String(BW), 203, 4, PrimaryColor, PrimaryColorSmooth, BackgroundColor, 28);
BWOld = BW;
BWreset = false;
if (wifi) {
Udp.beginPacket(remoteip, 9030);
Udp.print("from=TEF_tuner_" + String(stationlistid, DEC) + ";bandwidth=" + String(BW * 1000));
Udp.print("from=TEF_tuner_" + String(stationlistid) + ";bandwidth=" + String(BW * 1000));
Udp.endPacket();
}
}
@@ -860,10 +855,10 @@ void ShowOffset() {
if (millis() >= offsetupdatetimer + TIMER_OFFSET_TIMER) offsetupdatetimer = millis();
else return;
int baseX = 13; // Left boundary
int baseY = 2; // Top boundary
int width = 4; // Max width
int height = 26; // Max height
constexpr int baseX = 13; // Left boundary
constexpr int baseY = 2; // Top boundary
constexpr int width = 4; // Max width
constexpr int height = 26; // Max height
int centerX = baseX + width / 2; // Center dot X
int centerY = baseY + height / 2; // Center dot Y
@@ -934,8 +929,7 @@ void ShowOffset() {
tft.fillCircle(centerX, centerY, 3, GreyoutColor);
tuned = false;
}
}
OStatusold = OStatus;
} OStatusold = OStatus;
}
}
@@ -998,11 +992,8 @@ void GetData() {
showPTY();
showRadioText();
if (millis() >= tuningtimer + 200) doAF();
}
showPI();
}
ShowStereoStatus();
} showPI();
} ShowStereoStatus();
if (!screenmute) {
showCT();
@@ -1019,6 +1010,7 @@ inline __attribute__((always_inline)) const char* getBWButtonLabel(byte temp, bo
if (temp == BW_OK_BUTTON) return "OK";
return isFM ? BWButtonLabelsFM[temp - 1] : BWButtonLabelsAM[temp - 1];
}
inline __attribute__((always_inline)) bool shouldHighlightBWButton(byte temp, byte set) {
return (set == temp) || (temp == 17 && set == 0) || (temp == 18 && !iMSset) || (temp == 19 && !EQset);
}
@@ -1069,13 +1061,9 @@ void loop() {
if(i2c_pc_control) {
total_pc_control();
if(i2c_pc_control) return;
if(EEPROM.readByte(EE_BYTE_CONTROLMODE)) {
saveData();
esp_restart();
}
}
wifiPoll(); // He really lost it
wifiPoll(); //
handleWiFi();
handleTouch();
@@ -1194,8 +1182,8 @@ void loop() {
}
if (!foundmemaf) {
frequency = freqold;
radio.SetFreq(frequency);
frequency = freqold;
}
if (!screenmute) {
@@ -1224,8 +1212,7 @@ void loop() {
if (screenmute) {
freqold = frequency;
dropout = false;
}
store = true;
} store = true;
}
if (band == BAND_FM && af != 0 && radio.rds.correctPI != 0) {
@@ -1244,8 +1231,7 @@ void loop() {
if (screenmute) {
freqold = frequency;
dropout = false;
}
store = true;
} store = true;
}
}
@@ -1300,8 +1286,7 @@ void loop() {
tftPrint16(ALEFT, "RT", 3, 221, ActiveColor, ActiveColorSmooth);
tftPrint16(ALEFT, "PTY", 3, 163, ActiveColor, ActiveColorSmooth);
if (!showaudio) tft.drawLine(16, 143, 189, 143, GreyoutColor); else tft.drawLine(16, 143, 189, 143, ActiveColor);
}
LowLevelInit = true;
} LowLevelInit = true;
}
if ((SStatus / 10 <= LowLevelSet) && band < BAND_GAP) {
@@ -1333,8 +1318,7 @@ void loop() {
tftPrint16(ALEFT, "PTY", 3, 163, GreyoutColor, BackgroundColor);
tft.drawLine(16, 143, 189, 143, GreyoutColor);
tft.drawBitmap(68, 5, RDSLogo, 35, 22, GreyoutColor);
}
LowLevelInit = false;
} LowLevelInit = false;
}
if (!BWtune && !menu && (screenmute || radio.rds.correctPI != 0)) readRds();
@@ -1349,7 +1333,6 @@ void loop() {
GetData();
}
}
} else {
if (af || (!screenmute || (screenmute && (XDRGTKTCP || XDRGTKUSB)))) {
if (band < BAND_GAP) radio.getStatus(&SStatus, &USN, &WAM, &OStatus, &BW, &MStatus);
+13 -21
View File
@@ -127,7 +127,7 @@ void doAF() {
if (radio.af_counter != af_counterold && radio.rds.hasAF) {
if (wifi) {
Udp.beginPacket(remoteip, 9030);
Udp.print("from=TEF_tuner_" + String(stationlistid, DEC) + ";AF=");
Udp.print("from=TEF_tuner_" + String(stationlistid) + ";AF=");
for (byte af_scan = 0; af_scan < radio.af_counter; af_scan++) {
if (wifi) {
@@ -254,7 +254,7 @@ void readRds() {
sprintf(hexbuf, "%X%X", (erroutput >> 4) & 0xF, erroutput & 0xF);
XDRGTKRDS += hexbuf;
XDRGTKRDS += '\n';
XDRGTKRDS += F('\n');
if (XDRGTKRDS.changed(0)) {
uint8_t piError = radio.rds.rdsErr >> 14;
@@ -491,7 +491,7 @@ void showCT() {
char dateStr[9];
time_t t = rtc.getEpoch();
t += Timezone * 3600; // Convert offset from hours to seconds
t += Timezone * 3600;
if (autoDST && isDST(t)) t += 3600;
auto localtm = localtime(&t);
@@ -511,9 +511,7 @@ void showCT() {
if (hour < 0 || hour > 23) hour = 0;
snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d", hour, localtm->tm_min, localtm->tm_sec);
}
rds_clock = String(timeStr);
} rds_clock = String(timeStr);
if (clockampm) strftime(dateStr, sizeof(dateStr), "%m-%d-%y", localtm);
else strftime(dateStr, sizeof(dateStr), "%d-%m-%y", localtm);
@@ -548,11 +546,8 @@ void showRadioText() {
if (RThex[i] < 0x10) Udp.print("0");
if (RThex[i] == ' ') RThex[i] = '_';
Udp.print(String(RThex[i], HEX));
}
Udp.endPacket();
}
RTold = RTString;
} Udp.endPacket();
} RTold = RTString;
}
}
@@ -711,8 +706,7 @@ void ShowAFEON() {
uint8_t nibble = (radio.rds.aid[y] >> (4 * (3 - z))) & 0xF;
if (nibble < 10) id[z] = nibble + '0';
else id[z] = nibble - 10 + 'A';
}
id[4] = '\0';
} id[4] = '\0';
AIDStringTemp += String(id);
AIDStringTemp += ": ";
@@ -733,9 +727,7 @@ void ShowAFEON() {
AIDWidth = FullLineSprite.textWidth(AIDString);
AIDStringold = AIDString;
}
}
aidDisplay.update(AIDString, true, ActiveColor, ActiveColorSmooth, 0, 0, BackgroundColor);
} aidDisplay.update(AIDString, true, ActiveColor, ActiveColorSmooth, 0, 0, BackgroundColor);
}
}
@@ -784,7 +776,7 @@ void ShowRDSStatistics() {
if (lastX >= 0 && (lastX != xpos || lastY != ypos)) tft.fillCircle(lastX - 55, lastY + 7, 2, SignificantColor);
tftReplace(ARIGHT, oldBuf, newBuf, xpos, ypos, PrimaryColor, PrimaryColorSmooth, BackgroundColor, 16);
tftPrint(ACENTER, "%", xposPct, ypos, ActiveColor, ActiveColorSmooth, 16);
tftPrint(ACENTER, F("%"), xposPct, ypos, ActiveColor, ActiveColorSmooth, 16);
tft.fillCircle(xpos - 55, ypos + 7, 2, InsignificantColor);
@@ -796,11 +788,11 @@ void ShowRDSStatistics() {
}
String HexString = String(((radio.rds.rdsA >> 12) & 0xF), HEX) + String(((radio.rds.rdsA >> 8) & 0xF), HEX) + String(((radio.rds.rdsA >> 4) & 0xF), HEX) + String((radio.rds.rdsA & 0xF), HEX);
HexString += " ";
HexString += F(" ");
HexString += String(((radio.rds.rdsB >> 12) & 0xF), HEX) + String(((radio.rds.rdsB >> 8) & 0xF), HEX) + String(((radio.rds.rdsB >> 4) & 0xF), HEX) + String((radio.rds.rdsB & 0xF), HEX);
HexString += " ";
HexString += F(" ");
HexString += String(((radio.rds.rdsC >> 12) & 0xF), HEX) + String(((radio.rds.rdsC >> 8) & 0xF), HEX) + String(((radio.rds.rdsC >> 4) & 0xF), HEX) + String((radio.rds.rdsC & 0xF), HEX);
HexString += " ";
HexString += F(" ");
HexString += String(((radio.rds.rdsD >> 12) & 0xF), HEX) + String(((radio.rds.rdsD >> 8) & 0xF), HEX) + String(((radio.rds.rdsD >> 4) & 0xF), HEX) + String((radio.rds.rdsD & 0xF), HEX);
HexString.toUpperCase();
@@ -836,7 +828,7 @@ void ShowRDSStatistics() {
int berPercent = (int)(smoothBER * 100.0);
if (berPercentold != berPercent) {
tftReplace(ARIGHT, String(berPercentold) + "%", String(berPercent) + "%", 318, 34, PrimaryColor, PrimaryColorSmooth, BackgroundColor, 16);
tftReplace(ARIGHT, String(berPercentold) + F("%"), String(berPercent) + F("%"), 318, 34, PrimaryColor, PrimaryColorSmooth, BackgroundColor, 16);
berPercentold = berPercent;
}
}
+1 -1
View File
@@ -118,7 +118,7 @@ bool init_rtc() {
writeToModule(0x1F, 0);
return true;
}
sync_from_rx_rtc(1); // mystery offset
sync_from_rx_rtc(1); // mystery offset, without it the time is offset by one second
return false;
}
+5 -10
View File
@@ -159,11 +159,9 @@ void doTouchEvent(uint16_t x, uint16_t y) {
doBW();
BWtune = false;
bwtouchtune = false;
if (advancedRDS) {
BuildAdvancedRDS();
} else if (afscreen) {
BuildAFScreen();
} else {
if (advancedRDS) BuildAdvancedRDS();
else if (afscreen) BuildAFScreen();
else {
BuildDisplay();
SelectBand();
}
@@ -173,8 +171,7 @@ void doTouchEvent(uint16_t x, uint16_t y) {
showBWSelector();
doBW();
bwtouchtune = false;
}
return;
} return;
}
if (!BWtune && !menu && !advancedRDS && !seek && !afscreen) {
@@ -188,9 +185,7 @@ void doTouchEvent(uint16_t x, uint16_t y) {
} else if (x > 0 && x < 30 && y > 25 && y < 90) {
doTuneMode();
return;
} else if (x > 250 && x < 320 && y > 50 && y < 80) {
toggleiMSEQ();
}
} else if (x > 250 && x < 320 && y > 50 && y < 80) toggleiMSEQ();
}
if (!BWtune && !menu && advancedRDS && !seek && !afscreen) {