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

View file

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

View file

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

View file

@ -61,7 +61,8 @@ fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
disable_control, disable_control,
enable_control, enable_control,
metrics, 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}") 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")] #[get("/regulator-state")]
async fn regulator_state(state: &State<ServerState>) -> Result<Json<PlState>, ServerError> { async fn regulator_state(state: &State<ServerState>) -> Result<Json<PlState>, ServerError> {
state state