👋 better coded, added notes and mario ringtones

develop
Pablo Jean 3 years ago
parent cf4035fc2d
commit 0b2fa88806

@ -80,7 +80,10 @@ void __attribute__((weak)) buzzer_end_callback(buzzer_t *buzzer){
void buzzer_interrupt(buzzer_t *buzzer){
uint16_t i;
buzzer->counting += buzzer->baseTimeMs;
// if (buzzer->started == 0){
// return;
// }
buzzer->counting += buzzer->interruptMs;
if (buzzer->active &&
buzzer->play_param.len > 0 &&
buzzer->counting > buzzer->play_param.time){
@ -120,30 +123,29 @@ void buzzer_interrupt(buzzer_t *buzzer){
}
}
/*
* Default values:
* baseTimeMs = 1;
* type = BUZZER_TYPE_PASSIVE;
*
* the rest is zero
*/
void buzzer_init_params(buzzer_t *buzzer){
if (buzzer != NULL){
memset(buzzer, 0, sizeof(buzzer_t));
buzzer->baseTimeMs = 1;
buzzer->type = BUZZER_TYPE_PASSIVE;
}
}
void buzzer_init(buzzer_t *buzzer){
buzzer_err_e buzzer_init(buzzer_t *buzzer){
if (buzzer != NULL){
// nothing to do here
// nothing to do here yet
if (buzzer->fnx.gpioOut){
buzzer->type = BUZZER_TYPE_ACTIVE;
buzzer->fnx.gpioOut(0);
return BUZZER_ERR_OK;
}
else if (buzzer->fnx.pwmOut){
buzzer->type = BUZZER_TYPE_PASSIVE;
buzzer->fnx.pwmOut(0);
return BUZZER_ERR_OK;
}
}
return BUZZER_ERR_PARAMS;
}
void buzzer_stop(buzzer_t *buzzer){
if (buzzer != NULL){
buzzer->active = 0;
buzzer->active = BUZZER_IS_NOT_ACTIVE;
if (buzzer->type == BUZZER_TYPE_ACTIVE){
__buzzer_stop_gpio(buzzer);
}
@ -155,7 +157,7 @@ void buzzer_stop(buzzer_t *buzzer){
void buzzer_turn_on(buzzer_t *buzzer, uint16_t freq){
if (buzzer != NULL){
buzzer->active = 1;
buzzer->active = BUZZER_IS_ACTIVE;
buzzer->play_param.loop = 0;
buzzer->play_param.len = 0;
if (buzzer->type == BUZZER_TYPE_ACTIVE){
@ -168,14 +170,14 @@ void buzzer_turn_on(buzzer_t *buzzer, uint16_t freq){
}
}
void buzzer_start(buzzer_t *buzzer, uint16_t freq, uint16_t period, uint8_t loop){
void buzzer_start(buzzer_t *buzzer, uint16_t freq, uint16_t period, buzzer_loop_e loop){
if (buzzer != NULL){
buzzer->play_param.i = 0;
buzzer->play_param.time = period;
buzzer->play_param.loop = loop;
buzzer->play_param.pTimes = NULL;
buzzer->play_param.pFreq = NULL;
buzzer->active = 1;
buzzer->active = BUZZER_IS_ACTIVE;
buzzer->play_param.len = 2;
if (buzzer->type == BUZZER_TYPE_ACTIVE){
__buzzer_start_gpio(buzzer);
@ -190,10 +192,11 @@ void buzzer_start(buzzer_t *buzzer, uint16_t freq, uint16_t period, uint8_t loop
void buzzer_start_array(buzzer_t *buzzer, uint16_t *pPeriod, uint16_t *pFreq, uint16_t len){
if (buzzer != NULL && pPeriod != NULL){
buzzer->play_param.len = len;
buzzer->play_param.i = 0;
buzzer->play_param.pTimes = pPeriod;
buzzer->play_param.pFreq = pFreq;
buzzer->play_param.loop = 0;
buzzer->active = 1;
buzzer->play_param.loop = BUZZER_LOOP_OFF;
buzzer->active = BUZZER_IS_ACTIVE;
if (buzzer->type == BUZZER_TYPE_ACTIVE){
__buzzer_start_array_gpio(buzzer);
}
@ -203,7 +206,7 @@ void buzzer_start_array(buzzer_t *buzzer, uint16_t *pPeriod, uint16_t *pFreq, ui
}
}
uint8_t buzzer_is_active(buzzer_t *buzzer){
buzzer_active_e buzzer_is_active(buzzer_t *buzzer){
if (buzzer != NULL){
return buzzer->active;
}

@ -12,6 +12,9 @@
#include <stddef.h>
#include <string.h>
#include "notes.h"
#include "ringtones.h"
/*
* Enumerates
*/
@ -21,6 +24,24 @@ typedef enum{
BUZZER_TYPE_ACTIVE
}buzzer_type_e;
typedef enum{
BUZZER_LOOP_OFF,
BUZZER_LOOP_ON
}buzzer_loop_e;
typedef enum{
BUZZER_IS_NOT_ACTIVE,
BUZZER_IS_ACTIVE
}buzzer_active_e;
typedef enum{
BUZZER_ERR_OK,
BUZZER_ERR_FAIL,
BUZZER_ERR_PARAMS,
BUZZER_ERR_UNKNOWN = 0xFF
}buzzer_err_e;
/*
* Functions typedefs
*/
@ -34,20 +55,21 @@ typedef void (*gpioOutFx)(uint32_t val);
*/
typedef struct{
uint8_t started;
buzzer_type_e type;
uint_fast8_t active;
uint_fast16_t baseTimeMs;
buzzer_active_e active;
uint_fast16_t interruptMs;
uint_fast16_t counting;
struct{
uint_fast16_t *pTimes;
uint_fast16_t *pFreq;
uint16_t *pTimes;
uint16_t *pFreq;
uint_fast16_t i;
uint_fast16_t len;
int_fast32_t time;
uint_fast16_t freq;
uint_fast8_t loop;
buzzer_loop_e loop;
}play_param;
struct{
pwmOutFx pwmOut;
@ -59,24 +81,46 @@ typedef struct{
* Functions Prototypes
*/
void buzzer_init_params(buzzer_t *buzzer);
void buzzer_init(buzzer_t *buzzer);
/**
* Initialize the buzzer
*/
buzzer_err_e buzzer_init(buzzer_t *buzzer);
/**
* Stop buzzer immediately
*/
void buzzer_stop(buzzer_t *buzzer);
/**
* Turnon buzzer immediately
*/
void buzzer_turn_on(buzzer_t *buzzer, uint16_t freq);
void buzzer_start(buzzer_t *buzzer, uint16_t freq, uint16_t period, uint8_t loop);
/**
* Start buzzer for a determined period
* If loop is BUZZER_LOOP_ON the buzzer will turn on and turn off
* with the period
* buzzer_interrupt must be working
*/
void buzzer_start(buzzer_t *buzzer, uint16_t freq, uint16_t period, buzzer_loop_e loop);
/**
* Start to play a array of period and frequencies, generally used to play ringtones
* buzzer_interrupt must be working
*/
void buzzer_start_array(buzzer_t *buzzer, uint16_t *pPeriod, uint16_t *pFreq, uint16_t len);
uint8_t buzzer_is_active(buzzer_t *buzzer);
/**
* Return if Buzzer is active
*/
buzzer_active_e buzzer_is_active(buzzer_t *buzzer);
// call this function in a periodic timing
// call this function in a periodic timing, if you're using a RTOS
// we suggest you to use a task to handle this with a sleep
// the period of interrupt is configured on buzzer_t structure
void buzzer_interrupt(buzzer_t *buzzer);
// callback
// callback when an array has your execution finished
void buzzer_end_callback(buzzer_t *buzzer);

@ -0,0 +1,106 @@
/*
* notes.h
*
* Created on: Nov 28, 2022
* Author: pablo
*/
#ifndef BUZZER_NOTES_H_
#define BUZZER_NOTES_H_
#include <stdint.h>
// notes and your frequencies
#define NOTE_OFF 0
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#endif /* BUZZER_NOTES_H_ */

@ -0,0 +1,111 @@
/*
* ringtones.c
*
* Created on: Nov 28, 2022
* Author: pablo
*/
#include "ringtones.h"
uint16_t mario_theme_melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0
};
uint16_t mario_theme_time[] = {
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
90, 90, 90,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
90, 90, 90,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
};
uint16_t mario_theme_len = sizeof(mario_theme_time)/sizeof(uint16_t);
//Underworld melody
uint16_t underworld_melody[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_CS4, NOTE_D4,
NOTE_CS4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4, NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0
};
uint16_t underworld_time[] = {
120, 120, 120, 120,
120, 120, 60,
30,
120, 120, 120, 120,
120, 120, 60,
30,
120, 120, 120, 120,
120, 120, 60,
30,
120, 120, 120, 120,
120, 120, 60,
60, 180, 180, 180,
60, 60,
60, 60,
60, 60,
180, 180, 180, 180, 180, 180,
100, 100, 100,
100, 100, 100,
30, 30, 30
};
uint16_t underworld_len = sizeof(underworld_time)/sizeof(uint16_t);

@ -0,0 +1,25 @@
/*
* ringtones.h
*
* Created on: Nov 28, 2022
* Author: pablo
*/
#ifndef BUZZER_RINGTONES_H_
#define BUZZER_RINGTONES_H_
#include <stdint.h>
#include "notes.h"
// mario mais theme
extern uint16_t mario_theme_melody[];
extern uint16_t mario_theme_time[];
extern uint16_t mario_theme_len;
// Underworld melody
extern uint16_t underworld_melody[];
extern uint16_t underworld_time[];
extern uint16_t underworld_len;
#endif /* BUZZER_RINGTONES_H_ */
Loading…
Cancel
Save