Because I did not create a radio control application, I used a common Bluetooth terminal program to control the radio. And, I have a plan to create an Android Bluetooth control app and support a real frequency controller connected to the device with a display window.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <TEA5767.h> | |
#include <Wire.h> | |
#include <ctype.h> | |
SoftwareSerial BT(9, 8); | |
TEA5767 Radio; //Pinout SCL and SDA - Arduino pins A5 and A4 | |
double old_frequency; | |
double frequency; | |
int search_mode = 0; | |
int search_direction; | |
unsigned char buf[5]; | |
int stereo; | |
int signal_level; | |
double current_freq; | |
char buffer[256]; | |
int index = 0; | |
void setup() | |
{ | |
// Uncomment following statements to change the bluetooth device name | |
/* | |
Serial.begin(9600); | |
BT.begin(9600); | |
delay(500); | |
BT.print("AT+NAMEFocalRadio 1"); | |
delay(200); | |
return; | |
*/ | |
Serial.begin(9600); | |
BT.begin(9600); | |
BT.println("FocalRadio 1"); | |
Serial.println("FocalRadio 1"); | |
Wire.begin(); | |
Radio.init(); | |
Radio.set_frequency(104.5); | |
} | |
void loop() | |
{ | |
index = 0; | |
buffer[0] = NULL; | |
while (BT.available()) | |
{ | |
char ch = BT.read(); | |
buffer[index++] = ch; | |
if (index == 256 || ch == '\0') | |
break; | |
} | |
if (index > 0) | |
{ | |
buffer[index] = NULL; | |
//Serial.println(buffer); | |
} | |
else | |
{ | |
delay(50); | |
return; | |
} | |
if (search_mode == 1) | |
{ | |
if (Radio.process_search(buf, search_direction) == 1) | |
{ | |
search_mode = 0; | |
getCurrentInfo(); | |
} | |
} | |
if (String(buffer).equals("s")) | |
{ | |
getCurrentInfo(); | |
} | |
else if (String(buffer).equals("u")) | |
{ | |
BT.println("Search up..."); | |
search_mode = 1; | |
search_direction = TEA5767_SEARCH_DIR_UP; | |
Radio.search_up(buf); | |
delay(100); | |
} | |
else if (String(buffer).equals("d")) | |
{ | |
BT.println("Search down..."); | |
search_mode = 1; | |
search_direction = TEA5767_SEARCH_DIR_DOWN; | |
Radio.search_down(buf); | |
delay(100); | |
} | |
else if (isdigit(buffer[0]) && !isalpha(buffer)) | |
{ | |
float freq = String(buffer).toFloat(); | |
if (76.0 <= freq && freq <= 108.0) | |
{ | |
BT.print("Freq: "); | |
BT.println(freq); | |
Radio.set_frequency(freq); | |
} | |
} | |
delay(50); | |
} | |
void getCurrentInfo() | |
{ | |
if (Radio.read_status(buf) == 1) | |
{ | |
current_freq = floor(Radio.frequency_available (buf) / 100000 + .5) / 10; | |
stereo = Radio.stereo(buf); | |
signal_level = Radio.signal_level(buf); | |
BT.print(current_freq); | |
if (stereo) | |
BT.print(" STEREO "); | |
else | |
BT.print(" MONO "); | |
BT.print(signal_level); | |
BT.println("/15"); | |
} | |
} | |
No comments:
Post a Comment