Skip to content

Commit

Permalink
Verbose examples
Browse files Browse the repository at this point in the history
Updated example code to include verbose explanations for people new to SensESP
  • Loading branch information
joelkoz committed Jun 6, 2019
1 parent ca58adc commit 5cdc539
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 30 deletions.
49 changes: 42 additions & 7 deletions examples/analog_input.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
#include <Arduino.h>

#include "sensesp_app.h"
#include "wiring_helpers.h"
#include "devices/analog_input.h"
#include "transforms/linear.h"

// SensESP builds upon the ReactESP framework. Every ReactESP application
// defines an "app" object vs defining a "main()" method.
ReactESP app([] () {

// Some initialization boilerplate when in debug mode...
#ifndef SERIAL_DEBUG_DISABLED
Serial.begin(115200);

// A small arbitrary delay is required to let the
// serial port catch up

delay(100);
Debug.setSerialEnabled(true);
#endif


// Create the global SensESPApp() object.
sensesp_app = new SensESPApp();

setup_analog_input(
sensesp_app,
"sensors.indoor.illumination",
1, 0,
"/sensors/indoor_illumination");

// The "SignalK path" identifies this sensor to the SignalK network. Leaving
// this blank would indicate this particular sensor (or transform) does not
// broadcast SignalK data
const char* sk_path = "sensors.indoor.illumination";


// The "Configuration path" is combined with "/config" to formulate a URL
// used by the RESTful API for retrieving or setting configuration data.
// It is ALSO used to specify a path to the SPIFFS file system
// where configuration data is saved on the MCU board. It should
// ALWAYS start with a forward slash if specified. If left blank,
// that indicates this sensor or transform does not have any
// configuration to save.
// Note that if you want to be able to change the sk_path at runtime,
// you will need to specify a config_path.
const char* config_path = "/sensors/indoor_illumination";


// Create a "device" that is the source of our data
auto* pAnalogInput = new AnalogInput();


// Create a "transform" that can modify the data and/or broadcast it
// over the SignalK network.
const float multiplier = 1.0;
const float offset = 0.0;
auto* pTransform = new Linear(sk_path, multiplier, offset, config_path);


// Wire up the output of the analog input to the transform
pAnalogInput->connectTo(pTransform);


// Start the SensESP application running
sensesp_app->enable();
});
64 changes: 41 additions & 23 deletions examples/rpm_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "transforms/frequency.h"
#include "wiring_helpers.h"


// SensESP builds upon the ReactESP framework. Every ReactESP application
// defines an "app" object vs defining a "main()" method.
ReactESP app([]() {
#ifndef SERIAL_DEBUG_DISABLED
Serial.begin(115200);
Expand All @@ -20,39 +23,54 @@ ReactESP app([]() {

sensesp_app = new SensESPApp();

//////////
// connect a RPM meter. A DigitalInputCounter counts pulses
// and reports the readings every read_delay ms
// (500 in the example). A Frequency
// transform takes a number of pulses and converts that into
// a frequency. The sample multiplier converts the 97 tooth
// tach output into Hz, SK native units.

// Three ways to wire up devices and transformations:

// 1. Connect by specifying <Observer,Transform> (aka <Device,Transform>):
/*
sensesp_app->connect_1to1<DigitalInputCounter, Frequency>(
new DigitalInputCounter(D5, INPUT_PULLUP, RISING, 500),
new Frequency("propulsion.left.revolutions", 1./97.,
"/sensors/engine_rpm")
);
*/
// The "SignalK path" identifies this sensor to the SignalK network. Leaving
// this blank would indicate this particular sensor (or transform) does not
// broadcast SignalK data
const char* sk_path = "propulsion.left.revolutions";


// The "Configuration path" is combined with "/config" to formulate a URL
// used by the RESTful API for retrieving or setting configuration data.
// It is ALSO used to specify a path to the SPIFFS file system
// where configuration data is saved on the MCU board. It should
// ALWAYS start with a forward slash if specified. If left blank,
// that indicates this sensor or transform does not have any
// configuration to save.
// Note that if you want to be able to change the sk_path at runtime,
// you will need to specify a config_path.
const char* config_path = "/sensors/engine_rpm";

// 2. Connect by specifying data type of Producer's output/Consumer's input


//////////
// connect a RPM meter. A DigitalInputCounter counts pulses
// and reports the readings every read_delay ms
// (500 in the example). A Frequency
// transform takes a number of pulses and converts that into
// a frequency. The sample multiplier converts the 97 tooth
// tach output into Hz, SK native units.
const float multiplier = 1.0 / 97.0;
const uint read_delay = 500;


// Two ways to wire up devices and transformations:

// 1. Connect by specifying data type of Producer's output/Consumer's input
/*
sensesp_app->connect<int>(
new DigitalInputCounter(D5, INPUT_PULLUP, RISING, 500),
new Frequency("propulsion.left.revolutions", 1./97.,
"/sensors/engine_rpm")
new DigitalInputCounter(D5, INPUT_PULLUP, RISING, read_delay),
new Frequency(sk_path, multiplier, config_path)
);
*/

// 3. Connect the producer directly to the consumer
(new DigitalInputCounter(D5, INPUT_PULLUP, RISING, 500))
->connectTo(new Frequency("propulsion.left.revolutions", 1. / 97.,
"/sensors/engine_rpm"));
auto* pDevice = new DigitalInputCounter(D5, INPUT_PULLUP, RISING, read_delay);

pDevice->connectTo(new Frequency(sk_path, multiplier, config_path));


// Start the SensESP application running
sensesp_app->enable();
});

0 comments on commit 5cdc539

Please sign in to comment.