Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread Tutorial Updates #479

Merged
merged 10 commits into from
Dec 9, 2024
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