Created
August 6, 2021 12:25
-
-
Save lupyuen/cec1a423062556263a7ba02971862001 to your computer and use it in GitHub Desktop.
BL602 Simulation Events Serialized as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the serde crate for JSON Serialization | |
use serde::{Serialize, Deserialize}; | |
/// Event to be simulated by the BL602 Simulator | |
#[derive(Serialize, Deserialize, Debug)] | |
#[allow(non_camel_case_types)] | |
enum SimulationEvent { | |
/// GPIO Set Output | |
gpio_output_set { | |
pin: i32, | |
value: i32, | |
}, | |
/// Time Delay | |
time_delay { | |
ticks: i32, | |
}, | |
} | |
/// Create a vector of BL602 Simulation Events and serialize as JSON | |
fn main() { | |
// Create a vector of simulation events (i.e. event array) | |
let mut simulation_events: Vec<SimulationEvent> = Vec::new(); | |
// Add a GPIO Set Output event | |
let ev = SimulationEvent::gpio_output_set { | |
pin: 11, | |
value: 0, | |
}; | |
simulation_events.push(ev); | |
// Add a Time Delay event | |
let ev = SimulationEvent::time_delay { | |
ticks: 1000, | |
}; | |
simulation_events.push(ev); | |
// Convert vector of events to a JSON string | |
let serialized = serde_json::to_string(&simulation_events) | |
.unwrap(); | |
// Print the serialized JSON events | |
println!("serialized JSON events = {}", serialized); | |
// Result: | |
// [{"gpio_output_set":{"pin":11,"value":0}}, | |
// {"time_delay":{"ticks":1000}}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment