a barely functional driver. hooray!

This commit is contained in:
Morgan 'ARR\!' Allen 2021-10-14 22:13:37 -07:00
commit 7ec406c5af
3 changed files with 143 additions and 0 deletions

5
CMakeLists.txt Normal file
View file

@ -0,0 +1,5 @@
idf_component_register(SRCS
"main/dfplayermini.c"
INCLUDE_DIRS "include"
)

8
include/dfplayermini.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef __DFPLAYERMINI_H_
#define __DFPLAYERMINI_H_
#include <stdint.h>
uint8_t dfplayermini_init();
#endif

130
main/dfplayermini.c Normal file
View file

@ -0,0 +1,130 @@
#include "stdint.h"
#include "driver/uart.h"
#include "dfplayermini.h"
#define BUF_SIZE (1024)
#define PACKET_LEN (8)
#define BYTE_START (0x7E)
#define BYTE_END (0xEF)
struct DFPlayerMini {
uart_config_t *uart_config;
} dfplayermini;
/*
* 0x7E START
* 0xFF VER
* 0x06 LEN
* 0x00 ACK
* 0x01 CMD
* 0x00 PARAM-hi
* 0x00 PARAM-lo
* 0x00 CHECK-hi
* 0x00 CHECK-lo
* 0xEF FIN
*/
uint8_t packet[] = { 0x7E, 0xFF, 0x06, 0x00, 0x01, 0x00, 0x00, 0xEF };
uint8_t send_packet(uint8_t cmd, uint8_t arg_low, uint8_t arg_high) {
uint8_t i = 0;
uint16_t sum = 0;
packet[3] = cmd;
packet[4] = 1;
packet[5] = arg_high;
packet[6] = arg_low;
for(i = 1; i < 7; i++) {
sum += packet[i];
}
//packet[7] = -sum << 8;
//packet[8] = -sum;
return uart_write_bytes(UART_NUM_1, (const char*)&packet, PACKET_LEN);
}
uint8_t set_volume(uint8_t vol) {
return send_packet(0x06, vol, 0);
}
void dfplayermini_uart_read_task(void *arg) {
setvbuf(stdout, NULL, _IONBF, 0);
uint8_t buf[PACKET_LEN];
uint8_t err;
uint8_t idx = 0;
uint8_t inited = 0;
uint8_t timeout = 1;
while(true) {
int len = uart_read_bytes(UART_NUM_1, &buf[idx], 1, 20 / portTICK_RATE_MS);
if(len == 0) {
if(inited == 0 && timeout++ == 0) {
printf("timeout, sending reset\n");
send_packet(0x0C, 0, 0);
}
continue;
}
if(buf[0] == BYTE_START) {
printf("0x%02X ", buf[idx]);
idx++;
//printf("got start byte\n");
}
if(buf[idx - 1] == BYTE_END) {
printf("\n");
switch(buf[3]) {
case 0x3F:
printf("player ready\n");
inited = 1;
//send_packet(0x0C, 0, 0);
//vTaskDelay(10 / portTICK_RATE_MS);
//if(!(err = send_packet(0x09, 1, 0))) {
//printf("Failed to set play source: %d\n", err);
//}
if(!(err = set_volume(10))) {
printf("Failed to set volume: %d\n", err);
}
vTaskDelay(100 / portTICK_RATE_MS);
send_packet(0x03, 0, 0);
break;
}
idx = 0;
}
}
}
uint8_t dfplayermini_init() {
uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
dfplayermini.uart_config = &uart_config;
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, dfplayermini.uart_config);
uart_set_pin(UART_NUM_1, 13, 34, -1, -1);
xTaskCreate(dfplayermini_uart_read_task, "dfplayermini_uart_read_task", 2048, NULL, 10, NULL);
return 0;
}