#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <ezButton.h>
#define CLK_PIN 34
#define DT_PIN 35
#define SW_PIN 32
#define CLK_PIN2 4
#define DT_PIN2 16
#define SW_PIN2 17
#define DIRECTION_CW 0
#define DIRECTION_CCW 1
#define TFT_CS 5
#define TFT_RST 2
#define TFT_DC 0
#define TFT_BL 22
long long vfoa = 740000000;
long long vfob = 2400000000;
long long adjustmentA = 1;
long long adjustmentB = 1;
int direction;
int direction2;
int prev_CLK_state, prev_CLK_state2;
bool updateVFOA = false, updateVFOB = false;
Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);
ezButton button(SW_PIN);
ezButton button2(SW_PIN2);
void highlightDigitRightJustified(long long value, long long adjustment, int y, uint16_t normalColor, uint16_t highlightColor) {
char buffer[15]; // Ensure buffer size is large enough for leading zeros
sprintf(buffer, "%010lld", value); // Format frequency as 10 digits with leading zeros
// Calculate the total text width (12 pixels per character for text size 2)
int textWidth = strlen(buffer) * 12;
// Starting X position for right justification (160 is screen width)
int xStart = 160 - textWidth;
// Find the index of the digit to highlight
int digitIndex = 9 - log10(adjustment); // Determine which digit corresponds to the adjustment
// Render the frequency string with highlighting
for (int i = 0, pos = 0; i < strlen(buffer); i++) {
// Highlight the correct digit
if (pos == digitIndex) {
tft.setTextColor(highlightColor, ST77XX_BLACK);
} else {
tft.setTextColor(normalColor, ST77XX_BLACK);
}
tft.setCursor(xStart + pos * 12, y);
tft.print(buffer[i]);
pos++;
}
}
void updateDisplayVFOA() {
tft.fillRect(0, 20, 160, 20, ST77XX_BLACK); // Clear the area
highlightDigitRightJustified(vfoa, adjustmentA, 20, ST77XX_GREEN, ST77XX_RED);
// Correctly justify "MHz" under the VFOA display
tft.setCursor(124, 40); // Position "MHz" to be right-aligned
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
tft.print("MHz");
}
void updateDisplayVFOB() {
tft.fillRect(0, 70, 160, 20, ST77XX_BLACK); // Clear the area
highlightDigitRightJustified(vfob, adjustmentB, 70, ST77XX_YELLOW, ST77XX_RED);
// Correctly justify "MHz" under the VFOB display
tft.setCursor(124, 90); // Position "MHz" to be right-aligned
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
tft.print("MHz");
}
void setup() {
tft.initR(INITR_BLACKTAB);
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
pinMode(CLK_PIN2, INPUT);
pinMode(DT_PIN2, INPUT);
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
button.setDebounceTime(50);
button2.setDebounceTime(50);
prev_CLK_state = digitalRead(CLK_PIN);
prev_CLK_state2 = digitalRead(CLK_PIN2);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
updateDisplayVFOA();
updateDisplayVFOB();
}
void loop() {
button.loop();
button2.loop();
int CLK_state = digitalRead(CLK_PIN);
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
if (digitalRead(DT_PIN) == HIGH) {
vfoa -= adjustmentA;
direction = DIRECTION_CCW;
} else {
vfoa += adjustmentA;
direction = DIRECTION_CW;
}
updateVFOA = true;
}
prev_CLK_state = CLK_state;
int CLK_state2 = digitalRead(CLK_PIN2);
if (CLK_state2 != prev_CLK_state2 && CLK_state2 == HIGH) {
if (digitalRead(DT_PIN2) == HIGH) {
vfob -= adjustmentB;
direction2 = DIRECTION_CCW;
} else {
vfob += adjustmentB;
direction2 = DIRECTION_CW;
}
updateVFOB = true;
}
prev_CLK_state2 = CLK_state2;
if (updateVFOA) {
updateVFOA = false;
updateDisplayVFOA();
}
if (updateVFOB) {
updateVFOB = false;
updateDisplayVFOB();
}
if (button.isPressed()) {
adjustmentA *= 10;
if (adjustmentA > 1000000000) {
adjustmentA = 1;
}
updateVFOA = true;
}
if (button2.isPressed()) {
adjustmentB *= 10;
if (adjustmentB > 1000000000) {
adjustmentB = 1;
}
updateVFOB = true;
}
}
any thoughts about this?