read eep! rom

This commit is contained in:
Alex Janka 2024-01-17 09:28:00 +11:00
parent dff2bf1b15
commit 461a0b59a5
4 changed files with 36 additions and 16 deletions

3
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
version = "1.0.2-prerelease"
version = "1.0.2"
dependencies = [
"async-channel",
"chrono",
@ -2272,7 +2272,6 @@ dependencies = [
"serde",
"serde_json",
"serialport",
"termcolor",
"teslatte",
"thiserror",
"tokio",

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-charge-controller"
version = "1.0.2-prerelease"
version = "1.0.2"
edition = "2021"
license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data"
@ -30,4 +30,3 @@ prometheus = "0.13"
env_logger = "0.10"
log = "0.4"
serialport = "4.3"
termcolor = "1.4.1"

View file

@ -7,7 +7,6 @@ use std::{
use metrics::{gauge, Gauge, Label};
use serde::{Deserialize, Serialize};
use serialport::SerialPort;
use termcolor::WriteColor;
use crate::errors::{PliError, PrintErrors};
@ -140,6 +139,7 @@ impl RegulatorGauges {
#[derive(Debug, Clone, Copy)]
pub enum PliRequest {
ReadRam(u8),
ReadEeprom(u8),
}
impl Pli {
@ -197,16 +197,15 @@ impl Pli {
match message {
PliRequest::ReadRam(address) => {
if let Some(data) = self.read_ram(address).some_or_print_with("reading pl ram") {
let mut stdout =
termcolor::StandardStream::stdout(termcolor::ColorChoice::Always);
let _ = stdout.set_color(
termcolor::ColorSpec::new().set_fg(Some(termcolor::Color::Green)),
);
if writeln!(&mut stdout, "Read RAM at {address}: data {data}").is_err() {
log::warn!(
"Failed to set stdout colour\nRead RAM at {address}: data {data}"
);
};
log::warn!("Read RAM at {address}: data {data}");
}
}
PliRequest::ReadEeprom(address) => {
if let Some(data) = self
.read_eeprom(address)
.some_or_print_with("reading pl eeprom")
{
log::warn!("Read EEPROM at {address}: data {data}");
}
}
}
@ -260,6 +259,19 @@ impl Pli {
Err(PliError::ReadError(buf[0]))
}
}
fn read_eeprom<T>(&mut self, address: T) -> Result<u8, PliError>
where
T: Into<u8>,
{
self.send_command(command(72, address.into(), 0))?;
let buf = self.receive::<1>()?;
if buf[0] == 200 {
Ok(self.receive::<1>()?[0])
} else {
Err(PliError::ReadError(buf[0]))
}
}
}
enum PlRamAddress {

View file

@ -61,7 +61,8 @@ fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
disable_control,
enable_control,
metrics,
read_ram
read_ram,
read_eeprom
],
)
}
@ -129,6 +130,15 @@ async fn read_ram(address: u8, state: &State<ServerState>) -> String {
format!("reading at ram address {address}")
}
#[get("/read-eeprom/<address>")]
async fn read_eeprom(address: u8, state: &State<ServerState>) -> String {
let _ = state
.pli_requests
.send(PliRequest::ReadEeprom(address))
.await;
format!("reading at eeprom address {address}")
}
#[get("/regulator-state")]
async fn regulator_state(state: &State<ServerState>) -> Result<Json<PlState>, ServerError> {
state