diff --git a/F0-nolib/Socket_fans/hardware.c b/F0-nolib/Socket_fans/hardware.c index 827dabf..9b5d832 100644 --- a/F0-nolib/Socket_fans/hardware.c +++ b/F0-nolib/Socket_fans/hardware.c @@ -23,8 +23,9 @@ #include "adc.h" #include "hardware.h" +#include "proto.h" -uint8_t ledsON = 0; +buzzer_state buzzer = BUZZER_OFF; // buzzer state void adc_setup(){ uint16_t ctr = 0; // 0xfff0 - more than 1.3ms @@ -187,6 +188,62 @@ void Jump2Boot(){ SysMemBootJump(); } +void buzzer_chk(){ // check buzzer state + static uint32_t lastTms = 0; + static buzzer_state oldstate = BUZZER_OFF; + uint32_t B, S; // length of beep and silence + if(buzzer == oldstate){ // keep current state + if(lastTms > Tms) return; + switch(buzzer){ // beep on/off + case BUZZER_LONG: + B = LONG_BUZZER_PERIOD; + S = LONG_BUZZER_PAUSE; + SEND("long "); + break; + case BUZZER_SHORT: + B = SHORT_BUZZER_PERIOD; + S = SHORT_BUZZER_PAUSE; + SEND("short "); + break; + default: + return; + } + if(CHK(BUZZER)){ // is ON + SEND("1->0\n"); + OFF(BUZZER); + lastTms = Tms + S; + }else{ // is OFF + SEND("0->1\n"); + ON(BUZZER); + lastTms = Tms + B; + } + sendbuf(); + return; + } + SEND("New state: "); + switch(buzzer){ // change buzzer state + case BUZZER_ON: + ON(BUZZER); + SEND("on"); + break; + case BUZZER_OFF: + OFF(BUZZER); + SEND("off"); + break; + case BUZZER_LONG: + ON(BUZZER); + lastTms = Tms + LONG_BUZZER_PERIOD; + SEND("long"); + break; + case BUZZER_SHORT: + ON(BUZZER); + lastTms = Tms + SHORT_BUZZER_PERIOD; + SEND("short"); + break; + } + newline(); sendbuf(); + oldstate = buzzer; +} void exti4_15_isr(){ EXTI->PR |= EXTI_PR_PR7; // clear pending bit diff --git a/F0-nolib/Socket_fans/hardware.h b/F0-nolib/Socket_fans/hardware.h index 8f76024..5f45d2d 100644 --- a/F0-nolib/Socket_fans/hardware.h +++ b/F0-nolib/Socket_fans/hardware.h @@ -61,16 +61,30 @@ #define ON(x) do{pin_set(x ## _port, x ## _pin);}while(0) #define OFF(x) do{pin_clear(x ## _port, x ## _pin);}while(0) +typedef enum{ + BUZZER_OFF, // buzzer is off + BUZZER_ON, // continuous beeep + BUZZER_SHORT, // short beeps with longer pause + BUZZER_LONG // long beeps with shorter pause +} buzzer_state; + +// Length of long and short time periods (ms) +#define LONG_BUZZER_PERIOD (4000) +#define LONG_BUZZER_PAUSE (6000) +#define SHORT_BUZZER_PERIOD (500) +#define SHORT_BUZZER_PAUSE (9500) + extern volatile uint32_t Tms; extern volatile uint32_t Cooler0speed; extern volatile uint32_t Cooler1speed; extern volatile uint32_t Cooler1RPM; -extern uint8_t ledsON; +extern buzzer_state buzzer; void HW_setup(void); void adc_setup(); void iwdg_setup(); void pause_ms(uint32_t pause); void Jump2Boot(); +void buzzer_chk(); #endif // __HARDWARE_H__ diff --git a/F0-nolib/Socket_fans/main.c b/F0-nolib/Socket_fans/main.c index b6d86cd..645f69e 100644 --- a/F0-nolib/Socket_fans/main.c +++ b/F0-nolib/Socket_fans/main.c @@ -93,6 +93,7 @@ int main(void){ IWDG->KR = IWDG_REFRESH; cmd_parser(txt); } + buzzer_chk(); } return 0; } diff --git a/F0-nolib/Socket_fans/proto.c b/F0-nolib/Socket_fans/proto.c index eb899e8..e9fd16e 100644 --- a/F0-nolib/Socket_fans/proto.c +++ b/F0-nolib/Socket_fans/proto.c @@ -203,6 +203,30 @@ static inline void changeRPM(char *str){ } } +static inline void buzzercmd(char cmd){ + SEND("Buzzer: "); + switch(cmd){ + case '0': + buzzer = BUZZER_OFF; + SEND("off"); + break; + case '1': + buzzer = BUZZER_ON; + SEND("on"); + break; + case 'l': + buzzer = BUZZER_LONG; + SEND("long beeps"); + break; + case 's': + buzzer = BUZZER_SHORT; + SEND("short beeps"); + break; + default: + SEND("wrong command"); + } +} + /** * @brief cmd_parser - command parsing * @param txt - buffer with commands & data @@ -226,6 +250,10 @@ void cmd_parser(char *txt){ ADCget(txt[1]); goto eof; break; + case 'b': // buzzer + buzzercmd(txt[1]); + goto eof; + break; case 'C': // Cooler RPM coolerRPM(txt[1]); goto eof; @@ -290,6 +318,7 @@ void cmd_parser(char *txt){ "'Ax' - get ADC raw value (x=0..7)\n" "'a' - show current value of ADC1->DR\n" "'B' - buttons' state\n" + "'bx' - buzzer: x==0 - off, x==1 - on, x==l - long beeps, x==s - short beeps\n" "'Cx' - get cooler x (0/1) RPM\n" "'D' - activate DFU mode\n" "'M' - get MCU temperature\n"