Skip to content

Commit

Permalink
Add support for "getchipstatus" command
Browse files Browse the repository at this point in the history
  • Loading branch information
nilclass committed Feb 9, 2024
1 parent 73a1ee7 commit 7e986d2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parser;
use crate::types::{Bridgelist, Color, Message, Net, SupplySwitchPos};
use crate::types::{Bridgelist, ChipStatus, Color, Message, Net, SupplySwitchPos};
use anyhow::{Context, Result};
use serialport::SerialPort;
use std::fs::File;
Expand Down Expand Up @@ -48,6 +48,7 @@ enum Instruction {
GetSupplySwitch,
SetSupplySwitch(SupplySwitchPos),
Lightnet(String, Color),
GetChipStatus,
Raw(String, String),
}

Expand Down Expand Up @@ -106,6 +107,10 @@ impl Instruction {
sequence_number, net_name, color
)
}

Instruction::GetChipStatus => {
format!("::getchipstatus:{}[]", sequence_number)
}
}
}
}
Expand Down Expand Up @@ -254,6 +259,27 @@ impl Device {
Ok(())
}

pub fn chipstatus(&mut self) -> Result<Vec<ChipStatus>> {
let seq = self.send_instruction(Instruction::GetChipStatus)?;
let mut result = vec![];
let mut begin = false;
self.receive_ok_capture(seq, |message| match message {
Message::ChipStatusBegin => {
begin = true;
}
Message::ChipStatus(chip_status) => {
if begin {
result.push(chip_status);
}
}
Message::ChipStatusEnd => {
begin = false;
}
_ => {}
})?;
Ok(result)
}

pub fn lightnet(&mut self, name: String, color: Color) -> Result<()> {
self.send_instruction(Instruction::Lightnet(name, color))?;
Ok(())
Expand Down
38 changes: 36 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::types::{Bridgelist, Color, Message, Net, Node, SupplySwitchPos};
use crate::types::{Bridgelist, ChipStatus, Color, Message, Net, Node, SupplySwitchPos};
use nom::{
branch::alt,
bytes::complete::{tag, take_till},
character::complete::{u32, u8},
character::complete::{anychar, i8, u32, u8},
combinator::{all_consuming, map, map_res, value},
multi::{separated_list0, separated_list1},
sequence::{preceded, separated_pair, tuple},
Expand All @@ -19,6 +19,9 @@ pub fn message(input: &str) -> IResult<&str, Message> {
map(net, Net),
map(bridgelist, Bridgelist),
map(supplyswitch, SupplySwitch),
map(chipstatus_begin, |_| ChipStatusBegin),
map(chipstatus_end, |_| ChipStatusEnd),
map(chipstatus, ChipStatus),
)))(input)
}

Expand Down Expand Up @@ -77,6 +80,37 @@ pub fn net(input: &str) -> IResult<&str, Net> {
)(input)
}

pub fn chipstatus_begin(input: &str) -> IResult<&str, ()> {
value((), tag("::chipstatus-begin"))(input)
}

pub fn chipstatus_end(input: &str) -> IResult<&str, ()> {
value((), tag("::chipstatus-end"))(input)
}

pub fn chipstatus(input: &str) -> IResult<&str, ChipStatus> {
map(
tuple((
tag("::chipstatus["),
anychar,
tag(","),
separated_list1(tag(","), i8),
tag("]"),
)),
|(_, char, _, status, _)| {
let mut x_status = [0; 16];
let mut y_status = [0; 8];
x_status.copy_from_slice(&status[0..16]);
y_status.copy_from_slice(&status[16..24]);
ChipStatus {
char,
x_status,
y_status,
}
},
)(input)
}

fn boolean(input: &str) -> IResult<&str, bool> {
alt((value(true, tag("true")), value(false, tag("false"))))(input)
}
Expand Down
14 changes: 13 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ async fn set_supply_switch_pos(
Ok(web::Json(pos.to_string()))
}

#[get("/chip_status")]
async fn get_chipstatus(shared: web::Data<Shared>) -> Result<impl Responder> {
let chipstatus = shared
.device_manager
.lock()
.unwrap()
.with_device(|device| device.chipstatus())
.map_err(Error)?;
Ok(web::Json(chipstatus))
}

// #[get("/bridges")]
// async fn bridges(shared: web::Data<Shared>) -> Result<impl Responder> {
// let nodefile: NodeFile = shared
Expand Down Expand Up @@ -230,7 +241,8 @@ async fn start_with_listener(
.service(put_nets)
.service(set_supply_switch_pos)
.service(get_supply_switch_pos)
.service(clear_bridges);
.service(clear_bridges)
.service(get_chipstatus);

#[cfg(feature = "jumperlab")]
{
Expand Down
10 changes: 10 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub enum Message {
Net(Net),
Bridgelist(Bridgelist),
SupplySwitch(SupplySwitchPos),
ChipStatusBegin,
ChipStatus(ChipStatus),
ChipStatusEnd,
}

pub type Bridgelist = Vec<(Node, Node)>;
Expand Down Expand Up @@ -391,3 +394,10 @@ impl std::fmt::Display for Node {
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct ChipStatus {
pub char: char,
pub x_status: [i8; 16],
pub y_status: [i8; 8],
}

0 comments on commit 7e986d2

Please sign in to comment.