Skip to content

Commit

Permalink
WiP/feat: I2C Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoisel committed Aug 2, 2024
1 parent a2d28ed commit 0dc0040
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
14 changes: 13 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ mqtt:
credentials:
user: user
password: password
inputs: []
ios:
inputs:
- address: 0x20
chip: PCF8574
# has 8 digital inputs (implicitly)
- address: 0x38
chip: PCF8574
outputs:
- address: 0x39
chip: PCF8574
# has 8 digital outputs (implicitly)
- address: 0x3a
chip: MAX7311
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anyhow::{Context, Result};
#[derive(Debug, Deserialize)]
pub struct Config {
pub mqtt: Mqtt,
pub ios: IOs,
}

#[derive(Debug, Deserialize)]
Expand All @@ -29,6 +30,18 @@ pub struct Credentials {
pub password: String,
}

#[derive(Debug, Deserialize)]
pub struct IOs {
pub inputs: Vec<IO>,
pub outputs: Vec<IO>,
}

#[derive(Debug, Deserialize)]
pub struct IO {
pub address: u8,
pub chip: String,
}

pub trait ConfigParser {
fn parse_config(self) -> Result<Config>;
}
Expand Down Expand Up @@ -103,6 +116,15 @@ mqtt:
credentials:
user: "user"
password: "pass"
ios:
inputs:
- address: 0x20
chip: PCF8574
outputs:
- address: 0x21
chip: MAX7311
- address: 0x22
chip: PCF8574
"#;

let result = yaml_str.parse_config();
Expand Down
44 changes: 30 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod config;

use config::{Config, ConfigParser};
use config::{Config, ConfigParser, IOs};
use docopt::Docopt;
use std::env::args;
use std::path::Path;
use std::process;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -60,17 +61,19 @@ fn main() {
);
let (client, connection) = Client::new(mqttoptions, 10);

thread::spawn(move || process(connection));
thread::spawn(move || publish(client));

println!("Done with the stream!!");
thread::spawn(move || notify_cb(connection));
let ios = Arc::new(config.ios);
let ios_clone_1 = Arc::clone(&ios);
thread::spawn(move || mqtt_worker(client, &ios_clone_1));
let ios_clone_2 = Arc::clone(&ios);
thread::spawn(move || i2c_worker(&ios_clone_2));

loop {
thread::park();
}
}

fn process(mut connection: Connection) {
fn notify_cb(mut connection: Connection) {
for (i, notification) in connection.iter().enumerate() {
match notification {
Ok(notif) => {
Expand All @@ -84,24 +87,37 @@ fn process(mut connection: Connection) {
}
}

/*
* This is a helper function for publishing MQTT messages. In this function, we first sleep
* for one second, then subscribe to a topic. Then we loop and send ten messages with lengths
* ranging from 0 to 9, with each message's QoS being at least once.
*/
fn publish(client: Client) {
fn mqtt_worker(client: Client, ios: &Arc<IOs>) {
// Wait for one second before subscribing to a topic
thread::sleep(Duration::from_secs(1));
client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();

// Send ten messages with lengths ranging from 0 to 9, each message's QoS being at least once
for i in 0..10_usize {
let payload = vec![1; i];
for (i, input) in ios.inputs.iter().enumerate() {
let payload = format!(
"Input -- Address: 0x{:02x}, Chip:{}",
input.address, input.chip
);
let topic = format!("hello/{i}/world");
println!("Publishing '{}' to topic '{}'", payload, topic);
let qos = QoS::AtLeastOnce;

client.publish(topic, qos, true, payload).unwrap();
}

for (i, output) in ios.outputs.iter().enumerate() {
let payload = format!(
"Output -- Address: 0x{:02x}, Chip:{}",
output.address, output.chip
);
let topic = format!("hello/{i}/world");
println!("Publishing '{}' to topic '{}'", payload, topic);
let qos = QoS::AtLeastOnce;

client.publish(topic, qos, true, payload).unwrap();
}

thread::sleep(Duration::from_secs(1));
}

fn i2c_worker(_ios: &Arc<IOs>) {}

0 comments on commit 0dc0040

Please sign in to comment.