Skip to content

Commit

Permalink
fix compilation; cargo fmt; update email address
Browse files Browse the repository at this point in the history
  • Loading branch information
felixwrt committed Jun 21, 2024
1 parent 9175140 commit 0f962b0
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "examples/serialport"]
[package]
name = "sml-rs"
version = "0.4.0"
authors = ["Felix Wirth <[email protected]>"]
authors = ["Felix Wirth <[email protected]>"]
description = "Smart Message Language (SML) parser written in Rust"
repository = "https://github.com/felixwrt/sml-rs"
license = "MIT OR Apache-2.0"
Expand Down
3 changes: 1 addition & 2 deletions examples/embedded/esp32c3-power-meter-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "esp32c3-power-meter-mock"
version = "0.1.0"
authors = ["Felix Wirth <[email protected]>"]
authors = ["Felix Wirth <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[workspace]


[dependencies]
embedded-io = "0.6.1"
esp-backtrace = { version = "0.12.0", features = [
Expand Down
20 changes: 17 additions & 3 deletions examples/embedded/esp32c3-power-meter-mock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@

use esp_backtrace as _;
use esp_hal::{
clock::ClockControl, delay::Delay, gpio::Io, peripherals::Peripherals, prelude::*, rmt::Rmt, system::SystemControl, uart::{config::{Config, StopBits}, TxRxPins, Uart}
clock::ClockControl,
delay::Delay,
gpio::Io,
peripherals::Peripherals,
prelude::*,
rmt::Rmt,
system::SystemControl,
uart::{
config::{Config, StopBits},
TxRxPins, Uart,
},
};
use esp_hal_smartled::{smartLedBuffer, SmartLedsAdapter};
use hex_literal::hex;
Expand Down Expand Up @@ -42,8 +52,12 @@ fn main() -> ! {
let rx_pin = io.pins.gpio3;
let pins = TxRxPins::new_tx_rx(tx_pin, rx_pin);

let uart_config = Config::default().baudrate(9600).parity_none().stop_bits(StopBits::STOP1);
let mut uart1 = Uart::new_with_config(peripherals.UART1, uart_config, Some(pins), &clocks, None);
let uart_config = Config::default()
.baudrate(9600)
.parity_none()
.stop_bits(StopBits::STOP1);
let mut uart1 =
Uart::new_with_config(peripherals.UART1, uart_config, Some(pins), &clocks, None);

esp_println::logger::init_logger_from_env();

Expand Down
4 changes: 3 additions & 1 deletion examples/embedded/esp32c3-sml-reader-async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[package]
name = "esp32c3-sml-reader-async"
version = "0.1.0"
authors = ["Felix Wirth <[email protected]>"]
authors = ["Felix Wirth <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[workspace]

[dependencies]
embassy-executor = { version = "0.5.0", features = ["task-arena-size-40960"] }
embassy-time = { version = "0.3.0", features = ["generic-queue-8"] }
Expand Down
49 changes: 23 additions & 26 deletions examples/embedded/esp32c3-sml-reader-async/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]
#![no_main]


use core::slice;

use embassy_executor::Spawner;
Expand All @@ -15,39 +14,34 @@ use esp_hal::{
system::SystemControl,
timer::timg::TimerGroup,
uart::{
config::{Config, StopBits}, TxRxPins, Uart, UartRx
config::{Config, StopBits},
TxRxPins, Uart, UartRx,
},
Async,
};
use sml_rs::{transport::Decoder, util::ArrayBuf};


#[embassy_executor::task]
async fn reader(
mut rx: UartRx<'static, UART1, Async>,
) {
async fn reader(mut rx: UartRx<'static, UART1, Async>) {
esp_println::println!("Starting reader task!");
let buf = ArrayBuf::<4069>::default();
let mut decoder = Decoder::from_buf(buf);

loop {
let mut b = 0u8;
let r = embedded_io_async::Read::read(&mut rx, slice::from_mut(&mut b)).await;
match r {
Ok(_) => {

match decoder.push_byte(b) {
Ok(None) => {
continue;
}
Ok(Some(bytes)) => {
esp_println::println!("Got data: {bytes:?}")
}
Err(e) => {
esp_println::println!("Error receiving data: {e}")
}
Ok(_) => match decoder.push_byte(b) {
Ok(None) => {
continue;
}
Ok(Some(bytes)) => {
esp_println::println!("Got data: {bytes:?}")
}
Err(e) => {
esp_println::println!("Error receiving data: {e}")
}
}
},
Err(e) => esp_println::println!("RX Error: {:?}", e),
}
}
Expand All @@ -73,20 +67,23 @@ async fn main(spawner: Spawner) {

spawner.spawn(print()).ok();


let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

// UART PORT
let tx_pin = io.pins.gpio10;
let rx_pin = io.pins.gpio9;
let pins = TxRxPins::new_tx_rx(tx_pin, rx_pin);

let uart_config = Config::default().baudrate(9600).parity_none().stop_bits(StopBits::STOP1);

let mut uart1 = Uart::new_async_with_config(peripherals.UART1, uart_config, Some(pins), &clocks);
let uart_config = Config::default()
.baudrate(9600)
.parity_none()
.stop_bits(StopBits::STOP1);

let mut uart1 =
Uart::new_async_with_config(peripherals.UART1, uart_config, Some(pins), &clocks);
uart1.set_rx_fifo_full_threshold(0).unwrap();

let (_tx, rx) = uart1.split();

spawner.spawn(reader(rx)).ok();
}
}
7 changes: 5 additions & 2 deletions examples/embedded/esp32c3-sml-reader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[package]
name = "esp32c3-sml-reader"
version = "0.1.0"
authors = ["Felix Wirth <[email protected]>"]
authors = ["Felix Wirth <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[workspace]

[dependencies]
embedded-io = "0.6.1"
esp-backtrace = { version = "0.12.0", features = [
Expand Down Expand Up @@ -34,4 +36,5 @@ opt-level = 's'
overflow-checks = false

[patch.crates-io]
esp-hal ={ path = "../esp-hal/esp-hal" }
# TODO: remove once https://github.com/esp-rs/esp-hal/pull/1702 is merged and released
esp-hal = { git = "https://github.com/felixwrt/esp-hal.git", rev = "c7bdd3519ace4e392b7c9f38d41c5cbc96f51bc8" }
42 changes: 32 additions & 10 deletions examples/embedded/esp32c3-sml-reader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ use core::slice;

use esp_backtrace as _;
use esp_hal::{
clock::ClockControl, delay::Delay, gpio::{AnyOutput, Io, Level}, peripherals::Peripherals, prelude::*, system::SystemControl, uart::{config::{Config, StopBits}, Uart}
clock::ClockControl,
delay::Delay,
gpio::{AnyOutput, Io, Level},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
uart::{
config::{Config, StopBits},
Uart,
},
};

use sml_rs::{transport::Decoder, util::ArrayBuf};
Expand All @@ -21,14 +30,25 @@ fn main() -> ! {

// LED
let mut led = AnyOutput::new(io.pins.gpio18, Level::High);

// UART PORT
let tx_pin = io.pins.gpio1;
let rx_pin = io.pins.gpio9;
// let pins = TxRxPins::new_tx_rx(tx_pin, rx_pin);

let uart_config = Config::default().baudrate(9600).parity_none().stop_bits(StopBits::STOP1);
let mut uart1 = Uart::new_with_config(peripherals.UART1, uart_config, &clocks, None, tx_pin, rx_pin).unwrap();
let uart_config = Config::default()
.baudrate(9600)
.parity_none()
.stop_bits(StopBits::STOP1);
let mut uart1 = Uart::new_with_config(
peripherals.UART1,
uart_config,
&clocks,
None,
tx_pin,
rx_pin,
)
.unwrap();

esp_println::logger::init_logger_from_env();

Expand All @@ -41,7 +61,7 @@ fn main() -> ! {
}

log::info!("Reading SML messages...");

// read_blocking(&mut led, &mut uart1)
// Not currently implemented in esp-hal, see https://github.com/esp-rs/esp-hal/issues/1620
read_polling(&mut led, &mut uart1)
Expand All @@ -58,10 +78,10 @@ fn read_blocking(led: &mut AnyOutput, pin: &mut impl embedded_io::Read) -> ! {
let mut b = 0u8;
// read byte from the pin
pin.read(slice::from_mut(&mut b)).unwrap();

led.toggle();
led_toggle = !led_toggle;

match decoder.push_byte(b) {
Ok(None) => {
continue;
Expand All @@ -77,15 +97,18 @@ fn read_blocking(led: &mut AnyOutput, pin: &mut impl embedded_io::Read) -> ! {
}

#[allow(unused)]
fn read_polling<PIN: embedded_io::Read + embedded_io::ReadReady>(led: &mut AnyOutput, pin: &mut PIN) -> ! {
fn read_polling<PIN: embedded_io::Read + embedded_io::ReadReady>(
led: &mut AnyOutput,
pin: &mut PIN,
) -> ! {
let buf = ArrayBuf::<4069>::default();
let mut decoder = Decoder::from_buf(buf);

loop {
if pin.read_ready().unwrap() {
let mut b = 0u8;
// read byte from the pin

pin.read(slice::from_mut(&mut b)).unwrap();

match decoder.push_byte(b) {
Expand All @@ -103,4 +126,3 @@ fn read_polling<PIN: embedded_io::Read + embedded_io::ReadReady>(led: &mut AnyOu
led.toggle();
}
}

2 changes: 1 addition & 1 deletion examples/serialport/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sml-rs-serialport-example"
version = "0.1.0"
authors = ["Felix Wirth <[email protected]>"]
authors = ["Felix Wirth <[email protected]>"]
description = "Example of using `sml-rs` with `serialport`"
repository = "https://github.com/felixwrt/sml-rs"
license = "MIT OR Apache-2.0"
Expand Down

0 comments on commit 0f962b0

Please sign in to comment.