Skip to content

Commit 423f8b8

Browse files
committed
sys/hmi: refactor status light indication
This moves most status light indication code out of platform/sys.c into sys/hmi.c so that platforms can share code. This required introducing a SYS_CONFIG_STATUS_LIGHT parameter since NXT does not have a status light. Additionally, Bluetooth advertising and low voltage indications are implemented. Other indications are not implemented yet. This also fixes the status light not turning on at power up on SPIKE Prime.
1 parent 22da7fd commit 423f8b8

File tree

18 files changed

+244
-82
lines changed

18 files changed

+244
-82
lines changed

lib/pbio/drv/city_hub/bluetooth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <pbio/error.h>
1616
#include <pbio/event.h>
1717
#include <pbio/util.h>
18+
#include <pbsys/status.h>
1819
#include <pbsys/sys.h>
1920

2021
#include <contiki.h>
@@ -1184,7 +1185,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
11841185
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
11851186

11861187
// TODO: we should have a timeout and stop scanning eventually
1188+
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
11871189
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION);
1190+
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
11881191

11891192
etimer_set(&timer, clock_from_msec(500));
11901193

lib/pbio/drv/cplus_hub/bluetooth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <pbio/error.h>
1616
#include <pbio/event.h>
1717
#include <pbio/util.h>
18+
#include <pbsys/status.h>
1819
#include <pbsys/sys.h>
1920

2021
#include <contiki.h>
@@ -1181,7 +1182,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
11811182
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
11821183

11831184
// TODO: we should have a timeout and stop scanning eventually
1185+
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
11841186
PROCESS_WAIT_UNTIL(conn_handle != NO_CONNECTION);
1187+
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
11851188

11861189
etimer_set(&timer, clock_from_msec(500));
11871190

lib/pbio/drv/move_hub/bluetooth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "pbio/config.h"
1616
#include "pbio/error.h"
1717
#include "pbio/event.h"
18+
#include "pbsys/status.h"
1819
#include "pbsys/sys.h"
1920
#include "../../src/processes.h"
2021

@@ -663,7 +664,9 @@ PROCESS_THREAD(pbdrv_bluetooth_hci_process, ev, data) {
663664
PROCESS_PT_SPAWN(&child_pt, set_discoverable(&child_pt));
664665

665666
// TODO: we should have a timeout and stop scanning eventually
667+
pbsys_status_set(PBSYS_STATUS_BLE_ADVERTISING);
666668
PROCESS_WAIT_UNTIL(conn_handle);
669+
pbsys_status_clear(PBSYS_STATUS_BLE_ADVERTISING);
667670

668671
etimer_set(&timer, clock_from_msec(500));
669672

lib/pbio/include/pbsys/config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
// System capability configuration options
5+
6+
#ifndef _PBSYS_CONFIG_H_
7+
#define _PBSYS_CONFIG_H_
8+
9+
#include "pbsysconfig.h"
10+
11+
// When set to (1) PBSYS_CONFIG_STATUS_LIGHT indicates that a hub has a hub status light
12+
#ifndef PBSYS_CONFIG_STATUS_LIGHT
13+
#error "Must define PBSYS_CONFIG_STATUS_LIGHT in pbsysconfig.h"
14+
#endif
15+
16+
#endif // _PBSYS_CONFIG_H_
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
#define PBSYS_CONFIG_STATUS_LIGHT (1)

lib/pbio/platform/city_hub/sys.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <contiki.h>
55

66
#include "pbdrv/bluetooth.h"
7-
#include "pbdrv/config.h"
8-
#include "pbdrv/led.h"
97

108
#include "pbio/color.h"
119
#include "pbio/event.h"
@@ -42,17 +40,14 @@ void pbsys_prepare_user_program(const pbsys_user_program_callbacks_t *callbacks)
4240
user_stop_func = NULL;
4341
user_stdin_event_func = NULL;
4442
}
45-
_pbio_light_set_user_mode(true);
4643
pbio_light_on_with_pattern(PBIO_PORT_SELF, PBIO_COLOR_GREEN, PBIO_LIGHT_PATTERN_BREATHE);
44+
pbsys_status_set(PBSYS_STATUS_USER_PROGRAM_RUNNING);
4745
}
4846

4947
void pbsys_unprepare_user_program(void) {
48+
pbsys_status_clear(PBSYS_STATUS_USER_PROGRAM_RUNNING);
5049
user_stop_func = NULL;
5150
user_stdin_event_func = NULL;
52-
_pbio_light_set_user_mode(false);
53-
pbdrv_led_dev_t *led;
54-
pbdrv_led_get_dev(0, &led);
55-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
5651
_pbio_motorpoll_reset_all();
5752
}
5853

@@ -77,11 +72,6 @@ static void init(void) {
7772
IWDG->RLR = 1875; // 40kHz / 64 / 1875 = 0.33.. Hz => 3 second timeout
7873
IWDG->KR = 0xaaaa; // refresh counter
7974
IWDG->KR = 0xcccc; // start watchdog timer
80-
81-
_pbio_light_set_user_mode(false);
82-
pbdrv_led_dev_t *led;
83-
pbdrv_led_get_dev(0, &led);
84-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
8575
}
8676

8777
static void handle_stdin_char(uint8_t c) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
#define PBSYS_CONFIG_STATUS_LIGHT (1)

lib/pbio/platform/cplus_hub/sys.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <contiki.h>
55

66
#include "pbdrv/bluetooth.h"
7-
#include "pbdrv/config.h"
8-
#include "pbdrv/led.h"
97

108
#include "pbio/color.h"
119
#include "pbio/event.h"
@@ -40,19 +38,14 @@ void pbsys_prepare_user_program(const pbsys_user_program_callbacks_t *callbacks)
4038
user_stop_func = NULL;
4139
user_stdin_event_func = NULL;
4240
}
43-
_pbio_light_set_user_mode(true);
4441
pbio_light_on_with_pattern(PBIO_PORT_SELF, PBIO_COLOR_GREEN, PBIO_LIGHT_PATTERN_BREATHE);
42+
pbsys_status_set(PBSYS_STATUS_USER_PROGRAM_RUNNING);
4543
}
4644

4745
void pbsys_unprepare_user_program(void) {
46+
pbsys_status_clear(PBSYS_STATUS_USER_PROGRAM_RUNNING);
4847
user_stop_func = NULL;
4948
user_stdin_event_func = NULL;
50-
51-
_pbio_light_set_user_mode(false);
52-
pbdrv_led_dev_t *led;
53-
pbdrv_led_get_dev(0, &led);
54-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
55-
5649
_pbio_motorpoll_reset_all();
5750
}
5851

@@ -71,13 +64,6 @@ pbio_error_t pbsys_stdout_put_char(uint8_t c) {
7164
return pbdrv_bluetooth_tx(c);
7265
}
7366

74-
static void init(void) {
75-
_pbio_light_set_user_mode(false);
76-
pbdrv_led_dev_t *led;
77-
pbdrv_led_get_dev(0, &led);
78-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
79-
}
80-
8167
static void handle_stdin_char(uint8_t c) {
8268
uint8_t new_head = (stdin_buf_head + 1) & (STDIN_BUF_SIZE - 1);
8369

@@ -101,7 +87,6 @@ PROCESS_THREAD(pbsys_process, ev, data) {
10187

10288
PROCESS_BEGIN();
10389

104-
init();
10590
pbsys_battery_init();
10691
etimer_set(&timer, clock_from_msec(50));
10792

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
#define PBSYS_CONFIG_STATUS_LIGHT (1)

lib/pbio/platform/debug/sys.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#include <contiki.h>
77

8-
#include "pbdrv/config.h"
9-
#include "pbdrv/led.h"
10-
118
#include "pbio/color.h"
129
#include "pbio/event.h"
1310
#include "pbio/light.h"
@@ -33,18 +30,14 @@ void pbsys_prepare_user_program(const pbsys_user_program_callbacks_t *callbacks)
3330
user_stop_func = NULL;
3431
user_stdin_event_func = NULL;
3532
}
36-
_pbio_light_set_user_mode(true);
3733
pbio_light_on_with_pattern(PBIO_PORT_SELF, PBIO_COLOR_GREEN, PBIO_LIGHT_PATTERN_BREATHE);
34+
pbsys_status_set(PBSYS_STATUS_USER_PROGRAM_RUNNING);
3835
}
3936

4037
void pbsys_unprepare_user_program(void) {
38+
pbsys_status_clear(PBSYS_STATUS_USER_PROGRAM_RUNNING);
4139
user_stop_func = NULL;
4240
user_stdin_event_func = NULL;
43-
44-
_pbio_light_set_user_mode(false);
45-
pbdrv_led_dev_t *led;
46-
pbdrv_led_get_dev(0, &led);
47-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
4841
}
4942

5043
pbio_error_t pbsys_stdin_get_char(uint8_t *c) {
@@ -66,19 +59,11 @@ pbio_error_t pbsys_stdout_put_char(uint8_t c) {
6659
return PBIO_SUCCESS;
6760
}
6861

69-
static void init(void) {
70-
_pbio_light_set_user_mode(false);
71-
pbdrv_led_dev_t *led;
72-
pbdrv_led_get_dev(0, &led);
73-
pbdrv_led_on(led, PBIO_COLOR_BLUE);
74-
}
75-
7662
PROCESS_THREAD(pbsys_process, ev, data) {
7763
static struct etimer timer;
7864

7965
PROCESS_BEGIN();
8066

81-
init();
8267
etimer_set(&timer, clock_from_msec(50));
8368

8469
while (true) {

0 commit comments

Comments
 (0)