forked from ariel-os/ariel-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
146 lines (126 loc) · 4.45 KB
/
main.rs
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
137
138
139
140
141
142
143
144
145
146
#![no_main]
#![no_std]
#![feature(impl_trait_in_assoc_type)]
#![feature(used_with_arg)]
use ariel_os::debug::{
exit,
log::{defmt, info},
ExitCode,
};
// Imports for using [`ariel_os::storage`]
use ariel_os::storage;
use serde::{Deserialize, Serialize};
/// Example object.
///
/// The serde Serialize / Deserialize traits are required for storage
#[derive(Serialize, Deserialize, Debug, defmt::Format)]
struct MyConfig {
val_one: heapless::String<64>,
val_two: u64,
}
#[ariel_os::task(autostart)]
async fn main() {
info!("Start storage example");
// Storing a primitive type (e.g., u32)
let value: Option<u32> = storage::get("counter").await.unwrap();
let value = if let Some(value) = value {
info!("got counter value {} from storage", value);
value
} else {
info!("no counter value in storage. Is this the first time running this example?");
0
};
if value > 10 {
info!("counter value > 10, aborting test to save flash cycles");
exit(ExitCode::SUCCESS);
}
info!("");
storage::insert("counter", value + 1).await.unwrap();
// By getting the Storage mutex directly, changing e.g., a counter,
// can be done atomically w.r.t. concurrent access from the same firmware:
{
let mut s = storage::lock().await;
let value: Option<u32> = s.get("another_counter").await.unwrap();
let value = value.unwrap_or_default();
s.insert("another_counter", value + 1).await.unwrap();
info!("Old 'another_counter' value at {}", value);
}
info!("");
// Storing a string value
// For insertion, a literal can be used.
info!("Don't try this in your code!");
info!("Storing \"string_key\": \"string_value\" into storage");
storage::insert("string_key", "string_value").await.unwrap();
// Retrieve a string value
if let Some(string) = storage::get::<heapless::String<64>>("string_key")
.await
.unwrap()
{
info!("got heapless string value: \"{}\"", string);
}
if let Some(string) = storage::get::<arrayvec::ArrayString<64>>("string_key")
.await
.unwrap()
{
// no `defmt::Format` for arrayvec, so just print length
info!(
"Attempting to retrieve string value as ArrayString: {:02x}",
string.as_bytes()
);
}
info!("");
// Storing an object
let cfg = MyConfig {
val_one: heapless::String::<64>::try_from("some value").unwrap(),
val_two: 99,
};
info!("Storing cfg object {} as struct", cfg);
storage::insert("my_config", cfg).await.unwrap();
// Getting an object
// Type used for `get()` needs to match what was used for `insert()`.
let cfg: Option<MyConfig> = storage::get("my_config").await.unwrap();
if let Some(cfg) = cfg {
info!("got cfg object: {:?}", cfg);
}
// Getting a value as raw bytes probably does not return what you want due
// to the way postcard works
let cfg_array: Option<arrayvec::ArrayVec<u8, 256>> = storage::get("my_config").await.unwrap();
if let Some(cfg) = cfg_array.as_ref() {
info!(
"Attempting to retrieve cfg as ArrayVec: {:02x}",
cfg.as_slice()
);
}
// Same for byte arrays
let cfg_array: Option<[u8; 10]> = storage::get("my_config").await.unwrap();
if let Some(cfg) = cfg_array.as_ref() {
info!("Attempting to retrieve cfg as array: {:02x}", cfg);
}
info!("");
// raw bytes
let bytes: [u8; 5] = [0, 1, 2, 3, 4];
info!("Storing raw bytes {:02x}", bytes);
storage::insert("some_raw_bytes", bytes).await.unwrap();
let bytes: Option<[u8; 5]> = storage::get("some_raw_bytes").await.unwrap();
if let Some(bytes) = bytes.as_ref() {
info!("got bytes as array: {:02x}", bytes);
}
let bytes: Option<heapless::Vec<u8, 256>> = storage::get("some_raw_bytes").await.unwrap();
if let Some(bytes) = bytes.as_ref() {
info!(
"Attempting to retrieve bytes as heapless vec arr: {:02x}",
bytes
);
}
info!("");
info!("Accessing an object by composite key");
let key1: Option<usize> = storage::get(("key", 1)).await.unwrap();
info!("Original element with key (\"key\", 1) was: {:?}", key1);
if key1 != Some(42) {
info!("Storing 42 there.");
storage::insert(("key", 1), 42usize).await.unwrap();
}
info!("");
info!("Exit storage example");
exit(ExitCode::SUCCESS);
}