#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MD_REncoder.h>
#include <TEA5767Radio.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
MD_REncoder R = MD_REncoder(2, 3);
TEA5767Radio radio = TEA5767Radio();
#define SCALE_CLEAR 5
int count = 0;
float freq;
void setup()
{
Wire.begin();
lcd.init();
lcd.backlight();
R.begin();
//==============================================================//
byte scaleChar[8] = {
0b00000,
0b00000,
0b00100,
0b10101,
0b10101,
0b00100,
0b00000,
0b00000
};
lcd.createChar(SCALE_CLEAR, scaleChar);
int i;
byte needleChar[8];
for(int j = 0; j < 5; j++) {
for(i = 0; i < 8; i++)
needleChar[i] = scaleChar[i] | (0b10000 >> j);
lcd.createChar(j, needleChar);
}
lcd.clear();
for(i = 0; i < 17; i++)
lcd.write(SCALE_CLEAR);
//==============================================================//
freq = ((count*25)+8700)*0.01;
lcd.setCursor(3,1);
lcd.print(freq);
lcd.print(" MHz ");
radio.setFrequency(freq);
updateScale();
}
void loop()
{
uint8_t x = R.read();
if (x)
{
x == DIR_CW ? count++ : count--;
if (count > 84)
count = 0;
if (count < 0)
count = 84;
freq = ((count*25)+8700)*0.01;
lcd.setCursor(3,1);
lcd.print(freq);
lcd.print(" MHz ");
radio.setFrequency(freq);
updateScale();
}
}
void updateScale() {
int lcdBase = (freq - 87) * 4;
if(lcdBase > 83)
lcdBase = 83;
int lcdMajor = lcdBase / 5;
int lcdMinor = lcdBase % 5;
if(lcdMajor > 0) {
lcd.setCursor(lcdMajor - 1, 0);
lcd.write(SCALE_CLEAR);
} else
lcd.setCursor(lcdMajor, 0);
lcd.write(lcdMinor);
if(lcdMajor < 15)
lcd.write(SCALE_CLEAR);
}