2
2
#![ no_main]
3
3
#![ feature( alloc_error_handler) ]
4
4
#![ feature( trait_alias) ]
5
+ #![ allow( incomplete_features) ]
6
+ #![ feature( generic_const_exprs) ]
5
7
8
+ mod flash;
6
9
mod ssd1306_terminal;
7
10
mod uptime;
8
11
mod uptime_delay;
@@ -15,6 +18,7 @@ use core::{alloc::Layout, cell::RefCell};
15
18
use cortex_m_rt:: entry;
16
19
use embedded_hal:: digital:: v2:: InputPin ;
17
20
use embedded_time:: rate:: Extensions ;
21
+ use flash:: FlashSector ;
18
22
use panic_probe as _;
19
23
20
24
use rp_pico as bsp;
@@ -55,6 +59,31 @@ fn oom(_: Layout) -> ! {
55
59
#[ global_allocator]
56
60
static ALLOCATOR : CortexMHeap = CortexMHeap :: empty ( ) ;
57
61
62
+ #[ repr( C ) ]
63
+ #[ derive( Copy , Clone ) ]
64
+ struct Conf {
65
+ format : u16 ,
66
+ scale_unit : f32 ,
67
+ }
68
+
69
+ impl Conf {
70
+ const fn initial ( ) -> Self {
71
+ Self {
72
+ format : 1 ,
73
+ scale_unit : 1.0 ,
74
+ }
75
+ }
76
+ }
77
+
78
+ #[ link_section = ".rodata" ]
79
+ static mut CONF_FLASH_SECTOR : FlashSector < Conf > = FlashSector :: uninit ( ) ;
80
+
81
+ unsafe fn write_conf ( conf : Conf ) {
82
+ core:: sync:: atomic:: compiler_fence ( core:: sync:: atomic:: Ordering :: SeqCst ) ;
83
+ CONF_FLASH_SECTOR . write ( conf) ;
84
+ core:: sync:: atomic:: compiler_fence ( core:: sync:: atomic:: Ordering :: SeqCst ) ;
85
+ }
86
+
58
87
fn init_heap ( ) {
59
88
use core:: mem:: MaybeUninit ;
60
89
const HEAP_SIZE : usize = 128 * 1024 ;
@@ -130,6 +159,17 @@ fn _main() -> ! {
130
159
Uptime :: get_instant,
131
160
) ) ) ;
132
161
162
+ let conf = {
163
+ let conf = unsafe { CONF_FLASH_SECTOR . read ( ) . assume_init ( ) } ;
164
+
165
+ let init_conf = Conf :: initial ( ) ;
166
+ if conf. format == init_conf. format {
167
+ conf
168
+ } else {
169
+ init_conf
170
+ }
171
+ } ;
172
+
133
173
let i2c1 = I2C :: i2c1 (
134
174
pac. I2C1 ,
135
175
pins. gpio2 . into_mode ( ) ,
@@ -147,6 +187,7 @@ fn _main() -> ! {
147
187
)
148
188
. unwrap ( ) ;
149
189
let mut scale = Scale :: < i32 , f32 , 20 > :: default ( ) ;
190
+ scale. set_unit ( conf. scale_unit ) ;
150
191
151
192
schedule. push ( AppTask :: Fn ( FnTask :: new ( move |cx : & mut AppContext | {
152
193
if nau7802. data_available ( ) . unwrap ( ) {
@@ -155,11 +196,14 @@ fn _main() -> ! {
155
196
if scale. is_filled ( ) {
156
197
cx. mq . process ( |m, _push| match m {
157
198
AppMessage :: Tare => {
158
- scale. set_tare ( ) . unwrap ( ) ;
199
+ scale. capture_tare ( ) . unwrap ( ) ;
159
200
MessageProcessingStatus :: Processed
160
201
}
161
202
AppMessage :: Calibrate => {
162
- scale. set_unit ( 100.0 ) . unwrap ( ) ;
203
+ scale. capture_unit ( 100.0 ) . unwrap ( ) ;
204
+ let mut conf = Conf :: initial ( ) ;
205
+ conf. scale_unit = scale. get_unit ( ) ;
206
+ unsafe { write_conf ( conf) }
163
207
MessageProcessingStatus :: Processed
164
208
}
165
209
_ => MessageProcessingStatus :: Ignored ,
0 commit comments