Skip to content

Commit

Permalink
Merge pull request #22 from FrameworkComputer/b1display-is-sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAZoidberg authored Mar 10, 2023
2 parents 302dcfe + db0af58 commit b1b861b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
59 changes: 34 additions & 25 deletions b1display/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ type B1ST7306 = ST7306<
200,
>;

#[allow(clippy::large_enum_variant)]
#[derive(Clone)]
enum SleepState {
Awake,
Sleeping,
}

pub struct State {
sleeping: SleepState,
}

#[entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
Expand Down Expand Up @@ -191,8 +180,8 @@ fn main() -> ! {

let sleep = pins.sleep.into_pull_down_input();

let mut state = State {
sleeping: SleepState::Awake,
let mut state = B1DIsplayState {
sleeping: SimpleSleepState::Awake,
};

let mut said_hello = false;
Expand Down Expand Up @@ -238,16 +227,36 @@ fn main() -> ! {
// Do nothing
}
Ok(count) => {
if let Some(command) = parse_command(count, &buf) {
if let Command::Sleep(go_sleeping) = command {
match (parse_command(count, &buf), &state.sleeping) {
(Some(Command::Sleep(go_sleeping)), _) => {
handle_sleep(go_sleeping, &mut state, &mut delay, &mut disp);
} else if let SleepState::Awake = state.sleeping {
}
(Some(c @ Command::BootloaderReset), _)
| (Some(c @ Command::IsSleeping), _) => {
if let Some(response) =
handle_command(&c, &mut state, logo_rect, &mut disp)
{
let _ = serial.write(&response);
};
}
(Some(command), SimpleSleepState::Awake) => {
let mut text: String<64> = String::new();
write!(
&mut text,
"Handling command {}:{}:{}:{}\r\n",
buf[0], buf[1], buf[2], buf[3]
)
.unwrap();
let _ = serial.write(text.as_bytes());

// While sleeping no command is handled, except waking up
//handle_command(&command, &mut disp, logo_rect);
if let Some(response) = handle_command(&command, logo_rect, &mut disp) {
if let Some(response) =
handle_command(&command, &mut state, logo_rect, &mut disp)
{
let _ = serial.write(&response);
};
}
_ => {}
}
}
}
Expand All @@ -257,7 +266,7 @@ fn main() -> ! {

fn handle_sleep<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
go_sleeping: bool,
state: &mut State,
state: &mut B1DIsplayState,
_delay: &mut Delay,
disp: &mut ST7306<SPI, DC, CS, RST, COLS, ROWS>,
) where
Expand All @@ -268,9 +277,9 @@ fn handle_sleep<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
<SPI as spi::Write<u8>>::Error: Debug,
{
match (state.sleeping.clone(), go_sleeping) {
(SleepState::Awake, false) => (),
(SleepState::Awake, true) => {
state.sleeping = SleepState::Sleeping;
(SimpleSleepState::Awake, false) => (),
(SimpleSleepState::Awake, true) => {
state.sleeping = SimpleSleepState::Sleeping;

// Turn off display
disp.on_off(false).unwrap();
Expand All @@ -280,10 +289,10 @@ fn handle_sleep<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
// TODO: Set up SLEEP# pin as interrupt and wfi
//cortex_m::asm::wfi();
}
(SleepState::Sleeping, true) => (),
(SleepState::Sleeping, false) => {
(SimpleSleepState::Sleeping, true) => (),
(SimpleSleepState::Sleeping, false) => {
// Restore back grid before sleeping
state.sleeping = SleepState::Awake;
state.sleeping = SimpleSleepState::Awake;

// Turn display back on
disp.on_off(true).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion inputmodule-control/src/inputmodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn serial_commands(args: &crate::ClapCli) {
None => vec![],
};
if serialdevs.is_empty() {
println!("Failed to find serial devivce. Please manually specify with --serial-dev");
println!("Failed to find serial device. Please manually specify with --serial-dev");
return;
};

Expand Down
37 changes: 24 additions & 13 deletions lotus-inputmodules/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub enum Command {
_Unknown,
}

#[cfg(feature = "c1minimal")]
#[cfg(any(feature = "c1minimal", feature = "b1display"))]
#[derive(Clone)]
pub enum SimpleSleepState {
Awake,
Expand All @@ -164,6 +164,11 @@ pub struct C1MinimalState {
pub brightness: u8,
}

#[cfg(feature = "b1display")]
pub struct B1DIsplayState {
pub sleeping: SimpleSleepState,
}

pub fn parse_command(count: usize, buf: &[u8]) -> Option<Command> {
if let Some(command) = parse_module_command(count, buf) {
return Some(command);
Expand Down Expand Up @@ -355,10 +360,6 @@ pub fn handle_generic_command(command: &Command) -> Option<[u8; 32]> {
reset_to_usb_boot(0, 0);
None
}
Command::Sleep(_go_sleeping) => {
// Handled elsewhere
None
}
Command::Panic => panic!("Ahhh"),
Command::Version => {
let mut response: [u8; 32] = [0; 32];
Expand Down Expand Up @@ -479,6 +480,7 @@ pub fn handle_command(
#[cfg(feature = "b1display")]
pub fn handle_command<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
command: &Command,
state: &mut B1DIsplayState,
logo_rect: Rectangle,
disp: &mut ST7306<SPI, DC, CS, RST, COLS, ROWS>,
) -> Option<[u8; 32]>
Expand All @@ -490,14 +492,14 @@ where
<SPI as spi::Write<u8>>::Error: Debug,
{
match command {
Command::BootloaderReset => {
//let _ = serial.write("Bootloader Reset".as_bytes());
reset_to_usb_boot(0, 0);
None
}
Command::Sleep(_go_sleeping) => {
// Handled elsewhere
None
// TODO: Move to handle_generic_command
Command::IsSleeping => {
let mut response: [u8; 32] = [0; 32];
response[0] = match state.sleeping {
SimpleSleepState::Sleeping => 1,
SimpleSleepState::Awake => 0,
};
Some(response)
}
Command::Panic => panic!("Ahhh"),
Command::SetText(text) => {
Expand Down Expand Up @@ -567,6 +569,15 @@ pub fn handle_command(
ws2812: &mut impl SmartLedsWrite<Color = RGB8, Error = ()>,
) -> Option<[u8; 32]> {
match command {
// TODO: Move to handle_generic_command
Command::IsSleeping => {
let mut response: [u8; 32] = [0; 32];
response[0] = match state.sleeping {
SimpleSleepState::Sleeping => 1,
SimpleSleepState::Awake => 0,
};
Some(response)
}
Command::GetBrightness => {
let mut response: [u8; 32] = [0; 32];
response[0] = state.brightness;
Expand Down

0 comments on commit b1b861b

Please sign in to comment.