1
0
forked from me/IronOS

Add buzzer beep when at temperature 🎵

This commit is contained in:
Ben V. Brown
2021-05-05 19:19:00 +10:00
parent 79fa47eb16
commit 1b3445cefa
3 changed files with 28 additions and 6 deletions

View File

@@ -412,9 +412,19 @@ bool isTipDisconnected() {
return tipDisconnected;
}
void setBuzzer(bool on) {
if (on) {
htim3.Instance->CCR2 = 128;
htim3.Instance->PSC = 100; // drop down into audible range
} else {
htim3.Instance->CCR2 = 0;
htim3.Instance->PSC = 1; // revert back out of hearing range
}
}
void setStatusLED(const enum StatusLED state) {
static enum StatusLED lastState = LED_UNKNOWN;
static TickType_t buzzerEnd = 0;
if (lastState != state || state == LED_HEATING) {
switch (state) {
default:
@@ -426,11 +436,12 @@ void setStatusLED(const enum StatusLED state) {
ws2812.led_set_color(0, 0, 0xFF, 0); // green
break;
case LED_HEATING: {
ws2812.led_set_color(0, ((HAL_GetTick() / 10) % 192) + 64, 0,
0); // Red fade
ws2812.led_set_color(0, ((HAL_GetTick() / 10) % 192) + 64, 0, 0); // Red fade
} break;
case LED_HOT:
ws2812.led_set_color(0, 0xFF, 0, 0); // red
// We have hit the right temp, run buzzer for a short period
buzzerEnd = xTaskGetTickCount() + TICKS_SECOND / 3;
break;
case LED_COOLING_STILL_HOT:
ws2812.led_set_color(0, 0xFF, 0x8C, 0x00); // Orange
@@ -439,4 +450,9 @@ void setStatusLED(const enum StatusLED state) {
ws2812.led_update();
lastState = state;
}
if (state == LED_HOT && xTaskGetTickCount() < buzzerEnd) {
setBuzzer(true);
} else {
setBuzzer(false);
}
}

View File

@@ -31,6 +31,9 @@
#define PWM_Out_Pin GPIO_PIN_6
#define PWM_Out_GPIO_Port GPIOA
#define PWM_Out_CHANNEL TIM_CHANNEL_1
#define BUZZER_Pin GPIO_PIN_7
#define BUZZER_GPIO_Port GPIOA
#define BUZZER_CHANNEL TIM_CHANNEL_2
#define SCL_Pin GPIO_PIN_6
#define SCL_GPIO_Port GPIOB
#define SDA_Pin GPIO_PIN_7

View File

@@ -223,7 +223,9 @@ static void MX_TIM3_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
memset(&sClockSourceConfig, 0, sizeof(sClockSourceConfig));
memset(&sMasterConfig, 0, sizeof(sMasterConfig));
memset(&sConfigOC, 0, sizeof(sConfigOC));
htim3.Instance = TIM3;
htim3.Init.Prescaler = 1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
@@ -248,17 +250,18 @@ static void MX_TIM3_Init(void) {
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, PWM_Out_CHANNEL);
// TODO need to do buzzer
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, BUZZER_CHANNEL);
GPIO_InitTypeDef GPIO_InitStruct;
/**TIM3 GPIO Configuration
PWM_Out_Pin ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = PWM_Out_Pin;
GPIO_InitStruct.Pin = PWM_Out_Pin | BUZZER_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // We would like sharp rising edges
HAL_GPIO_Init(PWM_Out_GPIO_Port, &GPIO_InitStruct);
HAL_TIM_PWM_Start(&htim3, PWM_Out_CHANNEL);
HAL_TIM_PWM_Start(&htim3, BUZZER_CHANNEL);
}
/* TIM3 init function */
static void MX_TIM2_Init(void) {