Skip to content

Commit

Permalink
Merge pull request #479 from tyler-potyondy/ewsn-tutorial
Browse files Browse the repository at this point in the history
Thread Tutorial Updates
  • Loading branch information
ppannuto authored Dec 9, 2024
2 parents 874efe2 + ba9b86d commit 8205794
Show file tree
Hide file tree
Showing 29 changed files with 787 additions and 1,096 deletions.
33 changes: 0 additions & 33 deletions examples/tutorials/thread_network/01_sensor_ipc/main.c

This file was deleted.

21 changes: 21 additions & 0 deletions examples/tutorials/thread_network/01_sensor_temperature/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>

#include <libtock-sync/sensors/temperature.h>
#include <libtock-sync/services/alarm.h>
#include <libtock/kernel/ipc.h>
#include <libtock/tock.h>

static int current_temperature = 0;

int main(void) {
// Measure temperature -- returned in the form 2200 => 22C
libtocksync_temperature_read(&current_temperature);

// Convert temperature
int whole_degree = current_temperature / 100;
int decimal_degree = current_temperature % 100;

printf("Hello World, the temperature is: %i.%i\r\n", whole_degree, decimal_degree);

return 0;
}
46 changes: 11 additions & 35 deletions examples/tutorials/thread_network/02_sensor_final/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,23 @@
#include <libtock/kernel/ipc.h>
#include <libtock/tock.h>

// Global variable storing the current temperature. This is written to in the
// main loop, and read from in the IPC handler. Because the app is single
// threaded and has no yield point when writing the value, we do not need to
// worry about synchronization -- reads never happen during a write.
static int current_temperature = 0;

static void sensor_ipc_callback(int pid, int len, int buf,
__attribute__((unused)) void* ud) {
// A client has requested us to provide them the current temperature value.
// We must make sure that it provides us with a buffer sufficiently large to
// store a single integer:
if (len < ((int) sizeof(current_temperature))) {
// We do not inform the caller and simply return. We do print a log message:
puts("[thread-sensor] ERROR: sensor IPC invoked with too small buffer.\r\n");
return;
}

// The buffer is large enough, copy the current temperature into it:
memcpy((void*) buf, &current_temperature, sizeof(current_temperature));

// Let the client know:
ipc_notify_client(pid);
}

int main(void) {
// Measure the temperature once before registering ourselves as an IPC
// service. This ensures that we always return a correct (but potentially
// stale) temperature value.
libtocksync_temperature_read(&current_temperature);

// Register this application as an IPC service under its name:
ipc_register_service_callback("org.tockos.thread-tutorial.sensor",
sensor_ipc_callback,
NULL);

// We measure the temperature in the main loop and simply provide the latest
// reading in an IPC. This means that the control app does not have to wait
// for the temperature read system call to complete.
// We measure the temperature in the main loop and
// print this value to the console.
while (1) {
// Measure temperature -- returned in the form 2200 => 22C
libtocksync_temperature_read(&current_temperature);
// printf("Current temperature: %d\r\n", current_temperature);

// Convert temperature
int whole_degree = current_temperature / 100;
int decimal_degree = current_temperature % 100;

printf("Current temperature: %i.%i\r\n", whole_degree, decimal_degree);

// Delay 1000 ms (1 second).
libtocksync_alarm_delay_ms(1000);
}
}
138 changes: 0 additions & 138 deletions examples/tutorials/thread_network/03_controller_screen/main.c

This file was deleted.

Loading

0 comments on commit 8205794

Please sign in to comment.