-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzer.c
80 lines (66 loc) · 2.11 KB
/
buzzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "buzzer.h"
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/mutex.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/gpio.h>
void buzzer_main(void *, void *, void *);
static K_THREAD_DEFINE(buzzer, BUZZER_THREAD_STACK_SIZE,
buzzer_main, NULL, NULL, NULL, BUZZER_THREAD_PRIORITY, 0, 0);
// Defined and initialised be the above macro.
extern const k_tid_t display_thread_id;
static const struct gpio_dt_spec buzzer_pin = GPIO_DT_SPEC_GET(DT_NODELABEL(rfid_buzzer), gpios);
struct buzzer_queue_item {
void *fifo_reserved;
enum BuzzKind kind;
};
K_FIFO_DEFINE(buzzer_queue);
K_MEM_SLAB_DEFINE(buzzer_slab, (sizeof(struct buzzer_queue_item) * 8), 1, sizeof(void*));
void buzzer_send(enum BuzzKind kind)
{
struct buzzer_queue_item *item;
int error = k_mem_slab_alloc(&buzzer_slab, (void**)&item, K_NO_WAIT);
if (error != 0) {
// We don't want to wait for the buzzer thread; it's not that important
return;
}
item->kind = kind;
k_fifo_put(&buzzer_queue, item);
}
void buzzer_main(void *, void *, void *)
{
gpio_pin_configure_dt(&buzzer_pin, GPIO_OUTPUT_INACTIVE);
for (;;) {
struct buzzer_queue_item *item = k_fifo_get(&buzzer_queue, K_FOREVER);
switch (item->kind) {
case STARTUP:
// BEEEEP
gpio_pin_set_dt(&buzzer_pin, 1);
k_msleep(500);
gpio_pin_set_dt(&buzzer_pin, 0);
break;
case READ_OK:
case TURN_OK:
case TURN_END:
// BEEP
gpio_pin_set_dt(&buzzer_pin, 1);
k_msleep(300);
gpio_pin_set_dt(&buzzer_pin, 0);
break;
case READ_ERROR:
case TURN_ERROR:
// BI-BIP
gpio_pin_set_dt(&buzzer_pin, 1);
k_msleep(50);
gpio_pin_set_dt(&buzzer_pin, 0);
k_msleep(50);
gpio_pin_set_dt(&buzzer_pin, 1);
k_msleep(50);
gpio_pin_set_dt(&buzzer_pin, 0);
break;
}
k_mem_slab_free(&buzzer_slab, item);
k_msleep(100);
}
}