Skip to content

Commit

Permalink
Espnow remote, refactor terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
Kezii committed Mar 19, 2024
1 parent f4ded61 commit 2e27045
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 202 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Terminal emulator with rink-core built-in, with a reduced set of (gnu) units so
You can use it as a simple units-aware calculator

```
cargo run --release --bin terminal
cargo run --release --bin rink
```

# Credits
Expand Down
113 changes: 113 additions & 0 deletions src/bin/espnow_remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use cardputer::{
hal::cardputer_peripherals,
terminal::Terminal,
typing::{KeyboardEvent, Typing},
SCREEN_HEIGHT, SCREEN_WIDTH,
};

use esp_idf_hal::peripherals;
use log::info;

use esp_idf_svc::wifi::{AccessPointConfiguration, EspWifi};
use esp_idf_svc::{
espnow::{EspNow, PeerInfo},
eventloop::EspSystemEventLoop,
wifi::{ClientConfiguration, Configuration},
};

#[allow(clippy::approx_constant)]
fn main() {
esp_idf_svc::sys::link_patches();

esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = peripherals::Peripherals::take().unwrap();

let sysloop = EspSystemEventLoop::take().unwrap();

let (mut display, mut keyboard) =
cardputer_peripherals(peripherals.pins, peripherals.spi2, peripherals.ledc);

let mut raw_fb = Box::new([0u16; SCREEN_WIDTH * SCREEN_HEIGHT]);
let mut terminal = Terminal::<SCREEN_WIDTH, SCREEN_HEIGHT>::new(raw_fb.as_mut_ptr());

terminal.push_line("Espnow Remote");
display.eat_framebuffer(terminal.print("")).unwrap();

let mut wifi = EspWifi::new(peripherals.modem, sysloop.clone(), None).unwrap();

let client_cfg = ClientConfiguration {
channel: Some(0),
..Default::default()
};

let ap_cfg = AccessPointConfiguration {
ssid: "esp32-remote".try_into().unwrap(),
..Default::default()
};

wifi.set_configuration(&Configuration::Mixed(client_cfg, ap_cfg))
.unwrap();

wifi.start().unwrap();

terminal.push_line("Wifi started");
display.eat_framebuffer(terminal.print("")).unwrap();

let peer_address = loop {
let peer_address = find_client(&mut wifi);

if let Some(peer_address) = peer_address {
break peer_address;
}

terminal.push_line("No peer found. Retrying...");
display.eat_framebuffer(terminal.print("")).unwrap();
};

terminal.push_line(&format!("found peer: {:?}", peer_address));
display.eat_framebuffer(terminal.print("")).unwrap();

let espnow = EspNow::take().unwrap();

espnow
.register_send_cb(|_, _| {
info!("send_cb");
})
.unwrap();

let peer_info = PeerInfo {
peer_addr: peer_address,
channel: 0,
ifidx: esp_idf_hal::sys::wifi_interface_t_WIFI_IF_AP,
..Default::default()
};

espnow.add_peer(peer_info).unwrap();

let mut typing = Typing::new();

terminal.push_line("Ready. Type to send.");
display.eat_framebuffer(terminal.print("")).unwrap();

loop {
let evt = keyboard.read_events();
if let Some(evt) = evt {
if let Some(KeyboardEvent::Ascii(c)) = typing.eat_keyboard_events(evt) {
espnow.send(peer_address, &[c as u8]).unwrap();
}
}
}
}

fn find_client(wifi: &mut EspWifi) -> Option<[u8; 6]> {
let scan = wifi.scan().unwrap();

for ap in scan {
if ap.ssid == "esp32" {
return Some(ap.bssid);
}
}

None
}
3 changes: 2 additions & 1 deletion src/bin/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn main() {

let peripherals = peripherals::Peripherals::take().unwrap();

let (display, mut keyboard) = cardputer_peripherals(peripherals);
let (display, mut keyboard) =
cardputer_peripherals(peripherals.pins, peripherals.spi2, peripherals.ledc);

let mut raw_framebuffer_0 = Box::new([0u16; 240 * 135]);
let mut raw_framebuffer_1 = Box::new([0u16; 240 * 135]);
Expand Down
95 changes: 95 additions & 0 deletions src/bin/rink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use cardputer::{
hal::cardputer_peripherals,
terminal::Terminal,
typing::{KeyboardEvent, Typing},
SCREEN_HEIGHT, SCREEN_WIDTH,
};

use esp_idf_hal::peripherals;
use log::info;

#[allow(clippy::approx_constant)]
fn main() {
esp_idf_svc::sys::link_patches();

esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = peripherals::Peripherals::take().unwrap();

let (mut display, mut keyboard) =
cardputer_peripherals(peripherals.pins, peripherals.spi2, peripherals.ledc);

let mut raw_fb = Box::new([0u16; SCREEN_WIDTH * SCREEN_HEIGHT]);
let mut terminal = Terminal::<SCREEN_WIDTH, SCREEN_HEIGHT>::new(raw_fb.as_mut_ptr());

let mut typing = Typing::new();

let mut command_line = String::new();
let mut previous_command_line = String::new();
let mut ctx = simple_context_().unwrap();

loop {
let evt = keyboard.read_events();
if let Some(evt) = evt {
if let Some(evts) = typing.eat_keyboard_events(evt) {
match evts {
KeyboardEvent::Ascii(c) => {
command_line.push(c);
}
KeyboardEvent::Backspace => {
command_line.pop();
}
KeyboardEvent::Enter => {
info!("Command: {}", command_line);
terminal.push_line(&format!("> {}", command_line));
let res = execute_command(command_line.as_str(), &mut ctx);
previous_command_line = command_line.clone();
command_line.clear();

terminal.push_line(&res);
}
KeyboardEvent::ArrowUp => {
command_line = previous_command_line.clone();
}
_ => {}
}
}
}

display
.eat_framebuffer(terminal.print(&command_line))
.unwrap();
}
}

fn execute_command(command: &str, ctx: &mut rink_core::Context) -> String {
use rink_core::*;

//let mut ctx = Context::new();

let result = one_line(ctx, command);

match result {
Ok(r) => r,
Err(r) => r,
}
}

pub fn simple_context_() -> Result<rink_core::Context, String> {
use rink_core::*;

use rink_core::loader::gnu_units;

let units = include_str!("definitions.units");

let mut iter = gnu_units::TokenIterator::new(units).peekable();
let units = gnu_units::parse(&mut iter);

//let dates = parsing::datetime::parse_datefile(DATES_FILE);

let mut ctx = Context::new();
ctx.load(units)?;
//ctx.load_dates(dates);

Ok(ctx)
}
Loading

0 comments on commit 2e27045

Please sign in to comment.