Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Oct 10, 2024
1 parent 82dd940 commit 84c1533
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 55 deletions.
25 changes: 14 additions & 11 deletions examples/freertos_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,32 @@ void setup() {
SetupLogging();

SensESPMinimalAppBuilder builder;
SensESPMinimalApp *sensesp_app = builder.set_hostname("async")->get_app();
auto sensesp_app = builder.set_hostname("async")->get_app();

auto *networking = new Networking("/system/networking", "", "");
auto *http_server = new HTTPServer();
auto networking = std::make_shared<Networking>("/system/networking", "", "");
auto http_server = std::make_shared<HTTPServer>();

// create the SK delta object
auto sk_delta_queue_ = new SKDeltaQueue();
auto sk_delta_queue_ = std::make_shared<SKDeltaQueue>();

// create the websocket client
auto ws_client_ = new SKWSClient("/system/sk", sk_delta_queue_, "", 0);
auto ws_client_ =
std::make_shared<SKWSClient>("/system/sk", sk_delta_queue_, "", 0);

ws_client_->connect_to(
new LambdaConsumer<SKWSConnectionState>([](SKWSConnectionState input) {
auto output_consumer = std::make_shared<LambdaConsumer<SKWSConnectionState>>(
[](SKWSConnectionState input) {
ESP_LOGD("Example", "SKWSConnectionState: %d", input);
}));
});

ws_client_->connect_to(output_consumer);

// create the MDNS discovery object
auto mdns_discovery_ = new MDNSDiscovery();
auto mdns_discovery_ = std::make_shared<MDNSDiscovery>();

// create a system status controller and a led blinker

auto system_status_controller = new SystemStatusController();
auto system_status_led = new SystemStatusLed(LED_BUILTIN);
auto system_status_controller = std::make_shared<SystemStatusController>();
auto system_status_led = std::make_shared<SystemStatusLed>(LED_BUILTIN);

system_status_controller->connect_to(
system_status_led->get_system_status_consumer());
Expand Down
2 changes: 1 addition & 1 deletion examples/join_and_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

using namespace sensesp;

SensESPMinimalApp* sensesp_app;
std::shared_ptr<SensESPMinimalApp> sensesp_app;

void setup() {
SetupLogging();
Expand Down
35 changes: 20 additions & 15 deletions examples/minimal_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,32 @@ void setup() {
// manually create Networking and HTTPServer objects to enable
// the HTTP configuration interface

auto* networking = new Networking("/system/networking", "", "");
auto* http_server = new HTTPServer();
auto networking = std::make_shared<Networking>("/system/networking", "", "");
auto http_server = std::make_shared<HTTPServer>();

auto* digin1 = new DigitalInputCounter(input_pin1, INPUT, RISING, read_delay);
auto* digin2 = new DigitalInputCounter(input_pin2, INPUT, CHANGE, read_delay);
auto digin1 = std::make_shared<DigitalInputCounter>(input_pin1, INPUT, RISING,
read_delay);
auto digin2 = std::make_shared<DigitalInputCounter>(input_pin2, INPUT, CHANGE,
read_delay);

auto* scaled1 = new Linear(2, 1, "/digin1/scale");
auto* scaled2 = new Linear(4, -1, "/digin2/scale");
auto scaled1 = std::make_shared<Linear>(2, 1, "/digin1/scale");
auto scaled2 = std::make_shared<Linear>(4, -1, "/digin2/scale");
digin1->connect_to(scaled1);

scaled1->connect_to(new LambdaTransform<int, int>([](int input) {
Serial.printf("millis: %d\n", millis());
Serial.printf("Counter 1: %d\n", input);
return input;
}));

digin2->connect_to(scaled2)->connect_to(
new LambdaTransform<int, int>([](int input) {
auto lambda_transform1 =
std::make_shared<LambdaTransform<int, int>>([](int input) {
Serial.printf("millis: %d\n", millis());
Serial.printf("Counter 1: %d\n", input);
return input;
});
scaled1->connect_to(lambda_transform1);
auto lambda_transform2 =
std::make_shared<LambdaTransform<int, int>>([](int input) {
Serial.printf("Counter 2: %d\n", input);
return input;
}));
});

digin2->connect_to(scaled2)->connect_to(lambda_transform2);

pinMode(output_pin1, OUTPUT);
event_loop()->onRepeat(
Expand Down
6 changes: 3 additions & 3 deletions examples/raw_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ void setup() {
SetupLogging();

SensESPAppBuilder builder;
SensESPApp *sensesp_app = builder.set_hostname("json_demo")
->set_wifi_client("Hat Labs Sensors", "kanneluuri2406")
->get_app();
auto sensesp_app = builder.set_hostname("json_demo")
->set_wifi_client("Hat Labs Sensors", "kanneluuri2406")
->get_app();

event_loop()->onRepeat(1000, []() { toggler.set(!toggler.get()); });

Expand Down
2 changes: 1 addition & 1 deletion examples/repeat_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

using namespace sensesp;

SensESPMinimalApp* sensesp_app;
std::shared_ptr<SensESPMinimalApp> sensesp_app;

void setup() {
SetupLogging();
Expand Down
6 changes: 3 additions & 3 deletions examples/smart_switch/remote_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void setup() {
controller->connect_to(new BoolSKPutRequest(sk_path));

// Also connect the controller to an onboard LED...
controller->connect_to(led);
controller->connect_to(led->on_off_consumer_);

// Connect a physical button that will feed manual click types into the
// controller...
Expand All @@ -92,7 +92,7 @@ void setup() {
->set_title("Click Type")
->set_sort_order(1000);

pr->connect_to(click_type)->connect_to(controller);
pr->connect_to(click_type)->connect_to(controller->click_consumer_);

// In addition to the manual button "click types", a
// SmartSwitchController accepts explicit state settings via
Expand All @@ -105,7 +105,7 @@ void setup() {
// sent across the Signal K network when the controlling device
// confirms it has made the change in state.
auto* sk_listener = new SKValueListener<bool>(sk_path);
sk_listener->connect_to(controller);
sk_listener->connect_to(controller->swich_consumer_);
}

void loop() { event_loop()->tick(); }
14 changes: 8 additions & 6 deletions examples/smart_switch/smart_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void setup() {
// Create the global SensESPApp() object.
sensesp_app = builder.set_hostname("sk-engine-lights")
->set_sk_server("192.168.10.3", 3000)
->set_wifi_client(client("YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD")
->set_wifi_client("YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD")
->get_app();

// Define the SK Path that represents the load this device controls.
Expand All @@ -73,9 +73,10 @@ void setup() {
// electric light. Also connect this pin's state to an LED to get
// a visual indicator of load's state.
auto* load_switch = new DigitalOutput(PIN_RELAY);
load_switch->connect_to(new RgbLed(PIN_LED_R, PIN_LED_G, PIN_LED_B,
config_path_status_light, LED_ON_COLOR,
LED_OFF_COLOR));
load_switch->connect_to(
(new RgbLed(PIN_LED_R, PIN_LED_G, PIN_LED_B, config_path_status_light,
LED_ON_COLOR, LED_OFF_COLOR))
->on_off_consumer_);

// Create a switch controller to handle the user press logic and
// connect it to the load switch...
Expand All @@ -87,7 +88,8 @@ void setup() {
DigitalInputState* btn = new DigitalInputState(PIN_BUTTON, INPUT, 100);
PressRepeater* pr = new PressRepeater();
btn->connect_to(pr);
pr->connect_to(new ClickType(config_path_button_c))->connect_to(controller);
pr->connect_to(new ClickType(config_path_button_c))
->connect_to(controller->click_consumer_);

// In addition to the manual button "click types", a
// SmartSwitchController accepts explicit state settings via
Expand All @@ -98,7 +100,7 @@ void setup() {
// This allows any device on the SignalK network that can make
// such a request to also control the state of our switch.
auto* sk_listener = new StringSKPutRequestListener(sk_path);
sk_listener->connect_to(controller);
sk_listener->connect_to(controller->truthy_string_consumer_);

// Finally, connect the load switch to an SKOutput so it reports its state
// to the Signal K server. Since the load switch only reports its state
Expand Down
17 changes: 17 additions & 0 deletions src/sensesp/system/rgb_led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ bool RgbLed::from_json(const JsonObject& config) {
return true;
}

void RgbLed::set_color(long new_value) {
if (led_r_channel_ >= 0) {
float r = get_pwm(new_value, 16, common_anode_);
PWMOutput::set_pwm(led_r_channel_, r);
}

if (led_g_channel_ >= 0) {
float g = get_pwm(new_value, 8, common_anode_);
PWMOutput::set_pwm(led_g_channel_, g);
}

if (led_b_channel_ >= 0) {
float b = get_pwm(new_value, 0, common_anode_);
PWMOutput::set_pwm(led_b_channel_, b);
}
}

const String ConfigSchema(const RgbLed& obj) {
return R"({"type":"object","properties":{"led_on_rgb":{"title":"RGB color for led ON","type":"integer"},"led_off_rgb":{"title":"RGB color for led OFF","type":"integer"}}})";
}
Expand Down
16 changes: 1 addition & 15 deletions src/sensesp/system/rgb_led.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,8 @@ class RgbLed : public FileSystemSaveable {
long led_off_rgb_;
bool common_anode_;

void set_color(long new_value) {
if (led_r_channel_ >= 0) {
float r = get_pwm(new_value, 16, common_anode_);
PWMOutput::set_pwm(led_r_channel_, r);
}
void set_color(long new_value);

if (led_g_channel_ >= 0) {
float g = get_pwm(new_value, 8, common_anode_);
PWMOutput::set_pwm(led_g_channel_, g);
}

if (led_b_channel_ >= 0) {
float b = get_pwm(new_value, 0, common_anode_);
PWMOutput::set_pwm(led_b_channel_, b);
}
}
};

const String ConfigSchema(const RgbLed& obj);
Expand Down

0 comments on commit 84c1533

Please sign in to comment.