|
5 | 5 | #include <libtock/kernel/ipc.h>
|
6 | 6 | #include <libtock/tock.h>
|
7 | 7 |
|
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. |
12 | 8 | static int current_temperature = 0;
|
13 | 9 |
|
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, ¤t_temperature, sizeof(current_temperature)); |
27 |
| - |
28 |
| - // Let the client know: |
29 |
| - ipc_notify_client(pid); |
30 |
| -} |
31 |
| - |
32 | 10 | 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(¤t_temperature); |
37 | 11 |
|
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. |
46 | 14 | while (1) {
|
| 15 | + // Measure temperature -- returned in the form 2200 => 22C |
47 | 16 | libtocksync_temperature_read(¤t_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). |
49 | 25 | libtocksync_alarm_delay_ms(1000);
|
50 | 26 | }
|
51 | 27 | }
|
0 commit comments