Skip to content

Commit bebc0cf

Browse files
committed
support cube2
1 parent 889690c commit bebc0cf

File tree

4 files changed

+113
-8
lines changed

4 files changed

+113
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ experimental = ["esp-idf-svc/experimental"]
2525
boards = []
2626
box = []
2727
cube = ["boards"]
28+
cube2 = ["boards"]
2829

2930
[dependencies]
3031
log = "0.4"

src/audio.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn afe_worker(afe_handle: Arc<AFE>, tx: MicTx, trigger_mean_value: f32) -> anyho
185185
let len = cache_buffer.len() as f32;
186186
let mean = cache_buffer
187187
.iter()
188-
.map(|x| x.abs() as f32 / len)
188+
.map(|x| (*x as f32).abs() / len)
189189
.sum::<f32>();
190190

191191
if mean > trigger_mean_value_ || !playing {
@@ -661,9 +661,14 @@ impl BoardsAudioWorker {
661661
let afe_handle = Arc::new(AFE::new());
662662
let afe_handle_ = afe_handle.clone();
663663

664+
#[cfg(feature = "cube2")]
665+
const TRIGGER_MEAN_VALUE: f32 = 400.0;
666+
#[cfg(not(feature = "cube2"))]
667+
const TRIGGER_MEAN_VALUE: f32 = 300.0;
668+
664669
let _afe_r = std::thread::Builder::new()
665670
.stack_size(8 * 1024)
666-
.spawn(|| afe_worker(afe_handle_, tx, 200.0))?;
671+
.spawn(|| afe_worker(afe_handle_, tx, TRIGGER_MEAN_VALUE))?;
667672

668673
audio_task_run(&mut rx, &mut fn_read, &mut fn_write, &afe_handle)
669674
}

src/main.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ fn main() -> anyhow::Result<()> {
3434

3535
crate::hal::audio_init();
3636
ui::lcd_init().unwrap();
37+
#[cfg(feature = "cube2")]
38+
let _backlight = {
39+
let mut backlight = ui::backlight_init(peripherals.pins.gpio13.into()).unwrap();
40+
ui::set_backlight(&mut backlight, 50).unwrap();
41+
backlight
42+
};
3743

3844
log_heap();
3945

@@ -196,7 +202,8 @@ fn main() -> anyhow::Result<()> {
196202

197203
let evt_tx_ = evt_tx.clone();
198204

199-
if cfg!(feature = "box") {
205+
#[cfg(feature = "box")]
206+
{
200207
let bclk = peripherals.pins.gpio21;
201208
let din = peripherals.pins.gpio47;
202209
let dout = peripherals.pins.gpio14;
@@ -220,7 +227,10 @@ fn main() -> anyhow::Result<()> {
220227
}
221228
})
222229
.map_err(|e| anyhow::anyhow!("Failed to spawn audio worker thread: {:?}", e))?;
223-
} else {
230+
}
231+
232+
#[cfg(not(feature = "box"))]
233+
{
224234
let sck = peripherals.pins.gpio5;
225235
let din = peripherals.pins.gpio6;
226236
let dout = peripherals.pins.gpio7;
@@ -290,6 +300,47 @@ fn main() -> anyhow::Result<()> {
290300
vol_down_btn.set_pull(esp_idf_svc::hal::gpio::Pull::Up)?;
291301
vol_down_btn.set_interrupt_type(esp_idf_svc::hal::gpio::InterruptType::PosEdge)?;
292302

303+
let evt_tx_vol_down = evt_tx.clone();
304+
b.spawn(async move {
305+
loop {
306+
let _ = vol_down_btn.wait_for_falling_edge().await;
307+
log::info!("Button vol down pressed {:?}", vol_down_btn.get_level());
308+
if evt_tx_vol_down
309+
.send(app::Event::Event(app::Event::VOL_DOWN))
310+
.await
311+
.is_err()
312+
{
313+
log::error!("Failed to send VOL_DOWN event");
314+
break;
315+
}
316+
}
317+
});
318+
} else if cfg!(feature = "cube2") {
319+
let mut vol_up_btn = esp_idf_svc::hal::gpio::PinDriver::input(peripherals.pins.gpio40)?;
320+
vol_up_btn.set_pull(esp_idf_svc::hal::gpio::Pull::Up)?;
321+
vol_up_btn.set_interrupt_type(esp_idf_svc::hal::gpio::InterruptType::PosEdge)?;
322+
323+
let evt_tx_vol_up = evt_tx.clone();
324+
b.spawn(async move {
325+
loop {
326+
let _ = vol_up_btn.wait_for_falling_edge().await;
327+
log::info!("Button vol up pressed {:?}", vol_up_btn.get_level());
328+
if evt_tx_vol_up
329+
.send(app::Event::Event(app::Event::VOL_UP))
330+
.await
331+
.is_err()
332+
{
333+
log::error!("Failed to send VOL_UP event");
334+
break;
335+
}
336+
}
337+
});
338+
339+
let mut vol_down_btn =
340+
esp_idf_svc::hal::gpio::PinDriver::input(peripherals.pins.gpio39)?;
341+
vol_down_btn.set_pull(esp_idf_svc::hal::gpio::Pull::Up)?;
342+
vol_down_btn.set_interrupt_type(esp_idf_svc::hal::gpio::InterruptType::PosEdge)?;
343+
293344
let evt_tx_vol_down = evt_tx.clone();
294345
b.spawn(async move {
295346
loop {

src/ui.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use embedded_graphics::{
1313
},
1414
};
1515
use embedded_text::TextBox;
16-
use esp_idf_svc::sys::EspError;
16+
use esp_idf_svc::{
17+
hal::{self, gpio::Pin, ledc::LedcDriver},
18+
sys::{ledc_timer_config_t, EspError},
19+
};
1720
use u8g2_fonts::U8g2TextStyle;
1821

1922
pub type ColorFormat = Rgb565;
@@ -35,16 +38,21 @@ fn init_spi() -> Result<(), EspError> {
3538
use esp_idf_svc::sys::*;
3639
const GPIO_NUM_NC: i32 = -1;
3740

38-
#[cfg(not(feature = "cube"))]
41+
#[cfg(all(not(feature = "cube"), not(feature = "cube2")))]
3942
const DISPLAY_MOSI_PIN: i32 = 47;
40-
#[cfg(not(feature = "cube"))]
43+
#[cfg(all(not(feature = "cube"), not(feature = "cube2")))]
4144
const DISPLAY_CLK_PIN: i32 = 21;
4245

4346
#[cfg(feature = "cube")]
4447
const DISPLAY_MOSI_PIN: i32 = 41;
4548
#[cfg(feature = "cube")]
4649
const DISPLAY_CLK_PIN: i32 = 42;
4750

51+
#[cfg(feature = "cube2")]
52+
const DISPLAY_MOSI_PIN: i32 = 10;
53+
#[cfg(feature = "cube2")]
54+
const DISPLAY_CLK_PIN: i32 = 9;
55+
4856
let mut buscfg = spi_bus_config_t::default();
4957
buscfg.__bindgen_anon_1.mosi_io_num = DISPLAY_MOSI_PIN;
5058
buscfg.__bindgen_anon_2.miso_io_num = GPIO_NUM_NC;
@@ -66,11 +74,19 @@ static mut ESP_LCD_PANEL_HANDLE: esp_idf_svc::sys::esp_lcd_panel_handle_t = std:
6674
#[cfg(feature = "boards")]
6775
fn init_lcd() -> Result<(), EspError> {
6876
use esp_idf_svc::sys::*;
69-
#[cfg(not(feature = "cube"))]
77+
#[cfg(all(not(feature = "cube"), not(feature = "cube2")))]
7078
const DISPLAY_CS_PIN: i32 = 41;
7179
#[cfg(feature = "cube")]
7280
const DISPLAY_CS_PIN: i32 = 21;
81+
#[cfg(feature = "cube2")]
82+
const DISPLAY_CS_PIN: i32 = 14;
83+
84+
#[cfg(not(feature = "cube2"))]
7385
const DISPLAY_DC_PIN: i32 = 40;
86+
87+
#[cfg(feature = "cube2")]
88+
const DISPLAY_DC_PIN: i32 = 8;
89+
7490
::log::info!("Install panel IO");
7591
let mut panel_io: esp_lcd_panel_io_handle_t = std::ptr::null_mut();
7692
let mut io_config = esp_lcd_panel_io_spi_config_t::default();
@@ -86,7 +102,12 @@ fn init_lcd() -> Result<(), EspError> {
86102
})?;
87103

88104
::log::info!("Install LCD driver");
105+
#[cfg(not(feature = "cube2"))]
89106
const DISPLAY_RST_PIN: i32 = 45;
107+
108+
#[cfg(feature = "cube2")]
109+
const DISPLAY_RST_PIN: i32 = 18;
110+
90111
let mut panel_config = esp_lcd_panel_dev_config_t::default();
91112
let mut panel: esp_lcd_panel_handle_t = std::ptr::null_mut();
92113

@@ -145,6 +166,33 @@ pub fn lcd_init() -> Result<(), EspError> {
145166
Ok(())
146167
}
147168

169+
#[allow(unused)]
170+
pub fn backlight_init(bl_pin: hal::gpio::AnyIOPin) -> anyhow::Result<LedcDriver<'static>> {
171+
let config = hal::ledc::config::TimerConfig::new()
172+
.resolution(hal::ledc::Resolution::Bits13)
173+
.frequency(hal::units::Hertz(5000));
174+
let time = unsafe { hal::ledc::TIMER0::new() };
175+
let timer_driver = hal::ledc::LedcTimerDriver::new(time, &config)?;
176+
177+
let ledc_driver =
178+
hal::ledc::LedcDriver::new(unsafe { hal::ledc::CHANNEL0::new() }, timer_driver, bl_pin)?;
179+
180+
Ok(ledc_driver)
181+
}
182+
183+
const LEDC_MAX_DUTY: u32 = (1 << 13) - 1;
184+
#[allow(unused)]
185+
pub fn set_backlight<'d>(
186+
ledc_driver: &mut hal::ledc::LedcDriver<'d>,
187+
light: u8,
188+
) -> anyhow::Result<()> {
189+
let light = 100.min(light) as u32;
190+
let duty = LEDC_MAX_DUTY - (81 * (100 - light));
191+
let duty = if light == 0 { 0 } else { duty };
192+
ledc_driver.set_duty(duty)?;
193+
Ok(())
194+
}
195+
148196
#[inline(always)]
149197
fn get_esp_lcd_panel_handle() -> esp_idf_svc::sys::esp_lcd_panel_handle_t {
150198
#[cfg(feature = "boards")]

0 commit comments

Comments
 (0)