summaryrefslogtreecommitdiffstats
path: root/apps/buzzer-test/app.c
blob: 65d2356c74db3aab642e9a8dbb8249c1e24c55bc (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <string.h>
#include "watch.h"

typedef struct ApplicationState {
    bool play;
} ApplicationState;

ApplicationState application_state;


void cb_alarm_pressed(void) {
    application_state.play = true;
}

void app_init(void) {
    memset(&application_state, 0, sizeof(application_state));
}

void app_wake_from_backup(void) {
}

void app_setup(void) {
    watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);

    watch_enable_display();

    watch_enable_buzzer();
}

void app_prepare_for_standby(void) {
    watch_display_string("  rains ", 2);
}

void app_wake_from_standby(void) {
}

bool app_loop(void) {
    if (application_state.play) {
        printf("Playing song...\n");
        const BuzzerNote rains[] = {
            BUZZER_NOTE_A4,
            BUZZER_NOTE_F5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_A4,
            BUZZER_NOTE_E5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_A4,
            BUZZER_NOTE_F5,
            BUZZER_NOTE_G5,
            BUZZER_NOTE_E5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_A4,
            BUZZER_NOTE_G5,
            BUZZER_NOTE_F5,
            BUZZER_NOTE_E5,
            BUZZER_NOTE_D5,
            BUZZER_NOTE_E5,
            BUZZER_NOTE_REST,

            BUZZER_NOTE_A5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_A5,
            BUZZER_NOTE_A5SHARP_B5FLAT,
            BUZZER_NOTE_G5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_C5,
            BUZZER_NOTE_A5,
            BUZZER_NOTE_A5SHARP_B5FLAT,
            BUZZER_NOTE_G5,
            BUZZER_NOTE_REST,
            BUZZER_NOTE_D5,
            BUZZER_NOTE_A5SHARP_B5FLAT,
            BUZZER_NOTE_A5,
            BUZZER_NOTE_G5,
            BUZZER_NOTE_F5,
            BUZZER_NOTE_E5,
        };
        const uint16_t durations[] = {
            200,
            600,
            100,
            200,
            600,
            100,
            200,
            400,
            400,
            600,
            100,
            200,
            400,
            400,
            400,
            400,
            800,
            600,

            200,
            50,
            400,
            200,
            400,
            100,
            200,
            400,
            400,
            400,
            200,
            200,
            400,
            400,
            400,
            400,
            900,
        };
        application_state.play = false;
        for(size_t i = 0; i < sizeof(rains); i++) {
            char buf[9] = {0};
            if (rains[i] == BUZZER_NOTE_REST) {
                printf("rest for %d ms\n", durations[i]);
                sprintf(buf, "%2drESt  ", i);
            } else {
                printf("playing note %2d: %3.0f Hz for %d ms\n", i, 1000000.0 / (float)NotePeriods[rains[i]], durations[i]);
                sprintf(buf, "%2d%6d", i, NotePeriods[rains[i]]);
            }
            watch_display_string(buf, 2);
            watch_buzzer_play_note(rains[i], durations[i]);
        }
        printf("done!\n\n");
    }

    return true;
}