#![no_std]
#![no_main]


use esp_hal::{
    clock::ClockControl,
    peripherals::Peripherals,
    gpio::*,
    prelude::*,
    spi,
    timer::TimerGroup,
    Rtc,
    IO,
    Delay,
};


/* Display and graphics stuff */
use max7219::connectors::PinConnector;
use max7219::MAX7219;
use max7219::DecodeMode;

use esp_max7219_nostd::{prepare_display, show_moving_text_in_loop, remove_gaps_in_display_text, draw_point, clear_with_state};
use esp_max7219_nostd::mappings::SingleDisplayData;

use esp_backtrace as _;
use esp_println::println;


#[entry]
fn main() -> ! {
    extern crate alloc;
    #[global_allocator]
    static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

    const HEAP_SIZE: usize = 32 * 1024;
    static mut HEAP: core::mem::MaybeUninit<[u8; HEAP_SIZE]> = core::mem::MaybeUninit::uninit();

    unsafe {
        ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
    }

    let peripherals = Peripherals::take();
    let mut system = peripherals.SYSTEM.split();
    let mut clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    let mut delay = Delay::new(&clocks);

    let din = io.pins.gpio23.into_push_pull_output();
    let cs = io.pins.gpio15.into_push_pull_output();
    let clk = io.pins.gpio18.into_push_pull_output();

    let mut display = MAX7219::from_pins(7, din, cs, clk).unwrap();
    /* this variable will contain actual configuration of display (which points are lit) */
    prepare_display(&mut display, 7, 0x5);
    show_moving_text_in_loop(
        &mut display, 
        "Hello, Espressif!",
        7, 
        25, 
        2, 
        &mut delay,
    );

    loop {}
}