From 8536d80048a5cdd98fa6215363bc252f42bb27f3 Mon Sep 17 00:00:00 2001 From: Alexander Shtuchkin Date: Sun, 19 Mar 2017 23:32:19 -0700 Subject: [PATCH] Added Wifi output to Particle platform. --- platform-particle/main.cpp | 8 +++++++ platform-particle/notes.md | 8 ++++++- platform-particle/output_serial.cpp | 2 +- platform-particle/output_wifi.cpp | 36 +++++++++++++++++++++++++++++ platform-particle/output_wifi.h | 18 +++++++++++++++ platform-particle/platform.cpp | 2 +- src/settings.cpp | 1 + 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 platform-particle/output_wifi.cpp create mode 100644 platform-particle/output_wifi.h diff --git a/platform-particle/main.cpp b/platform-particle/main.cpp index 479295a..1678d65 100644 --- a/platform-particle/main.cpp +++ b/platform-particle/main.cpp @@ -2,9 +2,17 @@ #include "settings.h" #include "application.h" +SYSTEM_MODE(MANUAL); +//SerialLogHandler logHandler; // Uncomment to pipe system logging to Serial. + // This will be either configuration pipeline, or production pipeline. std::unique_ptr pipeline; +void setup() { + WiFi.connect(); + waitUntil(WiFi.ready); +} + void loop() { try { if (!pipeline || pipeline->is_stop_requested()) { diff --git a/platform-particle/notes.md b/platform-particle/notes.md index 7608a70..c5e29ac 100644 --- a/platform-particle/notes.md +++ b/platform-particle/notes.md @@ -4,9 +4,15 @@ sensor0 pin 3 negative tim base0 origin -1.528180 2.433750 -1.969390 matrix -0.841840 0.332160 -0.425400 -0.046900 0.740190 0.670760 0.537680 0.584630 -0.607540 base1 origin 1.718700 2.543170 0.725060 matrix 0.458350 -0.649590 0.606590 0.028970 0.693060 0.720300 -0.888300 -0.312580 0.336480 object0 sensor0 0.0000 0.0000 0.0000 -stream0 position object0 > usb_serial +serial2 9600 # Wifi +stream0 position object0 > serial2 +To get the outputs via wifi, use socat: +``` +socat - udp4-recv:3300,reuseaddr +``` + ## Build sequence for 'main' project. dependent modules: user wiring hal system services communication platform wiring_globals diff --git a/platform-particle/output_serial.cpp b/platform-particle/output_serial.cpp index 126c910..b47ada7 100644 --- a/platform-particle/output_serial.cpp +++ b/platform-particle/output_serial.cpp @@ -46,7 +46,7 @@ void HardwareSerialOutputNode::start() { } OutputNode::CreatorRegistrar HardwareSerialOutputNode::creator_([](uint32_t idx, const OutputDef& def) -> std::unique_ptr { - if (idx > 0 && idx < num_outputs) + if (idx < num_outputs && hardware_serials[idx]) return std::make_unique(idx, def); return nullptr; }); diff --git a/platform-particle/output_wifi.cpp b/platform-particle/output_wifi.cpp new file mode 100644 index 0000000..c07ca2c --- /dev/null +++ b/platform-particle/output_wifi.cpp @@ -0,0 +1,36 @@ +#include "output_wifi.h" + +OutputNodeWifi::OutputNodeWifi(uint32_t idx, const OutputDef& def) + : OutputNode(idx, def) + , udp_stream_{} { + local_port_ = 33000; + remote_port_ = 3300; +} + +void OutputNodeWifi::start() { + OutputNode::start(); + udp_stream_.begin(local_port_); + uint32_t local_ip = WiFi.localIP().raw().ipv4; + uint32_t subnet_mask = WiFi.subnetMask().raw().ipv4; + uint32_t broadcast_ip = local_ip | ~subnet_mask; + remote_ip_ = broadcast_ip; +} + +size_t OutputNodeWifi::write(const uint8_t *buffer, size_t size) { + size_t ret = udp_stream_.sendPacket(buffer, size, remote_ip_, remote_port_); + if (ret < 0) + Serial.printf("Error sending UDP packet: %d\n", ret); + return ret; +} + +int OutputNodeWifi::read() { + if (!udp_stream_.available()) + udp_stream_.parsePacket(); + return udp_stream_.read(); +} + +OutputNode::CreatorRegistrar OutputNodeWifi::creator_([](uint32_t idx, const OutputDef& def) -> std::unique_ptr { + if (idx == 2) + return std::make_unique(idx, def); + return nullptr; +}); diff --git a/platform-particle/output_wifi.h b/platform-particle/output_wifi.h new file mode 100644 index 0000000..6be9bfc --- /dev/null +++ b/platform-particle/output_wifi.h @@ -0,0 +1,18 @@ +#pragma once +#include "output_serial.h" +#include + +class OutputNodeWifi : public OutputNode { +public: + OutputNodeWifi(uint32_t idx, const OutputDef& def); + +protected: + virtual void start(); + virtual size_t write(const uint8_t *buffer, size_t size); + virtual int read(); + static CreatorRegistrar creator_; + + uint16_t local_port_, remote_port_; + IPAddress remote_ip_; + UDP udp_stream_; +}; diff --git a/platform-particle/platform.cpp b/platform-particle/platform.cpp index c86d6c9..6452fe6 100644 --- a/platform-particle/platform.cpp +++ b/platform-particle/platform.cpp @@ -34,7 +34,7 @@ uint32_t Timestamp::cur_time_millis() { LedState cur_led_state = LedState::kNotInitialized; LEDStatus led_statuses[] = { [(int)LedState::kNotInitialized] = LEDStatus(RGB_COLOR_ORANGE, LED_PATTERN_SOLID), - [(int)LedState::kConfigMode] = LEDStatus(RGB_COLOR_GREEN, LED_PATTERN_BLINK, LED_SPEED_SLOW), + [(int)LedState::kConfigMode] = LEDStatus(RGB_COLOR_ORANGE, LED_PATTERN_BLINK, LED_SPEED_SLOW), [(int)LedState::kNoFix] = LEDStatus(RGB_COLOR_BLUE, LED_PATTERN_BLINK, LED_SPEED_SLOW), [(int)LedState::kFixFound] = LEDStatus(RGB_COLOR_BLUE, LED_PATTERN_BLINK, LED_SPEED_FAST), }; diff --git a/src/settings.cpp b/src/settings.cpp index a501a26..76a4094 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -176,6 +176,7 @@ bool PersistentSettings::process_command(char *input_cmd, PrintStream &stream) { case "continue"_hash: if (!validate_setup(stream)) break; + is_configured_ = true; return false; default: