timeouts: Port BBC micro:bit to the new timeout API

Port the BBC micro:bit display driver to the new timeout API exposed by
the kernel.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
Carles Cufi 2020-04-30 22:48:32 +02:00 committed by Carles Cufí
parent e052c7991c
commit 9b04a99243
10 changed files with 50 additions and 45 deletions

View file

@ -8,7 +8,6 @@ config MICROBIT_DISPLAY
depends on BOARD_BBC_MICROBIT
depends on PRINTK
depends on GPIO
select LEGACY_TIMEOUT_API
help
Enable this to be able to display images and text on the 5x5
LED matrix display on the BBC micro:bit.

View file

@ -79,7 +79,7 @@
#define SCROLL_OFF 0
#define SCROLL_START 1
#define SCROLL_DEFAULT_DURATION K_MSEC(80)
#define SCROLL_DEFAULT_DURATION_MS 80
struct mb_display {
struct device *dev; /* GPIO device */
@ -160,8 +160,8 @@ static void start_image(struct mb_display *disp, const struct mb_image *img)
disp->cur = 0U;
if (disp->duration == K_FOREVER) {
disp->expiry = K_FOREVER;
if (disp->duration == SYS_FOREVER_MS) {
disp->expiry = SYS_FOREVER_MS;
} else {
disp->expiry = k_uptime_get() + disp->duration;
}
@ -308,7 +308,7 @@ static void show_row(struct k_timer *timer)
update_pins(disp, disp->row[disp->cur]);
disp->cur = (disp->cur + 1) % DISPLAY_ROWS;
if (disp->cur == 0U && disp->expiry != K_FOREVER &&
if (disp->cur == 0U && disp->expiry != SYS_FOREVER_MS &&
k_uptime_get() > disp->expiry) {
if (disp->scroll) {
update_scroll(disp);
@ -335,7 +335,7 @@ static void start_scroll(struct mb_display *disp, s32_t duration)
if (duration) {
disp->duration = duration / scroll_steps(disp);
} else {
disp->duration = SCROLL_DEFAULT_DURATION;
disp->duration = SCROLL_DEFAULT_DURATION_MS;
}
disp->scroll = SCROLL_START;

View file

@ -124,7 +124,8 @@ struct mb_display *mb_display_get(void);
*
* @param disp Display object.
* @param mode One of the MB_DISPLAY_MODE_* options.
* @param duration Duration how long to show each image (in milliseconds).
* @param duration Duration how long to show each image (in milliseconds), or
* @ref SYS_FOREVER_MS.
* @param img Array of image bitmaps (struct mb_image objects).
* @param img_count Number of images in 'img' array.
*/
@ -143,7 +144,8 @@ void mb_display_image(struct mb_display *disp, u32_t mode, s32_t duration,
*
* @param disp Display object.
* @param mode One of the MB_DISPLAY_MODE_* options.
* @param duration Duration how long to show each character (in milliseconds).
* @param duration Duration how long to show each character (in milliseconds),
* or @ref SYS_FOREVER_MS.
* @param fmt printf-style format string
* @param ... Optional list of format arguments.
*/

View file

@ -22,8 +22,8 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
{
struct mb_display *disp = mb_display_get();
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, K_MSEC(500),
"%04u", oob_number);
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, 500, "%04u",
oob_number);
}
static void configure_button(void)
@ -57,7 +57,8 @@ void board_output_number(bt_mesh_output_action_t action, u32_t number)
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
GPIO_INT_EDGE_TO_ACTIVE);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, K_FOREVER, &arrow, 1);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, SYS_FOREVER_MS, &arrow,
1);
}
void board_prov_complete(void)
@ -73,7 +74,7 @@ void board_prov_complete(void)
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
GPIO_INT_DISABLE);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, K_SECONDS(10),
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, 10 * MSEC_PER_SEC,
&arrow, 1);
}
@ -94,7 +95,7 @@ void board_init(void)
};
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_SECONDS(1), blink, ARRAY_SIZE(blink));
1 * MSEC_PER_SEC, blink, ARRAY_SIZE(blink));
configure_button();
}

View file

@ -20,7 +20,7 @@
#include "board.h"
#define SCROLL_SPEED K_MSEC(300)
#define SCROLL_SPEED 300
#define BUZZER_PIN EXT_P0_GPIO_PIN
#define BEEP_DURATION K_MSEC(60)
@ -53,10 +53,10 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
if (target > 0x0009) {
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE,
K_SECONDS(2), "A");
2 * MSEC_PER_SEC, "A");
} else {
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE,
K_SECONDS(2), "%X", (target & 0xf));
2 * MSEC_PER_SEC, "%X", (target & 0xf));
}
}
}
@ -172,7 +172,7 @@ void board_heartbeat(u8_t hops, u16_t feat)
if (hops) {
hops = MIN(hops, ARRAY_SIZE(hops_img));
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2),
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, 2 * MSEC_PER_SEC,
&hops_img[hops - 1], 1);
}
}
@ -183,8 +183,8 @@ void board_other_dev_pressed(u16_t addr)
printk("board_other_dev_pressed(0x%04x)\n", addr);
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2),
"%X", (addr & 0xf));
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE, 2 * MSEC_PER_SEC, "%X",
(addr & 0xf));
}
void board_attention(bool attention)
@ -216,7 +216,7 @@ void board_attention(bool attention)
if (attention) {
mb_display_image(disp,
MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_MSEC(150), attn_img, ARRAY_SIZE(attn_img));
150, attn_img, ARRAY_SIZE(attn_img));
} else {
mb_display_stop(disp);
}

View file

@ -72,7 +72,7 @@ void main(void)
/* Display countdown from '9' to '0' */
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE,
K_SECONDS(1), "9876543210");
1 * MSEC_PER_SEC, "9876543210");
k_sleep(K_SECONDS(11));
/* Iterate through all pixels one-by-one */
@ -80,28 +80,28 @@ void main(void)
for (x = 0; x < 5; x++) {
struct mb_image pixel = {};
pixel.row[y] = BIT(x);
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE,
K_MSEC(250), &pixel, 1);
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, 250,
&pixel, 1);
k_sleep(K_MSEC(300));
}
}
/* Show a smiley-face */
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2),
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, 2 * MSEC_PER_SEC,
&smiley, 1);
k_sleep(K_SECONDS(2));
/* Show a short scrolling animation */
mb_display_image(disp, MB_DISPLAY_MODE_SCROLL, K_SECONDS(1),
mb_display_image(disp, MB_DISPLAY_MODE_SCROLL, 1 * MSEC_PER_SEC,
scroll, ARRAY_SIZE(scroll));
k_sleep(K_SECONDS(1) * (ARRAY_SIZE(scroll) + 2));
k_sleep(K_SECONDS((ARRAY_SIZE(scroll) + 2)));
/* Show a sequential animation */
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_MSEC(150), animation, ARRAY_SIZE(animation));
k_sleep(K_MSEC(150) * ARRAY_SIZE(animation) * 5);
150, animation, ARRAY_SIZE(animation));
k_sleep(K_MSEC(150 * ARRAY_SIZE(animation) * 5));
/* Show some scrolling text ("Hello Zephyr!") */
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_MSEC(500), "Hello Zephyr!");
500, "Hello Zephyr!");
}

View file

@ -395,12 +395,12 @@ static u32_t adv_timeout(void)
u32_t timeout;
if (bt_rand(&timeout, sizeof(timeout)) < 0) {
return K_SECONDS(10);
return 10 * MSEC_PER_SEC;
}
timeout %= K_SECONDS(10);
timeout %= (10 * MSEC_PER_SEC);
return timeout + K_SECONDS(1);
return timeout + (1 * MSEC_PER_SEC);
}
static void cancel_connect(void)
@ -474,7 +474,7 @@ static void ble_timeout(struct k_work *work)
printk("Advertising successfully started\n");
ble_state = BLE_ADVERTISING;
k_delayed_work_submit(&ble_work, adv_timeout());
k_delayed_work_submit(&ble_work, K_MSEC(adv_timeout()));
break;
case BLE_ADVERTISING:
printk("Timed out advertising\n");

View file

@ -25,7 +25,7 @@
* system where top-left is (0, 0) and bottom-right is (49, 49).
*/
#define SCROLL_SPEED K_MSEC(400) /* Text scrolling speed */
#define SCROLL_SPEED 400 /* Text scrolling speed */
#define PIXEL_SIZE 10 /* Virtual coordinates per real pixel */
@ -42,8 +42,10 @@
#define BALL_POS_Y_MIN 0 /* Maximum ball Y coordinate */
#define BALL_POS_Y_MAX 39 /* Maximum ball Y coordinate */
#define START_THRESHOLD K_MSEC(100) /* Max time between A & B press */
#define RESTART_THRESHOLD K_SECONDS(2) /* Time before restart is allowed */
#define START_THRESHOLD 100 /* Max time between A & B press */
#define RESTART_THRESHOLD (2 * MSEC_PER_SEC) /* Time before restart is
* allowed
*/
#define REAL_TO_VIRT(r) ((r) * 10)
#define VIRT_TO_REAL(v) ((v) / 10)
@ -295,7 +297,7 @@ static void game_ended(bool won)
printk("You lost!\n");
}
k_delayed_work_submit(&refresh, RESTART_THRESHOLD);
k_delayed_work_submit(&refresh, K_MSEC(RESTART_THRESHOLD));
}
static void game_stack_dump(const struct k_thread *thread, void *user_data)
@ -396,13 +398,13 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
/* Filter out spurious presses */
if (pins & BIT(DT_GPIO_PIN(DT_ALIAS(sw0), gpios))) {
printk("A pressed\n");
if (k_uptime_delta(&a_timestamp) < K_MSEC(100)) {
if (k_uptime_delta(&a_timestamp) < 100) {
printk("Too quick A presses\n");
return;
}
} else {
printk("B pressed\n");
if (k_uptime_delta(&b_timestamp) < K_MSEC(100)) {
if (k_uptime_delta(&b_timestamp) < 100) {
printk("Too quick B presses\n");
return;
}
@ -538,6 +540,6 @@ void main(void)
}
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE,
K_FOREVER, &img, 1);
SYS_FOREVER_MS, &img, 1);
}
}

View file

@ -72,7 +72,7 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
printk("Period is %u us (%u Hz)\n", period, US_TO_HZ(period));
disp = mb_display_get();
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, K_MSEC(500), "%uHz",
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, 500, "%uHz",
US_TO_HZ(period));
k_work_submit(&beep_work);

View file

@ -22,7 +22,7 @@ static void button_pressed(struct device *dev, struct gpio_callback *cb,
{
struct mb_display *disp = mb_display_get();
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, K_MSEC(500),
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, 500,
"OOB Number: %u", oob_number);
}
@ -58,7 +58,8 @@ void board_output_number(bt_mesh_output_action_t action, u32_t number)
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
GPIO_INT_EDGE_TO_ACTIVE);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, K_FOREVER, &arrow, 1);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, SYS_FOREVER_MS, &arrow,
1);
}
void board_prov_complete(void)
@ -74,7 +75,7 @@ void board_prov_complete(void)
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
GPIO_INT_DISABLE);
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, K_SECONDS(10),
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT, 10 * MSEC_PER_SEC,
&arrow, 1);
}
@ -95,7 +96,7 @@ void board_init(void)
};
mb_display_image(disp, MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_SECONDS(1), blink, ARRAY_SIZE(blink));
1 * MSEC_PER_SEC, blink, ARRAY_SIZE(blink));
configure_button();
}