#![no_main]
#![no_std]
#![allow(unused_variables)]
use cortex_m;
use cortex_m_rt::entry;
use rtt_target::{rprintln, rtt_init_print};
#[cfg(feature = "stm32f412")]
use stm32f4xx_hal::fsmc_lcd::ChipSelect1;
#[cfg(feature = "stm32f413")]
use stm32f4xx_hal::fsmc_lcd::ChipSelect3;
use stm32f4xx_hal::{
fsmc_lcd::{FsmcLcd, LcdPins, Timing},
gpio::Speed,
pac,
prelude::*,
rcc::Rcc,
};
use embedded_graphics::{
pixelcolor::Rgb565,
prelude::*,
primitives::{Circle, PrimitiveStyle},
};
#[cfg(feature = "stm32f413")]
use stm32f4xx_hal::fmpi2c::FMPI2c;
#[cfg(feature = "stm32f412")]
use stm32f4xx_hal::i2c::I2c;
#[allow(unused_imports)]
use panic_semihosting;
use ft6x06::{long_hard_reset, Ft6X06};
use st7789::*;
#[entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("Started");
let p = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let rcc: Rcc = p.RCC.constrain();
let clocks = rcc.cfgr.sysclk(100.MHz()).freeze();
let mut delay = cp.SYST.delay(&clocks);
let gpiob = p.GPIOB.split();
let gpioc = p.GPIOC.split();
let gpiod = p.GPIOD.split();
let gpioe = p.GPIOE.split();
let gpiof = p.GPIOF.split();
let gpiog = p.GPIOG.split();
let lcd_pins = LcdPins {
data: (
gpiod.pd14.into_alternate(),
gpiod.pd15.into_alternate(),
gpiod.pd0.into_alternate(),
gpiod.pd1.into_alternate(),
gpioe.pe7.into_alternate(),
gpioe.pe8.into_alternate(),
gpioe.pe9.into_alternate(),
gpioe.pe10.into_alternate(),
gpioe.pe11.into_alternate(),
gpioe.pe12.into_alternate(),
gpioe.pe13.into_alternate(),
gpioe.pe14.into_alternate(),
gpioe.pe15.into_alternate(),
gpiod.pd8.into_alternate(),
gpiod.pd9.into_alternate(),
gpiod.pd10.into_alternate(),
),
address: gpiof.pf0.into_alternate(),
read_enable: gpiod.pd4.into_alternate(),
write_enable: gpiod.pd5.into_alternate(),
#[cfg(feature = "stm32f413")]
chip_select: ChipSelect3(gpiog.pg10.into_alternate()),
#[cfg(feature = "stm32f412")]
chip_select: ChipSelect1(gpiod.pd7.into_alternate()),
};
#[cfg(feature = "stm32f413")]
let mut backlight_control = gpioe.pe5.into_push_pull_output();
#[cfg(feature = "stm32f412")]
let mut backlight_control = gpiof.pf5.into_push_pull_output();
backlight_control.set_high();
#[cfg(feature = "stm32f413")]
let mut lcd_reset = gpiob.pb13.into_push_pull_output().speed(Speed::VeryHigh);
#[cfg(feature = "stm32f412")]
let lcd_reset = gpiod.pd11.into_push_pull_output().speed(Speed::VeryHigh);
#[cfg(feature = "stm32f412")]
let mut ts_reset = gpiof.pf12.into_push_pull_output().speed(Speed::VeryHigh);
#[cfg(feature = "stm32f412")]
long_hard_reset(&mut ts_reset, &mut delay).expect("long hard reset");
#[cfg(feature = "stm32f413")]
long_hard_reset(&mut lcd_reset, &mut delay).expect("long hard reset");
let mut _te = gpiob.pb14.into_floating_input();
let write_timing = Timing::default().data(3).address_setup(3).bus_turnaround(0);
let read_timing = Timing::default().data(8).address_setup(8).bus_turnaround(0);
let (_fsmc, interface) = FsmcLcd::new(p.FSMC, lcd_pins, &read_timing, &write_timing);
let mut disp = ST7789::new(
interface,
Some(lcd_reset),
Some(backlight_control),
240,
240,
);
disp.init(&mut delay).unwrap();
disp.clear(Rgb565::BLACK).unwrap();
rprintln!("The orientation set is {}", disp.orientation() as u8);
rprintln!("Connecting to I2c");
#[cfg(feature = "stm32f412")]
let mut i2c = {
I2c::new(
p.I2C1,
(
gpiob.pb6.into_alternate_open_drain(), gpiob.pb7.into_alternate_open_drain(), ),
400.kHz(),
&clocks,
)
};
#[cfg(feature = "stm32f413")]
let mut i2c = {
FMPI2c::new(
p.FMPI2C1,
(
gpioc.pc6.into_alternate_open_drain(), gpioc.pc7.into_alternate_open_drain(), ),
400.kHz(),
)
};
#[cfg(feature = "stm32f412")]
let ts_int = gpiog.pg5.into_pull_down_input();
#[cfg(feature = "stm32f413")]
let ts_int = gpioc.pc1.into_pull_down_input();
let mut touch = Ft6X06::new(&i2c, 0x38, ts_int).unwrap();
let tsc = touch.ts_calibration(&mut i2c, &mut delay);
match tsc {
Err(e) => rprintln!("Error {} from ts_calibration", e),
Ok(u) => rprintln!("ts_calibration returned {}", u),
}
rprintln!("If nothing happens - touch the screen!");
loop {
let t = touch.detect_touch(&mut i2c);
let mut num: u8 = 0;
match t {
Err(_e) => rprintln!("Error from fetching number of touches"),
Ok(n) => {
num = n;
if num != 0 {
rprintln!("Number of touches: {}", num)
};
}
}
if num > 0 {
let t = touch.get_touch(&mut i2c, 1);
match t {
Err(_e) => rprintln!("Error fetching touch data"),
Ok(n) => {
rprintln!(
"Touch: {:>3}x{:>3} - weight: {:>3} misc: {}",
n.x,
n.y,
n.weight,
n.misc
);
Circle::new(Point::new(i32::from(n.y), 240 - i32::from(n.x)), 20)
.into_styled(PrimitiveStyle::with_stroke(Rgb565::RED, 1))
.draw(&mut disp)
.unwrap();
}
}
}
}
}