-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacklight.rs
More file actions
135 lines (114 loc) · 4.63 KB
/
Copy pathbacklight.rs
File metadata and controls
135 lines (114 loc) · 4.63 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
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
// 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!();
extern crate alloc;
use embedded_graphics::draw_target::DrawTarget;
use embedded_graphics::Drawable;
use embedded_graphics::geometry::Point;
use embedded_graphics::mono_font::ascii::FONT_6X10;
use embedded_graphics::mono_font::MonoTextStyle;
use embedded_graphics::pixelcolor::{Rgb565, RgbColor};
use embedded_graphics::text::Text;
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::{DriveMode, Input, InputConfig, Output, OutputConfig, Pull};
use esp_hal::main;
use esp_hal::ledc::{channel, timer, LSGlobalClkSource, Ledc, LowSpeed};
use esp_hal::ledc::channel::ChannelIFace;
use esp_hal::ledc::timer::TimerIFace;
use esp_hal::spi::master::Spi;
use esp_hal::time::Rate;
use log::info;
use mipidsi::Builder;
use mipidsi::interface::SpiInterface;
use mipidsi::models::ST7789;
use mipidsi::options::{ColorInversion, ColorOrder, Orientation, Rotation};
use esp_hal::spi::master::{Config as SpiConfig};
#[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);
info!("starting");
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);
info!("the board is on now");
// standard display setup
let mut tft_cs = Output::new(peripherals.GPIO12, High, OutputConfig::default());
tft_cs.set_high();
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();
let spi = Spi::new(
peripherals.SPI2,
SpiConfig::default().with_frequency(Rate::from_mhz(40)),
)
.unwrap()
.with_sck(peripherals.GPIO40)
.with_miso(Input::new(
peripherals.GPIO38,
InputConfig::default().with_pull(Pull::Up),
))
.with_mosi(peripherals.GPIO41);
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)
.display_size(240, 320)
.invert_colors(ColorInversion::Inverted)
.color_order(ColorOrder::Rgb)
.orientation(Orientation::new().rotate(Rotation::Deg90))
.init(&mut delay)
.unwrap();
// fill display with white
display.clear(*&Rgb565::WHITE).unwrap();
// write text in black
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::BLACK);
Text::new("Cycle backlight from 0 to 100% over a second.", Point::new(20, 30), style)
.draw(&mut display)
.unwrap();
// setup LEDC as a PWM to control the backlight
let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let mut lstimer0 = ledc.timer::<LowSpeed>(timer::Number::Timer0);
lstimer0.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::LSClockSource::APBClk,
frequency: Rate::from_khz(24),
}).unwrap();
let mut channel0 = ledc.channel(channel::Number::Channel0, peripherals.GPIO42);
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 0,
drive_mode: DriveMode::PushPull,
}).unwrap();
// fade the backlight from 0% to 100% over one second, then back again.
loop {
// You could animate the brightness here if you like
channel0.start_duty_fade(0, 100, 1000).unwrap();
while channel0.is_duty_fade_running() {}
channel0.start_duty_fade(100, 0, 1000).unwrap();
while channel0.is_duty_fade_running() {}
}
}