Skip to content

Instantly share code, notes, and snippets.

@mprymek
Created March 16, 2021 23:30
Show Gist options
  • Save mprymek/d1176be039c8f58e7d3ba4b3b7358e51 to your computer and use it in GitHub Desktop.
Save mprymek/d1176be039c8f58e7d3ba4b3b7358e51 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include "csp.h"
#define LED1_PIN LED_BUILTIN
#define BLINK_DELAY_MS 100
void blink_led1(task_data_t data) {
uint32_t state = (uint32_t)data;
digitalWrite(LED1_PIN, state);
state = (state == HIGH) ? LOW : HIGH;
csp_delay(CSP_MS_TO_TICKS(BLINK_DELAY_MS), blink_led1, (task_data_t)state);
}
typedef struct {
chan_t *c;
unsigned int i;
} publisher_state_t;
void publisher1(task_data_t data) {
publisher_state_t *state = (publisher_state_t *)data;
unsigned int i = state->i;
state->i++;
Serial.print("sending: ");
Serial.println(i);
csp_send(state->c, (chan_data_t)i, publisher1, (task_data_t)state);
}
void receiver2(chan_data_t data, task_data_t task_data);
void receiver1(task_data_t data) {
chan_t *c = (chan_t *)data;
csp_recv(c, receiver2, data);
}
void receiver2(chan_data_t data, task_data_t task_data) {
unsigned int i = (unsigned int)data;
Serial.print("received: ");
Serial.println(i);
csp_delay(CSP_MS_TO_TICKS(1000), receiver1, (task_data_t)task_data);
}
void greeter(task_data_t data) {
unsigned int i = (unsigned int)data;
Serial.print("Hello from task ");
Serial.println(i);
}
void setup() {
Serial.begin(115200);
Serial.println("\n\nCSP Pico! 0.1\n");
pinMode(LED1_PIN, OUTPUT);
csp_spawn(blink_led1, (task_data_t)HIGH);
chan_t c;
csp_chan_init(&c);
publisher_state_t publisher_state;
publisher_state.i = 0;
publisher_state.c = &c;
csp_spawn(publisher1, &publisher_state);
csp_spawn(receiver1, &c);
for (int i = 0; i < 800; i++) {
csp_spawn(greeter, (task_data_t)i);
}
csp_run();
}
// Won't be reached.
void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment