-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathveml6075-uv-display-f3.rs
More file actions
136 lines (120 loc) · 4.09 KB
/
veml6075-uv-display-f3.rs
File metadata and controls
136 lines (120 loc) · 4.09 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
136
//! Continuously measure the ultraviolet A and ultraviolet B light sensor data
//! and print it to an SSD1306 OLED display together with the calculated
//! UV index.
//!
//! Introductory blog post with some pictures here:
//! https://blog.eldruin.com/veml6075-uva-uvb-uv-index-light-sensor-driver-in-rust/
//!
//! This example is runs on the STM32F3 Discovery board using I2C1.
//!
//! ```
//! F3 <-> VEML6075 <-> Display
//! GND <-> GND <-> GND
//! 3.3V <-> VCC <-> VDD
//! PB7 <-> SDA <-> SDA
//! PB6 <-> SCL <-> SCL
//! ```
//!
//! Beware that the VEML6075 runs on 3.3V but PB6 and PB7 run on 5V level
//! so make sure to put a logic level shifter in between.
//!
//! Run with:
//! `cargo run --example veml6075-uv-display-f3 --target thumbv7em-none-eabihf`,
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use core::fmt::Write;
use cortex_m_rt::entry;
use embedded_graphics::{
mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
pixelcolor::BinaryColor,
prelude::*,
text::{Baseline, Text},
};
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use stm32f3xx_hal::{self as hal, delay::Delay, pac, prelude::*};
use veml6075::{Calibration, Measurement, Veml6075};
#[entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("VEML6075 example");
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut led = gpioe
.pe9
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
let mut delay = Delay::new(cp.SYST, clocks);
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let mut scl =
gpiob
.pb6
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut sda =
gpiob
.pb7
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
scl.internal_pull_up(&mut gpiob.pupdr, true);
sda.internal_pull_up(&mut gpiob.pupdr, true);
let i2c = hal::i2c::I2c::new(
dp.I2C1,
(scl, sda),
100.kHz().try_into().unwrap(),
clocks,
&mut rcc.apb1,
);
let manager = shared_bus::BusManagerSimple::new(i2c);
let interface = I2CDisplayInterface::new(manager.acquire_i2c());
let mut disp = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
disp.init().unwrap();
disp.flush().unwrap();
let text_style = MonoTextStyleBuilder::new()
.font(&FONT_6X10)
.text_color(BinaryColor::On)
.build();
let mut sensor = Veml6075::new(manager.acquire_i2c(), Calibration::default());
let mut lines: [heapless::String<32>; 3] = [
heapless::String::new(),
heapless::String::new(),
heapless::String::new(),
];
sensor.enable().unwrap();
loop {
// Blink LED 0 to check that everything is actually running.
// If the LED 0 is off, something went wrong.
led.set_high().unwrap();
delay.delay_ms(50_u16);
led.set_low().unwrap();
delay.delay_ms(50_u16);
// If there was an error, it will print 0.00, 0.00, 0.00.
let Measurement { uva, uvb, uv_index } = sensor.read().unwrap_or(Measurement {
uva: 0.0,
uvb: 0.0,
uv_index: 0.0,
});
lines[0].clear();
lines[1].clear();
lines[2].clear();
write!(lines[0], "UVA: {}", uva).unwrap();
write!(lines[1], "UVB: {}", uvb).unwrap();
write!(lines[2], "UV index: {}", uv_index).unwrap();
disp.clear();
for (i, line) in lines.iter().enumerate() {
Text::with_baseline(
line,
Point::new(0, i as i32 * 16),
text_style,
Baseline::Top,
)
.draw(&mut disp)
.unwrap();
}
disp.flush().unwrap();
}
}