Hi there,
Following code is apart of a simple FM radio using a TEA5767 module.
In the function drawSprite() I am trying to show the station name when the rotory encoder value hits the sation frequency from a 2D array (radioStations). This is where I am stuck.
If I hardcode a string value, it prints, but not the array eliment.
spr.drawString("Gold Radio", 160, 20, 5); //This works
spr.drawString(radioStations[i][0], 160, 20, 5); //This do not work
Full project code;
#include "AiEsp32RotaryEncoder.h"
#include <Wire.h>
#include <TEA5767.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#define TFT_GREY 0x5AEB
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);
#define color1 0xC638
#define color2 0xC638
#define ROTARY_ENCODER_A_PIN 16
#define ROTARY_ENCODER_B_PIN 17
#define ROTARY_ENCODER_BUTTON_PIN 5
#define ROTARY_ENCODER_STEPS 4
#define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */
TEA5767 radio = TEA5767();
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
long min_band = 87.5;
long max_band = 108;
float freq = 88;
String radioStations[6][2] = {
{"A FM", "87.8",},
{"B FM", "88.8",},
{"C FM", "89.3",},
{"D FM", "89.6",},
{"E FM", "90.9",},
{"F FM", "91.7",}
};
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
float getFrequency()
{
return (float)rotaryEncoder.readEncoder() / 10.0;
}
void setup() {
Wire.begin();
radio.setStereoNC(true);
tft.begin();
tft.writecommand(0x11);
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
spr.createSprite(320,170);
spr.setTextDatum(4);
spr.setSwapBytes(true);
spr.setFreeFont(&Orbitron_Light_24);
spr.setTextColor(color1,TFT_BLACK);
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(min_band * 10, max_band * 10, true); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setAcceleration(50);
rotaryEncoder.setEncoderValue(freq * 10); //set default to 92.1 MHz
drawSprite();
}
void loop(){
if (rotaryEncoder.encoderChanged())
{
freq = getFrequency();
drawSprite();
}
}
void drawSprite()
{
radio.setFrequency(freq);
spr.fillSprite(TFT_BLACK);
spr.setTextColor(TFT_WHITE,TFT_BLACK);
int stationCount = sizeof(radioStations) / sizeof(radioStations[0]);
for (int i = 0; i < stationCount; i++) {
if (radioStations[i][1] == String(freq, 1)) {
spr.drawString(radioStations[i][0], 160, 20, 5);
}
}
spr.drawFloat(freq,1,160,64,6);
spr.pushSprite(0,0);
}