-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.rs
More file actions
143 lines (124 loc) · 4.76 KB
/
Copy pathterm.rs
File metadata and controls
143 lines (124 loc) · 4.76 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![no_std]
#![no_main]
use alloc::string::String;
use embedded_hal_bus::spi::ExclusiveDevice;
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
use esp_hal::gpio::Level::{High, Low};
use esp_hal::gpio::{Input, InputConfig, Output, OutputConfig, Pull};
use esp_hal::i2c::master::{BusTimeout, Config, I2c};
use esp_hal::main;
use esp_hal::spi::master::{Config as SpiConfig, Spi};
use esp_hal::time::Rate;
use log::info;
use embedded_graphics::mono_font::ascii::FONT_8X13;
use embedded_graphics::{mono_font::MonoTextStyle, pixelcolor::Rgb565, prelude::*, text::Text};
use mipidsi::interface::SpiInterface;
use mipidsi::options::{ColorInversion, ColorOrder, Orientation, Rotation};
use mipidsi::{models::ST7789, Builder};
#[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 mut 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();
delay.delay_millis(1000);
// ==== display setup ====
// https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/examples/HelloWorld/HelloWorld.ino
// set TFT CS to high
let mut tft_cs = Output::new(peripherals.GPIO12, High, OutputConfig::default());
tft_cs.set_high();
let tft_miso = Input::new(
peripherals.GPIO38,
InputConfig::default().with_pull(Pull::Up),
);
let tft_sck = peripherals.GPIO40;
let tft_mosi = peripherals.GPIO41;
let tft_dc = Output::new(peripherals.GPIO11, Low, OutputConfig::default());
let mut tft_enable = Output::new(peripherals.GPIO42, High, OutputConfig::default());
tft_enable.set_high();
info!("creating spi device");
let spi = Spi::new(
peripherals.SPI2,
SpiConfig::default().with_frequency(Rate::from_mhz(40)), // .with_mode(Mode::_0)
)
.unwrap()
.with_sck(tft_sck)
.with_miso(tft_miso)
.with_mosi(tft_mosi);
let mut buffer = [0u8; 512];
info!("setting up the display");
let spi_delay = Delay::new();
let spi_device = ExclusiveDevice::new(spi, tft_cs, spi_delay).unwrap();
let di = SpiInterface::new(spi_device, tft_dc, &mut buffer);
info!("building");
let mut display = Builder::new(ST7789, di)
// .reset_pin(tft_enable)
.display_size(240, 320)
.orientation(Orientation::new().rotate(Rotation::Deg90))
.invert_colors(ColorInversion::Inverted)
.color_order(ColorOrder::Rgb)
// .display_size(320,240)
.init(&mut delay)
.unwrap();
// wait for everything to boot up
// delay.delay_millis(500);
// let colors = [Rgb565::BLACK, Rgb565::WHITE, Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE];
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!("initialized");
let style = MonoTextStyle::new(&FONT_8X13, Rgb565::WHITE);
display.clear(Rgb565::BLACK).unwrap();
let mut text = String::from("?");
Text::new(&text, Point::new(20, 30), style)
.draw(&mut display)
.unwrap();
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 {
let char = data[0];
if char == 8 {
info!("backspace");
text.pop();
} else {
info!("kb_res {}", data[0]);
let typed = String::from_utf8_lossy(&data);
info!("kb_res = {:?}", typed);
text.push_str(&typed);
}
display.clear(Rgb565::BLACK).unwrap();
Text::new(&text, Point::new(20, 30), style)
.draw(&mut display)
.unwrap();
}
}
Err(e) => {
info!("kb_res = {}", e);
delay.delay_millis(1000);
}
}
}
}