Commit 6a50161e authored by Astro's avatar Astro
Browse files

l0dable: buttons, color

parent afa6cbdc
......@@ -7,10 +7,29 @@ use l0dable::*;
main!(main);
fn main() {
writeln!(UART, "Hello from Rust\r").unwrap();
let display = Display::open();
for x in 0..160 {
display.print(160 - x, 10, "Hello Rust\0", 0xffff, 0);
for t in 0..Display::W {
display.clear(Color::yellow());
display.print(160 - t, 10, b"Hello Rust\0", Color::white(), Color::black());
let b = Buttons::read();
if b.left_bottom() {
display.print(0, 60, b"LB\0", Color::red(), Color::black());
}
if b.right_bottom() {
display.print(80, 60, b"RB\0", Color::red(), Color::black());
}
if b.left_top() {
display.print(0, 10, b"LT\0", Color::red(), Color::black());
}
if b.right_top() {
display.print(80, 10, b"RT\0", Color::red(), Color::black());
}
if b.right_top() {
display.print(80, 30, b"Reset\0", Color::red(), Color::black());
}
display.update();
}
}
use super::bindings::*;
pub struct Buttons {
state: u32,
}
impl Buttons {
pub fn read() -> Self {
let mask =
epic_button_BUTTON_LEFT_BOTTOM |
epic_button_BUTTON_RIGHT_BOTTOM |
epic_button_BUTTON_LEFT_TOP |
epic_button_BUTTON_RIGHT_TOP |
epic_button_BUTTON_RESET;
let state = unsafe { epic_buttons_read(mask as u8) }.into();
Buttons { state }
}
pub fn left_bottom(&self) -> bool {
self.state & epic_button_BUTTON_LEFT_BOTTOM != 0
}
pub fn right_bottom(&self) -> bool {
self.state & epic_button_BUTTON_RIGHT_BOTTOM != 0
}
pub fn left_top(&self) -> bool {
self.state & epic_button_BUTTON_LEFT_TOP != 0
}
pub fn right_top(&self) -> bool {
self.state & epic_button_BUTTON_RIGHT_TOP != 0
}
pub fn reset(&self) -> bool {
self.state & epic_button_BUTTON_RESET != 0
}
}
use super::bindings::*;
#[repr(C)]
pub struct Color(u16);
impl Color {
pub fn red() -> Self {
Self::rgb8(0xff, 0, 0)
}
pub fn blue() -> Self {
Self::rgb8(0xff, 0, 0)
}
pub fn green() -> Self {
Self::rgb8(0xff, 0, 0)
}
pub fn black() -> Self {
Self::rgb8(0, 0, 0)
}
pub fn white() -> Self {
Self::rgb8(0xff, 0xff, 0xff)
}
pub fn yellow() -> Self {
Self::rgb8(0xff, 0xff, 0)
}
pub fn rgb8(r8: u8, g8: u8, b8: u8) -> Self {
let c =
((u16::from(r8) & 0xF8) << 11) |
((u16::from(g8) & 0xFA) << 5) |
(u16::from(b8) & 0xF8);
Color(c)
}
pub fn r(&self) -> u8 {
(self.0 >> 11) as u8
}
pub fn r8(&self) -> u8 {
self.r() << 3
}
pub fn g(&self) -> u8 {
((self.0 >> 5) & 0xFA) as u8
}
pub fn g8(&self) -> u8 {
self.r() << 2
}
pub fn b(&self) -> u8 {
(self.0 & 0xF8) as u8
}
pub fn b8(&self) -> u8 {
self.r() << 3
}
}
#[repr(u32)]
pub enum LineStyle {
Full = disp_linestyle_LINESTYLE_FULL,
Dotted = disp_linestyle_LINESTYLE_DOTTED,
}
pub struct Display;
impl Display {
pub const W: u16 = 160;
pub const H: u16 = 80;
pub fn open() -> Self {
unsafe { epic_disp_open(); }
Display
......@@ -15,18 +82,34 @@ impl Display {
unsafe { epic_disp_update(); }
}
pub fn clear(&self, color: Color) {
unsafe { epic_disp_clear(color.0); }
}
/// s must be 0-terminated
pub fn print(&self, x: u16, y: u16, s: &[u8], fg: u16, bg: u16) {
pub fn print(&self, x: u16, y: u16, s: &[u8], fg: Color, bg: Color) {
unsafe {
epic_disp_print(x, y, s.as_ptr(), fg, bg);
epic_disp_print(x, y, s.as_ptr(), fg.0, bg.0);
}
}
pub fn pixel(&self, x: u16, y: u16, color: u16) {
pub fn pixel(&self, x: u16, y: u16, color: Color) {
unsafe {
epic_disp_pixel(x, y, color);
epic_disp_pixel(x, y, color.0);
}
}
pub fn line(&self, x1: u16, y1: u16, x2: u16, y2: u16, color: Color, linestyle: LineStyle, pixelsize: u16) {
unsafe {
epic_disp_line(x1, y1, x2, y2, color.0, linestyle as u32, pixelsize);
}
}
// pub fn rect(&self) {
// }
// pub fn circle(&self) {
// }
}
impl Drop for Display {
......
......@@ -64,7 +64,9 @@ pub mod bindings {
use bindings::*;
mod display;
pub use display::Display;
pub use display::{Display, Color, LineStyle};
mod buttons;
pub use buttons::Buttons;
pub mod uart;
pub const UART: uart::Uart = uart::Uart;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment