Skip to content

Commit

Permalink
Merge pull request #4 from QRPp/config-singleton-obj
Browse files Browse the repository at this point in the history
Add rlock-protected API for the singleton object
  • Loading branch information
rojer committed Mar 31, 2021
2 parents e5122f9 + ce01c09 commit cd33db7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/mgos_arduino_dallas_temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extern "C" {
// Auto-created, config-controlled singleton. NULL if disabled.
DallasTemperature *mgos_ds18x_get_global();

// The rlock-protected access to the above.
DallasTemperature *mgos_ds18x_get_global_locked();
void mgos_ds18x_put_global_locked();

// Initialize DallasTemperature driver.
// Return value: handle opaque pointer.
DallasTemperature *mgos_arduino_dt_create(OneWire *ow);
Expand Down
13 changes: 13 additions & 0 deletions src/mgos_arduino_dallas_temp_c.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
#include <stdbool.h>

#include <mgos_config.h>
#include <mgos_system.h>

#include <mgos_arduino_dallas_temp.h>

static struct mgos_rlock_type *lock = NULL;
static DallasTemperature *s_global_mgos_ds18x = NULL;

DallasTemperature *mgos_ds18x_get_global() {
return s_global_mgos_ds18x;
}

DallasTemperature *mgos_ds18x_get_global_locked() {
if (s_global_mgos_ds18x) mgos_rlock(lock);
return s_global_mgos_ds18x;
}

void mgos_ds18x_put_global_locked() {
if (s_global_mgos_ds18x) mgos_runlock(lock);
}

bool mgos_arduino_dallas_temperature_init(void) {
int pin = mgos_sys_config_get_ds18x_pin();
if (pin < 0) return true;
lock = mgos_rlock_create();
s_global_mgos_ds18x = mgos_arduino_dt_create(mgos_arduino_onewire_create(pin));
mgos_arduino_dt_begin(s_global_mgos_ds18x);
return true;
Expand Down

0 comments on commit cd33db7

Please sign in to comment.