Arduino示例
/**
* TEA5767 FM收音机模块示例程序
* author: Billy Zhang
*/
#include <Wire.h>
#include <radio.h> // https://github.com/mathertel/Radio
#include <TEA5767.h>
#include <OneButton.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
/**
* 北京FM台频率
* 北京文艺广播 FM87.6
* 北京新闻广播 FM94.5/AM828
* 北京音乐广播 FM97.4
* 京津冀之声 FM100.6
* 北京体育广播 FM102.5
* 北京交通广播 FM103.9
* 北京城市广播副中心之声 FM107.3/AM1026
*/
// 换成本地的FM台, 频率格式:87.6 MHz = 8760
static const uint16_t kFMFreq[7] = { 8760, 9450, 9740, 10060, 10250, 10390, 10730 };
static const uint8_t kFMNum = 7;
TEA5767 radio;
OneButton btnPrev(8);
OneButton btnNext(9);
QueueHandle_t queue;
uint8_t fm_index = 0;
/**
* 初始化按钮
* 按键btnPrev为向前调台,按键btnNex为向后调台
* 向消息队列传递调台数据,队列只存储最后一次的按钮数据,避免频繁操作。
*/
void initButtons() {
btnPrev.attachClick([](){
uint8_t value=1;
xQueueOverwrite(queue, &value);
});
btnNext.attachClick([](){
uint8_t value=2;
xQueueOverwrite(queue, &value);
});
xTaskCreate([](void* param) {
while(1) {
btnPrev.tick();
btnNext.tick();
delay(10);
}
}, "ButtonTick_Task", 4096, NULL, 1, NULL);
}
/**
* 调台
*/
void changeFrequency(uint8_t index) {
uint16_t freq = kFMFreq[index];
radio.setBandFrequency(RADIO_BAND_FM, freq);
Serial.printf("change frequency to %d", freq);
}
void setup() {
Serial.begin(115200);
queue = xQueueCreate(1, sizeof(uint8_t));
radio.init();
radio.setVolume(2);
changeFrequency(0);
initButtons();
}
void loop() {
uint8_t receive;
if (xQueueReceive(queue, &receive, 0) == pdPASS) {
// 接收按键数据进行调台
if (receive == 1) {
if (fm_index > 0) {
fm_index--;
changeFrequency(fm_index);
}
} else if (receive == 2) {
if (fm_index < kFMNum-1) {
fm_index++;
changeFrequency(fm_index);
}
}
delay(100);
}
}