Skip to content

Commit f00ad09

Browse files
authored
Merge pull request #488 from alistair23/alistair/rainfall
Support reading rainfall from the kernel
2 parents 14e9edc + aa7faae commit f00ad09

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed

examples/sensors/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <libtock-sync/sensors/moisture.h>
77
#include <libtock-sync/sensors/ninedof.h>
88
#include <libtock-sync/sensors/proximity.h>
9+
#include <libtock-sync/sensors/rainfall.h>
910
#include <libtock-sync/sensors/sound_pressure.h>
1011
#include <libtock-sync/sensors/temperature.h>
1112
#include <libtock-sync/services/alarm.h>
@@ -22,13 +23,15 @@ static bool ninedof_gyro = false;
2223
static bool proximity = false;
2324
static bool sound_pressure = false;
2425
static bool moisture = false;
26+
static bool rainfall = false;
2527
static void alarm_cb(__attribute__ ((unused)) uint32_t now,
2628
__attribute__ ((unused)) uint32_t scheduled,
2729
__attribute__ ((unused)) void* opaque) {
2830
int lite = 0;
2931
int temp = 0;
3032
int humi = 0;
3133
int mois = 0;
34+
uint32_t rain = 0;
3235
int ninedof_accel_x = 0, ninedof_accel_y = 0, ninedof_accel_z = 0;
3336
int ninedof_magneto_x = 0, ninedof_magneto_y = 0, ninedof_magneto_z = 0;
3437
int ninedof_gyro_x = 0, ninedof_gyro_y = 0, ninedof_gyro_z = 0;
@@ -45,6 +48,7 @@ static void alarm_cb(__attribute__ ((unused)) uint32_t now,
4548
if (proximity) libtocksync_proximity_read(&prox_reading);
4649
if (sound_pressure) libtocksync_sound_pressure_read(&sound_pressure_reading);
4750
if (moisture) libtocksync_moisture_read(&mois);
51+
if (rainfall) libtocksync_rainfall_read(&rain, 1);
4852

4953
if (light) printf("Amb. Light: Light Intensity: %d\n", lite);
5054
if (temperature) printf("Temperature: %d deg C\n", temp/100);
@@ -55,6 +59,7 @@ static void alarm_cb(__attribute__ ((unused)) uint32_t now,
5559
if (proximity) printf("Proximity: %u\n", prox_reading);
5660
if (sound_pressure) printf("Sound Pressure: %u\n", sound_pressure_reading);
5761
if (moisture) printf("Moisture: %d%%\n", mois/100);
62+
if (rainfall) printf("Rainfall: %lumm\n", rain / 1000);
5863

5964
/* *INDENT-ON* */
6065

@@ -74,6 +79,7 @@ int main(void) {
7479
proximity = libtock_proximity_exists();
7580
sound_pressure = libtock_sound_pressure_exists();
7681
moisture = libtock_moisture_exists();
82+
rainfall = libtock_rainfall_exists();
7783
/* *INDENT-ON* */
7884

7985
if (ninedof) {
@@ -93,6 +99,7 @@ int main(void) {
9399
if (proximity) printf("[Sensors] Sampling Proximity sensor.\n");
94100
if (sound_pressure) printf("[Sensors] Sampling Sound Pressure sensor.\n");
95101
if (moisture) printf("[Sensors] Sampling Moisture sensor.\n");
102+
if (rainfall) printf("[Sensors] Sampling Rainfall sensor.\n");
96103
/* *INDENT-ON* */
97104

98105
if (sound_pressure) {

libtock-sync/sensors/rainfall.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "rainfall.h"
2+
3+
typedef struct {
4+
uint32_t rainfall;
5+
returncode_t ret;
6+
bool fired;
7+
} rainfall_result_t;
8+
9+
static rainfall_result_t result = {.fired = false};
10+
11+
// callback for synchronous reads
12+
static void rainfall_callback(returncode_t ret, uint32_t rainfall) {
13+
result.rainfall = rainfall;
14+
result.ret = ret;
15+
result.fired = true;
16+
}
17+
18+
returncode_t libtocksync_rainfall_read(uint32_t* rainfall, int hours) {
19+
returncode_t err;
20+
21+
result.fired = false;
22+
23+
err = libtock_rainfall_read(rainfall_callback, hours);
24+
if (err != RETURNCODE_SUCCESS) return err;
25+
26+
yield_for(&result.fired);
27+
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
28+
29+
*rainfall = result.rainfall;
30+
31+
return RETURNCODE_SUCCESS;
32+
}

libtock-sync/sensors/rainfall.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <libtock/sensors/rainfall.h>
4+
#include <libtock/tock.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Read the rainfall sensor synchronously.
11+
//
12+
// ## Arguments
13+
//
14+
// - `rainfall`: Set to the ums of rainfall in the specified number of hours.
15+
// - `hours`: The number of hours to get the rainfall data from. 1 to 24
16+
// hours are valid values.
17+
//
18+
// ## Return Value
19+
//
20+
// A returncode indicating whether the sensor read was completed successfully.
21+
returncode_t libtocksync_rainfall_read(uint32_t* rainfall, int hours);
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif

libtock/sensors/rainfall.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "rainfall.h"
2+
3+
static void rainfall_upcall(int status,
4+
int rainfall,
5+
__attribute__ ((unused)) int unused2, void* opaque) {
6+
libtock_rainfall_callback cb = (libtock_rainfall_callback) opaque;
7+
cb(status, rainfall);
8+
}
9+
10+
returncode_t libtock_rainfall_read(libtock_rainfall_callback cb, int hours) {
11+
returncode_t err;
12+
13+
err = libtock_rainfall_set_upcall(rainfall_upcall, cb);
14+
if (err != RETURNCODE_SUCCESS) return err;
15+
16+
err = libtock_rainfall_command_read(hours);
17+
return err;
18+
}

libtock/sensors/rainfall.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "../tock.h"
4+
#include "syscalls/rainfall_syscalls.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Function signature for rainfall data callback.
11+
//
12+
// - `arg1` (`int`): Returncode indicating status from sampling the sensor.
13+
// - `arg2` (`uint32_t`): the number of um of rain in the time period specified
14+
typedef void (*libtock_rainfall_callback)(returncode_t, uint32_t);
15+
16+
// Start a rainfall measurement. The reading will be provided via the callback.
17+
returncode_t libtock_rainfall_read(libtock_rainfall_callback cb, int hours);
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "rainfall_syscalls.h"
2+
3+
bool libtock_rainfall_exists(void) {
4+
return driver_exists(DRIVER_NUM_RAINFALL);
5+
}
6+
7+
returncode_t libtock_rainfall_set_upcall(subscribe_upcall callback, void* opaque) {
8+
subscribe_return_t sval = subscribe(DRIVER_NUM_RAINFALL, 0, callback, opaque);
9+
return tock_subscribe_return_to_returncode(sval);
10+
}
11+
12+
returncode_t libtock_rainfall_command_read(int hours) {
13+
syscall_return_t rval = command(DRIVER_NUM_RAINFALL, 1, hours, 0);
14+
return tock_command_return_novalue_to_returncode(rval);
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "../../tock.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#define DRIVER_NUM_RAINFALL 0x6000B
10+
11+
// Check if the rainfall driver is installed.
12+
bool libtock_rainfall_exists(void);
13+
14+
// Configure the upcall for when the reading is ready.
15+
returncode_t libtock_rainfall_set_upcall(subscribe_upcall callback, void* opaque);
16+
17+
// Read the sensor.
18+
returncode_t libtock_rainfall_command_read(int hours);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif

0 commit comments

Comments
 (0)