Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f47b846

Browse files
committedJun 6, 2024
zephyrCommon: Add tone and noTone
- Add implementation of tone and noTone - Currently duration not implemented Signed-off-by: Ayush Singh <[email protected]>
1 parent 347a80d commit f47b846

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎cores/arduino/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int main(void) {
1212
for (;;) {
1313
loop();
1414
if (arduino::serialEventRun) arduino::serialEventRun();
15+
k_yield();
1516
}
1617

1718
return 0;

‎cores/arduino/zephyrCommon.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ PinStatus digitalRead(pin_size_t pinNumber) {
201201
return (gpio_pin_get_dt(&arduino_pins[pinNumber]) == 1) ? HIGH : LOW;
202202
}
203203

204+
struct k_timer arduino_pin_timers[ARRAY_SIZE(arduino_pins)];
205+
206+
void tone_expiry_cb(struct k_timer *timer) {
207+
const struct gpio_dt_spec *spec = (gpio_dt_spec*)k_timer_user_data_get(timer);
208+
gpio_pin_toggle_dt(spec);
209+
}
210+
211+
void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
212+
struct k_timer *timer = &arduino_pin_timers[pinNumber];
213+
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
214+
k_timeout_t timeout = K_NSEC(NSEC_PER_SEC / (2 * frequency));
215+
216+
k_timer_init(timer, tone_expiry_cb, NULL);
217+
k_timer_user_data_set(timer, (void*)spec);
218+
gpio_pin_set_dt(spec, 1);
219+
k_timer_start(timer, timeout, timeout);
220+
}
221+
222+
void noTone(pin_size_t pinNumber) {
223+
k_timer_stop(&arduino_pin_timers[pinNumber]);
224+
gpio_pin_set_dt(&arduino_pins[pinNumber], 0);
225+
}
226+
204227
void delay(unsigned long ms) { k_sleep(K_MSEC(ms)); }
205228

206229
void delayMicroseconds(unsigned int us) { k_sleep(K_USEC(us)); }

0 commit comments

Comments
 (0)
Please sign in to comment.