mirror of
https://github.com/sweidac/kidsbox.git
synced 2025-07-27 19:31:54 +02:00
Play a configured file when rfid-tag is placed on the reader.
This commit is contained in:
@@ -7,6 +7,8 @@
|
|||||||
AudioPlayer::AudioPlayer() {
|
AudioPlayer::AudioPlayer() {
|
||||||
playerHandle = setup_player(NULL, NULL);
|
playerHandle = setup_player(NULL, NULL);
|
||||||
esp_player_init(playerHandle);
|
esp_player_init(playerHandle);
|
||||||
|
|
||||||
|
esp_audio_vol_set(playerHandle, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayer::play(char* uri) {
|
void AudioPlayer::play(char* uri) {
|
||||||
|
@@ -1,2 +1,2 @@
|
|||||||
idf_component_register(SRCS "Networking.cpp" "main.cpp" "WebInterface.cpp" "RFIDInterface.cpp" "audio_setup.c" "AudioPlayer.cpp"
|
idf_component_register(SRCS "Networking.cpp" "main.cpp" "WebInterface.cpp" "RFIDInterface.cpp" "audio_setup.c" "AudioPlayer.cpp" "TagPlayerControl.cpp"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
@@ -19,10 +19,10 @@ static char* getLastTagId() {
|
|||||||
|
|
||||||
RFIDInterface::RFIDInterface() {
|
RFIDInterface::RFIDInterface() {
|
||||||
startConfig = (rc522_start_args_t){
|
startConfig = (rc522_start_args_t){
|
||||||
.miso_io = 26,
|
.miso_io = 12,
|
||||||
.mosi_io = 25,
|
.mosi_io = 13,
|
||||||
.sck_io = 23,
|
.sck_io = 23,
|
||||||
.sda_io = 5,
|
.sda_io = 18,
|
||||||
.callback = [](uint8_t* sn) { // serial number is always 5 bytes long
|
.callback = [](uint8_t* sn) { // serial number is always 5 bytes long
|
||||||
ESP_LOGI(TAG, "Tag: %#x %#x %#x %#x %#x",
|
ESP_LOGI(TAG, "Tag: %#x %#x %#x %#x %#x",
|
||||||
sn[0], sn[1], sn[2], sn[3], sn[4]
|
sn[0], sn[1], sn[2], sn[3], sn[4]
|
||||||
|
52
device/main/TagPlayerControl.cpp
Normal file
52
device/main/TagPlayerControl.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "TagPlayerControl.hpp"
|
||||||
|
|
||||||
|
#include "AudioPlayer.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
static const char* TAG = "TagPlayerControl";
|
||||||
|
|
||||||
|
TagPlayerControl::TagPlayerControl(AudioPlayer* player) {
|
||||||
|
audioPlayer = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TagPlayerControl::onTagChanged(char* tagId) {
|
||||||
|
std::string filePath = "/sdcard/internal/";
|
||||||
|
filePath.append(tagId);
|
||||||
|
|
||||||
|
std::fstream file(filePath);
|
||||||
|
|
||||||
|
if (! file) {
|
||||||
|
ESP_LOGE(TAG, "Error opening file for tag %s", tagId);
|
||||||
|
|
||||||
|
char* error_name;
|
||||||
|
|
||||||
|
switch (errno)
|
||||||
|
{
|
||||||
|
case EACCES:
|
||||||
|
ESP_LOGE(TAG, "EACCESS");
|
||||||
|
break;
|
||||||
|
case ENOENT:
|
||||||
|
ESP_LOGE(TAG, "ENOENT");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE(TAG, "not parsed");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
std::string content;
|
||||||
|
|
||||||
|
while(getline(file, line)) {
|
||||||
|
content.append(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
audioPlayer->play((char*)(content.c_str()));
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
}
|
11
device/main/TagPlayerControl.hpp
Normal file
11
device/main/TagPlayerControl.hpp
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AudioPlayer.hpp"
|
||||||
|
|
||||||
|
class TagPlayerControl {
|
||||||
|
AudioPlayer* audioPlayer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
TagPlayerControl(AudioPlayer* player);
|
||||||
|
void onTagChanged(char* tagId);
|
||||||
|
};
|
@@ -40,7 +40,7 @@ void WebInterface::initSD() {
|
|||||||
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
|
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
|
||||||
|
|
||||||
// Initialize SD Card peripheral
|
// Initialize SD Card peripheral
|
||||||
if (audio_board_sdcard_init(set, SD_MODE_4_LINE) == ESP_FAIL) {
|
if (audio_board_sdcard_init(set, SD_MODE_1_LINE) == ESP_FAIL) {
|
||||||
ESP_LOGE(TAG, "Sdcard not mounted");
|
ESP_LOGE(TAG, "Sdcard not mounted");
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "SDCard mounted");
|
ESP_LOGI(TAG, "SDCard mounted");
|
||||||
|
@@ -11,9 +11,9 @@ class WebInterface {
|
|||||||
void start();
|
void start();
|
||||||
void finishResourceRegistrations();
|
void finishResourceRegistrations();
|
||||||
void registerResource(httpd_uri_t* config);
|
void registerResource(httpd_uri_t* config);
|
||||||
|
void initSD();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initSD();
|
|
||||||
httpd_handle_t start_webserver();
|
httpd_handle_t start_webserver();
|
||||||
void registerFileServer();
|
void registerFileServer();
|
||||||
//esp_err_t hello_get_handler(httpd_req_t *req);
|
//esp_err_t hello_get_handler(httpd_req_t *req);
|
||||||
|
@@ -4,6 +4,9 @@
|
|||||||
#include "WebInterface.hpp"
|
#include "WebInterface.hpp"
|
||||||
#include "RFIDInterface.hpp"
|
#include "RFIDInterface.hpp"
|
||||||
#include "AudioPlayer.hpp"
|
#include "AudioPlayer.hpp"
|
||||||
|
#include "TagPlayerControl.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void app_main();
|
void app_main();
|
||||||
@@ -16,14 +19,21 @@ RFIDInterface rfid;
|
|||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
initArduino();
|
initArduino();
|
||||||
|
|
||||||
networking.connectWifi();
|
webInterface.initSD();
|
||||||
|
|
||||||
|
/* networking.connectWifi();
|
||||||
webInterface.start();
|
webInterface.start();
|
||||||
|
|
||||||
rfid.registerWebResources(&webInterface);
|
rfid.registerWebResources(&webInterface);
|
||||||
webInterface.finishResourceRegistrations();
|
webInterface.finishResourceRegistrations();
|
||||||
|
*/
|
||||||
AudioPlayer audioPlayer; // initialize here, because the ctor does init stuff that fails when executed before `app_main()`
|
AudioPlayer audioPlayer; // initialize here, because the ctor does init stuff that fails when executed before `app_main()`
|
||||||
audioPlayer.play("file://sdcard/howcanwehangontoadream.mp3");
|
|
||||||
|
|
||||||
rfid.start();
|
rfid.start();
|
||||||
|
|
||||||
|
TagPlayerControl tagPlayerControl(&audioPlayer);
|
||||||
|
|
||||||
|
rfid.registerTagChangeCallback([&tagPlayerControl](char* tagId) {
|
||||||
|
tagPlayerControl.onTagChanged(tagId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user