Skip to content

Commit 98f24d9

Browse files
feat: add heartbeat indicator on builtin led for core examples
1 parent 70013bf commit 98f24d9

14 files changed

Lines changed: 136 additions & 11 deletions

File tree

examples/core_usage/arduino/advanced_controller/advanced_controller.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@
1111

1212
// Instantiate CRUMBS as Controller, set to true for Controller mode
1313
static crumbs_context_t crumbsController; // C struct
14+
static uint32_t last_heartbeat_ms = 0;
15+
static bool led_state = false;
16+
17+
static void heartbeat_tick()
18+
{
19+
const uint32_t now = millis();
20+
if ((uint32_t)(now - last_heartbeat_ms) < HEARTBEAT_INTERVAL_MS)
21+
return;
22+
23+
last_heartbeat_ms = now;
24+
led_state = !led_state;
25+
digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
26+
}
1427

1528
// Initializes the Controller device, sets up serial communication, and provides usage instructions.
1629
void setup()
1730
{
1831
Serial.begin(SERIAL_BAUD); /**< Initialize serial communication at configured baud rate */
32+
pinMode(LED_BUILTIN, OUTPUT);
33+
digitalWrite(LED_BUILTIN, LOW);
1934

2035
while (!Serial)
2136
{
@@ -36,6 +51,7 @@ void setup()
3651
// Main loop that listens for serial input, parses commands, and sends crumbs_message_ts to the specified Slice.
3752
void loop()
3853
{
54+
heartbeat_tick();
3955
// Listen for serial input to send commands or request data
4056
handleSerialInput();
4157
}

examples/core_usage/arduino/advanced_controller/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include <stdint.h>
44

55
static const uint32_t SERIAL_BAUD = 115200;
6+
static const uint32_t HEARTBEAT_INTERVAL_MS = 500;
67
static const uint32_t SERIAL_WAIT_MS = 10;
78

89
static const uint32_t REQUEST_READ_DELAY_MS = 50;
910
static const uint8_t SCAN_START_ADDR = 0x03;
1011
static const uint8_t SCAN_END_ADDR = 0x77;
1112
static const uint32_t SCAN_TIMEOUT_US = 50000;
12-

examples/core_usage/arduino/basic_controller/basic_controller.ino

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
#include "config.h"
1010

1111
crumbs_context_t ctx;
12+
static uint32_t last_heartbeat_ms = 0;
13+
static bool led_state = false;
14+
15+
static void heartbeat_tick()
16+
{
17+
const uint32_t now = millis();
18+
if ((uint32_t)(now - last_heartbeat_ms) < HEARTBEAT_INTERVAL_MS)
19+
return;
20+
21+
last_heartbeat_ms = now;
22+
led_state = !led_state;
23+
digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
24+
}
1225

1326
void send_store_data()
1427
{
@@ -101,6 +114,8 @@ void query_stored_data()
101114
void setup()
102115
{
103116
Serial.begin(SERIAL_BAUD);
117+
pinMode(LED_BUILTIN, OUTPUT);
118+
digitalWrite(LED_BUILTIN, LOW);
104119
crumbs_arduino_init_controller(&ctx);
105120

106121
Serial.println("=== Basic Controller ===");
@@ -113,6 +128,8 @@ void setup()
113128

114129
void loop()
115130
{
131+
heartbeat_tick();
132+
116133
if (Serial.available())
117134
{
118135
char cmd = Serial.read();

examples/core_usage/arduino/basic_controller/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdint.h>
55

66
static const uint32_t SERIAL_BAUD = 115200;
7+
static const uint32_t HEARTBEAT_INTERVAL_MS = 500;
78

89
static const uint8_t TARGET_ADDR = 0x08;
910
static const uint8_t TARGET_TYPE_ID = 0x01;
@@ -23,4 +24,3 @@ static const char CMD_KEY_STORE = 's';
2324
static const char CMD_KEY_CLEAR = 'c';
2425
static const char CMD_KEY_QUERY_VERSION = 'v';
2526
static const char CMD_KEY_QUERY_STORED = 'd';
26-

examples/core_usage/arduino/basic_peripheral/basic_peripheral.ino

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
crumbs_context_t ctx;
1111
uint8_t stored_data[STORED_DATA_CAPACITY];
1212
uint8_t stored_len = 0;
13+
static uint32_t last_heartbeat_ms = 0;
14+
static bool led_state = false;
15+
16+
static void heartbeat_tick()
17+
{
18+
const uint32_t now = millis();
19+
if ((uint32_t)(now - last_heartbeat_ms) < HEARTBEAT_INTERVAL_MS)
20+
return;
21+
22+
last_heartbeat_ms = now;
23+
led_state = !led_state;
24+
digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
25+
}
1326

1427
void on_message(crumbs_context_t *ctx, const crumbs_message_t *msg)
1528
{
@@ -69,6 +82,8 @@ void on_request(crumbs_context_t *ctx, crumbs_message_t *reply)
6982
void setup()
7083
{
7184
Serial.begin(SERIAL_BAUD);
85+
pinMode(LED_BUILTIN, OUTPUT);
86+
digitalWrite(LED_BUILTIN, LOW);
7287
crumbs_arduino_init_peripheral(&ctx, DEVICE_ADDR);
7388
crumbs_set_callbacks(&ctx, on_message, on_request, NULL);
7489
Serial.print("Basic peripheral ready at 0x");
@@ -77,5 +92,7 @@ void setup()
7792

7893
void loop()
7994
{
80-
// All processing in callbacks
95+
// All processing in callbacks.
96+
heartbeat_tick();
97+
delay(1);
8198
}

examples/core_usage/arduino/basic_peripheral/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdint.h>
55

66
static const uint32_t SERIAL_BAUD = 115200;
7+
static const uint32_t HEARTBEAT_INTERVAL_MS = 500;
78

89
static const uint8_t DEVICE_ADDR = 0x08;
910
static const uint8_t DEVICE_TYPE_ID = 0x01;
@@ -19,4 +20,3 @@ static const uint8_t MODULE_VERSION_MINOR = 0;
1920
static const uint8_t MODULE_VERSION_PATCH = 0;
2021

2122
static const size_t STORED_DATA_CAPACITY = 10u;
22-

examples/core_usage/arduino/basic_peripheral_noncrumbs/basic_peripheral_noncrumbs.ino

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
#include <Wire.h>
2020
#include "config.h"
2121

22+
static uint32_t last_heartbeat_ms = 0;
23+
static bool led_state = false;
24+
25+
static void heartbeat_tick()
26+
{
27+
const uint32_t now = millis();
28+
if ((uint32_t)(now - last_heartbeat_ms) < HEARTBEAT_INTERVAL_MS)
29+
return;
30+
31+
last_heartbeat_ms = now;
32+
led_state = !led_state;
33+
digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
34+
}
35+
2236
void onReceiveHandler(int numBytes)
2337
{
2438
// Drain and count bytes (we intentionally do not interpret them as CRUMBS)
@@ -42,6 +56,8 @@ void onRequestHandler()
4256
void setup()
4357
{
4458
Serial.begin(SERIAL_BAUD);
59+
pinMode(LED_BUILTIN, OUTPUT);
60+
digitalWrite(LED_BUILTIN, LOW);
4561
while (!Serial)
4662
; // wait for serial monitor on some boards
4763

@@ -56,5 +72,6 @@ void setup()
5672
void loop()
5773
{
5874
// Nothing to do here - interrupts drive I2C callbacks.
59-
delay(LOOP_DELAY_MS);
75+
heartbeat_tick();
76+
delay(1);
6077
}

examples/core_usage/arduino/basic_peripheral_noncrumbs/config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <stdint.h>
44

55
static const uint32_t SERIAL_BAUD = 115200;
6+
static const uint32_t HEARTBEAT_INTERVAL_MS = 500;
67
static const uint8_t NONCRUMBS_ADDR = 69; // 0x45
7-
static const uint32_t LOOP_DELAY_MS = 1000;
88
static const char NONCRUMBS_PAYLOAD[] = "NOCRUMBS";
9-

examples/core_usage/arduino/hello_controller/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdint.h>
55

66
static const uint32_t SERIAL_BAUD = 115200;
7+
static const uint32_t HEARTBEAT_INTERVAL_MS = 500;
78

89
static const uint8_t TARGET_ADDR = 0x08;
910
static const uint8_t TARGET_TYPE_ID = 0x01;
@@ -18,4 +19,3 @@ static const uint32_t READ_TIMEOUT_US = 5000;
1819

1920
static const char CMD_SEND = 's';
2021
static const char CMD_REQUEST = 'r';
21-

examples/core_usage/arduino/hello_controller/hello_controller.ino

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
#include "config.h"
99

1010
crumbs_context_t ctx;
11+
static uint32_t last_heartbeat_ms = 0;
12+
static bool led_state = false;
13+
14+
static void heartbeat_tick()
15+
{
16+
const uint32_t now = millis();
17+
if ((uint32_t)(now - last_heartbeat_ms) < HEARTBEAT_INTERVAL_MS)
18+
return;
19+
20+
last_heartbeat_ms = now;
21+
led_state = !led_state;
22+
digitalWrite(LED_BUILTIN, led_state ? HIGH : LOW);
23+
}
1124

1225
void send_hello()
1326
{
@@ -48,12 +61,16 @@ void request_data()
4861
void setup()
4962
{
5063
Serial.begin(SERIAL_BAUD);
64+
pinMode(LED_BUILTIN, OUTPUT);
65+
digitalWrite(LED_BUILTIN, LOW);
5166
crumbs_arduino_init_controller(&ctx);
5267
Serial.println("Commands: s=send, r=request");
5368
}
5469

5570
void loop()
5671
{
72+
heartbeat_tick();
73+
5774
if (Serial.available())
5875
{
5976
char c = Serial.read();

0 commit comments

Comments
 (0)