#![no_std]
#![no_main]
mod keypad_driver;
use esp_backtrace as _;
use esp_println::println;
use esp_hal::{
clock::ClockControl,
peripherals::Peripherals,
prelude::*,
Delay,
gpio::*,
systimer::SystemTimer,
timer::TimerGroup,
Rtc,
IO,
i2c::*, // Display
};
use core::mem::MaybeUninit;
use keypad_driver::{Keypad};
use alloc::string::String;
use alloc::string;
extern crate alloc;
// For displays
use embedded_graphics::{
mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder, MonoTextStyle},
pixelcolor::BinaryColor,
prelude::*,
primitives::{
Circle, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, StrokeAlignment, Triangle,
},
text::{Alignment, Baseline, Text},
};
use ssd1306::{mode::BufferedGraphicsMode, prelude::*, I2CDisplayInterface, Ssd1306};
#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();
fn init_heap() {
const HEAP_SIZE: usize = 32 * 1024;
static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit();
unsafe {
ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
}
}
#[entry]
fn main() -> ! {
const KEY_BLANK: char = ' ';
const KEY_CONFIRM: char = '#';
init_heap();
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the RTC and TIMG watchdog timers
let mut rtc = Rtc::new(peripherals.LPWR);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks
);
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks
);
let mut wdt0 = timer_group0.wdt;
let mut wdt1 = timer_group1.wdt;
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
let mut delay = Delay::new(&clocks);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let pwd : String = String::from("ABBA");
println!("About to initialize the OLED driver SSD1306");
let scl = io.pins.gpio18;
let sda = io.pins.gpio19;
// Map Keypad ROWS and COLS your GPIO pins
let mut keypad = Keypad::new((
io.pins.gpio7.into_pull_up_input(), // R1
io.pins.gpio6.into_pull_up_input(), // R2
io.pins.gpio5.into_pull_up_input(), // R3
io.pins.gpio4.into_pull_up_input(), // R4
),
(
io.pins.gpio3.into_open_drain_output(), // C1
io.pins.gpio2.into_open_drain_output(), // C2
io.pins.gpio1.into_open_drain_output(), // C3
io.pins.gpio0.into_open_drain_output(), // C4
));
// Display
let mut i2c = I2C::new(
peripherals.I2C0,
sda,
scl,
esp_hal::prelude::_fugit_RateExtU32::kHz(100), //100.kHz()
&clocks,
);
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(
interface,
DisplaySize128x64,
DisplayRotation::Rotate0,
).into_buffered_graphics_mode();
display.init().unwrap();
// Create styles used by the drawing operations.
let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
let border_stroke = PrimitiveStyleBuilder::new()
.stroke_color(BinaryColor::On)
.stroke_width(3)
.stroke_alignment(StrokeAlignment::Inside)
.build();
let fill = PrimitiveStyle::with_fill(BinaryColor::On);
let character_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
let yoffset = 14;
// Draw a 3px wide outline around the display.
display
.bounding_box()
.into_styled(border_stroke)
.draw(&mut display);
// Draw a triangle.
Triangle::new(
Point::new(16, 16 + yoffset),
Point::new(16 + 16, 16 + yoffset),
Point::new(16 + 8, yoffset),
)
.into_styled(thin_stroke)
.draw(&mut display);
// Draw a filled square
Rectangle::new(Point::new(52, yoffset), Size::new(16, 16))
.into_styled(fill)
.draw(&mut display);
// Draw a circle with a 3px wide stroke.
Circle::new(Point::new(88, yoffset), 17)
.into_styled(thick_stroke)
.draw(&mut display);
// Draw centered text.
let text = "embedded-graphics";
Text::with_alignment(
text,
display.bounding_box().center() + Point::new(0, 15),
character_style,
Alignment::Center,
)
.draw(&mut display);
//let text_style = MonoTextStyleBuilder::new()
// .font(&FONT_6X10)
// .text_color(BinaryColor::On)
// .build();
//
//Text::with_baseline("Hello world!", Point::zero(), text_style, Baseline::Top)
// .draw(&mut display)
// .unwrap();
//
//Text::with_baseline("Hello Rust!", Point::new(0, 16), text_style, Baseline::Top)
// .draw(&mut display)
// .unwrap();
display.flush().unwrap();
let mut user_input: String = String::new();
loop {
//delay.delay_ms(1000u32); // Force here if a test is needed
let mut key_pressed = keypad.read_char(&mut delay);
if key_pressed != KEY_BLANK {
match key_pressed {
KEY_CONFIRM => match user_input == pwd {
true => { println!("Callback time!"); user_input.clear(); },
false => { println!("Not this time!"); user_input.clear(); },
},
_ => { user_input.push(key_pressed); println!("{}", user_input); },
}
while key_pressed != KEY_BLANK { key_pressed = keypad.read_char(&mut delay); }
}
}
}
Loading
esp32-c3-rust-1
esp32-c3-rust-1