Skip to content

Commit 8205794

Browse files
authored
Merge pull request #479 from tyler-potyondy/ewsn-tutorial
Thread Tutorial Updates
2 parents 874efe2 + ba9b86d commit 8205794

File tree

29 files changed

+787
-1096
lines changed

29 files changed

+787
-1096
lines changed

examples/tutorials/thread_network/01_sensor_ipc/main.c

Lines changed: 0 additions & 33 deletions
This file was deleted.
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
3+
#include <libtock-sync/sensors/temperature.h>
4+
#include <libtock-sync/services/alarm.h>
5+
#include <libtock/kernel/ipc.h>
6+
#include <libtock/tock.h>
7+
8+
static int current_temperature = 0;
9+
10+
int main(void) {
11+
// Measure temperature -- returned in the form 2200 => 22C
12+
libtocksync_temperature_read(&current_temperature);
13+
14+
// Convert temperature
15+
int whole_degree = current_temperature / 100;
16+
int decimal_degree = current_temperature % 100;
17+
18+
printf("Hello World, the temperature is: %i.%i\r\n", whole_degree, decimal_degree);
19+
20+
return 0;
21+
}

examples/tutorials/thread_network/02_sensor_final/main.c

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,23 @@
55
#include <libtock/kernel/ipc.h>
66
#include <libtock/tock.h>
77

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

14-
static void sensor_ipc_callback(int pid, int len, int buf,
15-
__attribute__((unused)) void* ud) {
16-
// A client has requested us to provide them the current temperature value.
17-
// We must make sure that it provides us with a buffer sufficiently large to
18-
// store a single integer:
19-
if (len < ((int) sizeof(current_temperature))) {
20-
// We do not inform the caller and simply return. We do print a log message:
21-
puts("[thread-sensor] ERROR: sensor IPC invoked with too small buffer.\r\n");
22-
return;
23-
}
24-
25-
// The buffer is large enough, copy the current temperature into it:
26-
memcpy((void*) buf, &current_temperature, sizeof(current_temperature));
27-
28-
// Let the client know:
29-
ipc_notify_client(pid);
30-
}
31-
3210
int main(void) {
33-
// Measure the temperature once before registering ourselves as an IPC
34-
// service. This ensures that we always return a correct (but potentially
35-
// stale) temperature value.
36-
libtocksync_temperature_read(&current_temperature);
3711

38-
// Register this application as an IPC service under its name:
39-
ipc_register_service_callback("org.tockos.thread-tutorial.sensor",
40-
sensor_ipc_callback,
41-
NULL);
42-
43-
// We measure the temperature in the main loop and simply provide the latest
44-
// reading in an IPC. This means that the control app does not have to wait
45-
// for the temperature read system call to complete.
12+
// We measure the temperature in the main loop and
13+
// print this value to the console.
4614
while (1) {
15+
// Measure temperature -- returned in the form 2200 => 22C
4716
libtocksync_temperature_read(&current_temperature);
48-
// printf("Current temperature: %d\r\n", current_temperature);
17+
18+
// Convert temperature
19+
int whole_degree = current_temperature / 100;
20+
int decimal_degree = current_temperature % 100;
21+
22+
printf("Current temperature: %i.%i\r\n", whole_degree, decimal_degree);
23+
24+
// Delay 1000 ms (1 second).
4925
libtocksync_alarm_delay_ms(1000);
5026
}
5127
}

examples/tutorials/thread_network/03_controller_screen/main.c

Lines changed: 0 additions & 138 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)