Skip to content

Commit

Permalink
Fix: do not change the speed of the JSY by default and detect the cur…
Browse files Browse the repository at this point in the history
…rent speed instead
  • Loading branch information
mathieucarbou committed Jan 28, 2024
1 parent 5f3f340 commit a316413
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 105 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ jobs:
- name: Build JSYReadAsync
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/JSYReadAsync/JSYReadAsync.ino"

- name: Build EnergyReset
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/EnergyReset/EnergyReset.ino"

- name: Build EnergyResetAsync
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/EnergyResetAsync/EnergyResetAsync.ino"

- name: Build SetSpeed
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/SetSpeed/SetSpeed.ino"

platformio:
name: PlatformIO
needs: cpplint
Expand Down Expand Up @@ -120,3 +129,12 @@ jobs:

- name: Build JSYReadAsync
run: platformio ci "examples/JSYReadAsync/JSYReadAsync.ino" -l '.' -b ${{ matrix.board }}

- name: Build EnergyReset
run: platformio ci "examples/EnergyReset/EnergyReset.ino" -l '.' -b ${{ matrix.board }}

- name: Build EnergyResetAsync
run: platformio ci "examples/EnergyResetAsync/EnergyResetAsync.ino" -l '.' -b ${{ matrix.board }}

- name: Build SetSpeed
run: platformio ci "examples/SetSpeed/SetSpeed.ino" -l '.' -b ${{ matrix.board }}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ void loop() {
// access values
}
```

### Energy reset

```c++
Mycila::JSY.resetEnergy();
```

### Update Baud rate (change speed)

```c++
if (Mycila::JSY.isEnabled() && Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400 && Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) {
ESP.restart();
}
```
14 changes: 14 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ void loop() {
// access values
}
```

### Energy reset

```c++
Mycila::JSY.resetEnergy();
```

### Update Baud rate (change speed)

```c++
if (Mycila::JSY.isEnabled() && Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400 && Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) {
ESP.restart();
}
```
27 changes: 27 additions & 0 deletions examples/EnergyReset/EnergyReset.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <MycilaJSY.h>
#include <MycilaLogger.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

Mycila::Logger.forwardTo(&Serial);

// read JSY on pins 17 (JSY RX) and 16 (JSY TX)
Mycila::JSY.begin(17, 16);
}

void loop() {
if (Mycila::JSY.read()) {

JsonDocument doc;
Mycila::JSY.toJson(doc.to<JsonObject>());
serializeJson(doc, Serial);
Serial.println();

if (Mycila::JSY.energy1 > 0 || Mycila::JSY.energy2 > 0 || Mycila::JSY.energyReturned1 > 0 || Mycila::JSY.energyReturned2 > 0)
Mycila::JSY.resetEnergy();
}
delay(1000);
}
26 changes: 26 additions & 0 deletions examples/EnergyResetAsync/EnergyResetAsync.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <MycilaJSY.h>
#include <MycilaLogger.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

Mycila::Logger.forwardTo(&Serial);

// read JSY on pins 17 (JSY RX) and 16 (JSY TX)
Mycila::JSY.begin(17, 16, &Serial2, true, 60, 0);
}

void loop() {
if (Mycila::JSY.isEnabled()) {
JsonDocument doc;
Mycila::JSY.toJson(doc.to<JsonObject>());
serializeJson(doc, Serial);
Serial.println();

if (Mycila::JSY.energy1 > 0 || Mycila::JSY.energy2 > 0 || Mycila::JSY.energyReturned1 > 0 || Mycila::JSY.energyReturned2 > 0)
Mycila::JSY.resetEnergy();
}
delay(1000);
}
9 changes: 5 additions & 4 deletions examples/JSYRead/JSYRead.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include <MycilaJSY.h>
#include <MycilaLogger.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_38400, &Serial2);
// Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_19200, &Serial2);
// Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_9600, &Serial2);
// Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_4800, &Serial2);
Mycila::Logger.forwardTo(&Serial);

// read JSY on pins 17 (JSY RX) and 16 (JSY TX)
Mycila::JSY.begin(17, 16);
}

void loop() {
Expand Down
8 changes: 6 additions & 2 deletions examples/JSYReadAsync/JSYReadAsync.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <MycilaJSY.h>
#include <MycilaLogger.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_38400, &Serial2, true, 60, 0);
Mycila::Logger.forwardTo(&Serial);

// read JSY on pins 17 (JSY RX) and 16 (JSY TX)
Mycila::JSY.begin(17, 16, &Serial2, true, 60, 0);
}

void loop() {
Expand All @@ -15,5 +19,5 @@ void loop() {
serializeJson(doc, Serial);
Serial.println();
}
delay(5000);
delay(1000);
}
41 changes: 41 additions & 0 deletions examples/SetSpeed/SetSpeed.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <MycilaJSY.h>
#include <MycilaLogger.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

Mycila::Logger.forwardTo(&Serial);

// read JSY on pins 17 (JSY RX) and 16 (JSY TX)
Mycila::JSY.begin(17, 16);

if (Mycila::JSY.isEnabled()) {
Mycila::Logger.info("APP", "JSY is enabled at %d bauds", Mycila::JSY.getBaudRate());

if (Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) {
Mycila::Logger.warn("APP", "JSY baud rate wrong");

if (Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) {
Mycila::Logger.info("APP", "JSY baud rate updated");
ESP.restart();

} else {
Mycila::Logger.error("APP", "JSY baud rate update failed");
}
}
} else {
Mycila::Logger.info("APP", "JSY is disabled");
}
}

void loop() {
if (Mycila::JSY.read()) {
JsonDocument doc;
Mycila::JSY.toJson(doc.to<JsonObject>());
serializeJson(doc, Serial);
Serial.println();
}
delay(1000);
}
30 changes: 24 additions & 6 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
[platformio]
lib_dir = .
; src_dir = examples/JSYRead
; src_dir = examples/JSYReadAsync
; src_dir = examples/EnergyReset
; src_dir = examples/EnergyResetAsync
src_dir = examples/SetSpeed
default_envs = esp32

[env]
build_flags = -Wall -Wextra

build_flags =
-Wall -Wextra
-Wunused -Wmisleading-indentation -Wduplicated-cond -Wlogical-op -Wnull-dereference
-std=c++17
-std=gnu++17
-O0
-ggdb -ggdb3 -g3
-D CONFIG_ARDUHAL_LOG_COLORS
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
build_unflags =
-std=gnu++11

lib_deps =
bblanchon/ArduinoJson
mathieucarbou/MycilaLogger
lib_compat_mode = strict
lib_ldf_mode = deep+

upload_protocol = esptool
upload_port = /dev/cu.usbserial-0001
Expand All @@ -11,11 +34,6 @@ monitor_port = /dev/cu.usbserial-0001
monitor_speed = 115200
monitor_filters = esp32_exception_decoder, log2file

[platformio]
lib_dir = .
src_dir = examples/JSYRead
; src_dir = examples/JSYReadAsync

[env:esp32]
platform = [email protected]
board = esp32dev
Expand Down
Loading

0 comments on commit a316413

Please sign in to comment.