-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard.rs
More file actions
72 lines (59 loc) · 1.99 KB
/
Copy pathkeyboard.rs
File metadata and controls
72 lines (59 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![no_std]
#![no_main]
use alloc::string::String;
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
use esp_hal::gpio::Level::High;
use esp_hal::gpio::{Output, OutputConfig};
use esp_hal::i2c::master::{BusTimeout, Config, I2c};
use esp_hal::main;
use esp_hal::time::Rate;
use log::info;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
extern crate alloc;
pub const LILYGO_KB_I2C_ADDRESS: u8 = 0x55;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 72 * 1024);
let delay = Delay::new();
// have to turn on the board and wait 500ms before using the keyboard
let mut board_power = Output::new(peripherals.GPIO10, High, OutputConfig::default());
board_power.set_high();
info!("board is powering up");
delay.delay_millis(1000);
let mut i2c = I2c::new(
peripherals.I2C0,
Config::default()
.with_frequency(Rate::from_khz(100))
.with_timeout(BusTimeout::Disabled),
)
.unwrap()
.with_sda(peripherals.GPIO18)
.with_scl(peripherals.GPIO8);
info!("looping over the keyboard");
loop {
let mut data = [0u8; 1];
let kb_res = i2c.read(LILYGO_KB_I2C_ADDRESS, &mut data);
match kb_res {
Ok(_) => {
if data[0] != 0x00 {
info!("kb_res = {:?}", String::from_utf8_lossy(&data));
delay.delay_millis(100);
}
}
Err(e) => {
info!("kb_res = {e}");
delay.delay_millis(1000);
}
}
}
}