~caolan/bramley

8e32e2a4c412b44a465365672df8d924282bca7e — Caolan McMahon 1 year, 5 months ago 0818441
add preview of current chord and re-instate shutdown command
2 files changed, 62 insertions(+), 38 deletions(-)

M input/src/lib.rs
M menu/src/main.rs
M input/src/lib.rs => input/src/lib.rs +1 -0
@@ 10,6 10,7 @@ pub enum Input {
    Character(char),
    Backspace,
    Unassigned,
    Menu,
}

pub type Chord = [bool; 6];

M menu/src/main.rs => menu/src/main.rs +61 -38
@@ 4,19 4,19 @@ use rusttype::Font;
use input::{InputEvents, InputEvent, Input};
use drawing::{size_of_text, MemoryDisplayBufferExt};
use futures::stream::StreamExt;
// use std::process::Command;
use std::process::Command;
use std::collections::HashMap;

const WIDTH: usize = 400;
const HEIGHT: u8 = 240;
const CS_GPIO: u8 = 12;

// fn shutdown() {
//     Command::new("sudo")
//             .arg("poweroff")
//             .status()
//             .expect("failed to execute shutdown command");
// }
fn shutdown() {
    Command::new("sudo")
            .arg("poweroff")
            .status()
            .expect("failed to execute shutdown command");
}

#[tokio::main]
async fn main() {


@@ 44,6 44,7 @@ async fn main() {
        // .ready_chunks(1000);

    let mut input_map = HashMap::new();
    input_map.insert([true, true, true, true, true, true], Input::Menu);
    input_map.insert([false, false, false, true, false, false], Input::Character(' '));
    input_map.insert([false, false, false, false, true, false], Input::Character('e'));
    input_map.insert([true, false, false, false, false, false], Input::Character('t'));


@@ 79,8 80,9 @@ async fn main() {
    let mut input_events = InputEvents::new(button_events, input_map).ready_chunks(1000);

    let mut text = String::new();
    let mut preview = String::new();

    let draw = |buffer: &mut MemoryDisplayBuffer, text: &str| {
    let draw = |buffer: &mut MemoryDisplayBuffer, text: &str, preview: &str| {
        // clear screen
        buffer.fill(Pixel::White);
        buffer.draw_text(


@@ 101,62 103,83 @@ async fn main() {
            &[&text],
        );
        draw_carat(
            &font,
            font_size,
            buffer,
            10 + last_line_width,
            10 + (text_bottom - last_line_height) as u8,
            last_line_height as u8
            last_line_height as u8,
            preview,
        );
    };

    draw(&mut buffer, &text);
    draw(&mut buffer, &text, &preview);
    display.update(&buffer).unwrap();

    while let Some(events) = input_events.next().await {
        // catch up with all input events before drawing screen
        for event in events {
            println!("event: {:?}", event);
            if let InputEvent::Commit(input) = event {
                match input {
                    Input::Backspace => {
                        text.pop();
                    },
                    Input::Character(ch) => {
                        text.push(ch);
                    },
                    Input::Unassigned => {
                        // do nothing
                    },
            preview.clear();
            match event {
                InputEvent::Commit(input) => {
                    match input {
                        Input::Backspace => {
                            text.pop();
                        },
                        Input::Character(ch) => {
                            text.push(ch);
                        },
                        Input::Menu => {
                            shutdown();
                            return;
                        },
                        Input::Unassigned => {
                            // do nothing
                        },
                    }
                },
                InputEvent::Preview(input) => {
                    match input {
                        Input::Backspace => {
                            preview.push_str("<-");
                        },
                        Input::Character(ch) => {
                            preview.push(ch);
                        },
                        Input::Menu => {
                            // do nothing
                        },
                        Input::Unassigned => {
                            // do nothing
                        },
                    }
                }
            }
        }
        // // if at any point all 6 buttons are pressed immediately
        // // shutdown
        // if pressed.iter().all(|x| *x) {
        //     // buffer.fill(Pixel::White);
        //     // buffer.draw_text(
        //     //     &font,
        //     //     font_size,
        //     //     &["Shutting down..."],
        //     //     10,
        //     //     10,
        //     // );
        //     // display.update(&buffer).unwrap();
        //     shutdown();
        //     return;
        // }
        draw(&mut buffer, &text);
        draw(&mut buffer, &text, &preview);
        // update display (can be slow)
        display.update(&buffer).unwrap();
    }
}

fn draw_carat(
    font: &Font,
    font_size: f32,
    buffer: &mut MemoryDisplayBuffer,
    x: usize,
    y: u8,
    height: u8
    height: u8,
    preview: &str,
) {
    for i in 0..height {
        buffer.set_pixel(x, y + i, Pixel::Black);
    }
    buffer.draw_text(
        font,
        font_size,
        &[preview],
        x,
        y,
    );
}