Skip to content

Commit ec53737

Browse files
committed
add IR example
1 parent 2ea5f1a commit ec53737

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ version = "1.0.2"
3838
cortex-m-rt = "0.6.10"
3939
cortex-m-rtic = "0.5.5"
4040
cortex-m-semihosting = "0.3.5"
41+
embedded-graphics = "0.5"
42+
embedded-sdmmc = "0.2.1"
43+
infrared = "0.11.0"
4144
panic-halt = "0.2.0"
4245
panic-semihosting = "0.5.3"
43-
embedded-sdmmc = "0.2.1"
44-
st7735-lcd = "0.6.1"
45-
embedded-graphics = "0.5"
4646
smart-leds = {git = "https://github.com/smart-leds-rs/smart-leds"}
47+
st7735-lcd = "0.6.1"
4748
ws2812-spi = {git = "https://github.com/smart-leds-rs/ws2812-spi-rs"}
4849

4950
[features]

examples/ir_remote.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![no_std]
2+
#![no_main]
3+
#![deny(warnings)]
4+
5+
extern crate cortex_m;
6+
extern crate cortex_m_rt as rt;
7+
extern crate panic_halt;
8+
extern crate rtic;
9+
extern crate stm32g0xx_hal as hal;
10+
11+
use hal::exti::Event;
12+
use hal::gpio::SignalEdge;
13+
use hal::prelude::*;
14+
use hal::rcc;
15+
use hal::stm32;
16+
use hal::time::*;
17+
use hal::timer::pwm::PwmPin;
18+
use hal::timer::{self, Timer};
19+
use infrared::protocols::nec::NecCommand;
20+
use infrared::{protocols::Nec, Sender};
21+
use rtic::app;
22+
23+
const IR_SAMPLERATE: Hertz = Hertz(20_000);
24+
const STOBE_COMMAND: NecCommand = NecCommand {
25+
addr: 0,
26+
cmd: 15,
27+
repeat: false,
28+
};
29+
30+
type IrPin = PwmPin<stm32::TIM17, timer::Channel1>;
31+
type IrTimer = Timer<stm32::TIM16>;
32+
33+
#[app(device = hal::stm32, peripherals = true)]
34+
const APP: () = {
35+
struct Resources {
36+
timer: IrTimer,
37+
transmitter: Sender<Nec, IrPin>,
38+
exti: stm32::EXTI,
39+
}
40+
41+
#[init]
42+
fn init(mut ctx: init::Context) -> init::LateResources {
43+
let mut rcc = ctx.device.RCC.freeze(rcc::Config::pll());
44+
45+
let gpiob = ctx.device.GPIOB.split(&mut rcc);
46+
let gpioc = ctx.device.GPIOC.split(&mut rcc);
47+
48+
gpioc.pc13.listen(SignalEdge::Falling, &mut ctx.device.EXTI);
49+
50+
let mut timer = ctx.device.TIM16.timer(&mut rcc);
51+
timer.start(IR_SAMPLERATE);
52+
timer.listen();
53+
54+
let carrier_timer = ctx.device.TIM17.pwm(38.khz(), &mut rcc);
55+
let mut ir_pin = carrier_timer.bind_pin(gpiob.pb9);
56+
ir_pin.set_duty(ir_pin.get_max_duty() / 2);
57+
let transmitter = Sender::new(IR_SAMPLERATE.0, ir_pin);
58+
59+
init::LateResources {
60+
timer,
61+
transmitter,
62+
exti: ctx.device.EXTI,
63+
}
64+
}
65+
66+
#[task(binds = TIM16, resources = [timer, transmitter])]
67+
fn timer_tick(ctx: timer_tick::Context) {
68+
ctx.resources.transmitter.tick();
69+
ctx.resources.timer.clear_irq();
70+
}
71+
72+
#[task(binds = EXTI4_15, resources = [exti, transmitter])]
73+
fn button_click(ctx: button_click::Context) {
74+
ctx.resources
75+
.transmitter
76+
.load(&STOBE_COMMAND)
77+
.expect("failed to send IR command");
78+
ctx.resources.exti.unpend(Event::GPIO13);
79+
}
80+
};

0 commit comments

Comments
 (0)