-
-
Save djmcgill/82ea5424945000333a39a69067a7ab37 to your computer and use it in GitHub Desktop.
Rust macro generated module
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
#![macro_escape] | |
use safe_component::port::Port; | |
pub trait EightSegment { | |
unsafe fn new() -> Self; | |
fn display(&mut self, count: u32); | |
} | |
macro_rules! create_eight_segment ( | |
($struct_name: ident, | |
$seg_a: ident, | |
$seg_b: ident, | |
$seg_c: ident, | |
$seg_d: ident, | |
$seg_e: ident, | |
$seg_f: ident, | |
$seg_g: ident, | |
$seg_dp: ident) => {{ | |
pub struct $struct_name; | |
impl EightSegment for $struct_name { | |
unsafe fn new() -> Self { | |
$seg_a.set_pin_dir(true); | |
$seg_b.set_pin_dir(true); | |
$seg_c.set_pin_dir(true); | |
$seg_d.set_pin_dir(true); | |
$seg_e.set_pin_dir(true); | |
$seg_f.set_pin_dir(true); | |
$seg_g.set_pin_dir(true); | |
$seg_dp.set_pin_dir(true); | |
let mut eight_segment = $struct_name; | |
eight_segment.display(0); | |
eight_segment | |
} | |
fn display(&mut self, count: u32) { | |
let (seg_e_on, seg_d_on, seg_c_on, seg_dp_on, seg_b_on, seg_a_on, seg_f_on, seg_g_on) = match count { | |
0 => (true, true, true, false, true, true, true, false), | |
1 => (false, false, true, false, true, false, false, false), | |
2 => (true, true, false, false, true, true, false, true), | |
3 => (false, true, true, false, true, true, false, true), | |
4 => (false, false, true, false, true, false, true, true), | |
5 => (false, true, true, false, false, true, true, true), | |
6 => (true, true, true, false, false, true, true, true), | |
7 => (false, false, true, false, true, true, false, false), | |
8 => (true, true, true, false, true, true, true, true), | |
9 => (false, false, true, false, true, true, true, true), | |
_ => (true, true, true, true, true, true, true, true), | |
}; | |
unsafe { | |
$seg_a.set_pin_out(!seg_a_on); | |
$seg_b.set_pin_out(!seg_b_on); | |
$seg_c.set_pin_out(!seg_c_on); | |
$seg_d.set_pin_out(!seg_d_on); | |
$seg_e.set_pin_out(!seg_e_on); | |
$seg_f.set_pin_out(!seg_f_on); | |
$seg_g.set_pin_out(!seg_g_on); | |
$seg_dp.set_pin_out(!seg_dp_on); | |
} | |
} | |
} | |
$struct_name::new() | |
}} | |
); |
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
// note: the pins are defined as `const` here but in the macro expansion are inlined | |
const SEG_A: Port = D7; | |
const SEG_B: Port = D6; | |
const SEG_C: Port = D3; | |
const SEG_D: Port = D4; | |
const SEG_E: Port = D5; | |
const SEG_F: Port = D8; | |
const SEG_G: Port = D9; | |
const SEG_DP: Port = D2; | |
pub struct EightSegment1; | |
impl EightSegment for EightSegment1 { | |
unsafe fn new() -> Self { | |
unsafe { | |
SEG_A.set_pin_dir(true); | |
SEG_B.set_pin_dir(true); | |
SEG_C.set_pin_dir(true); | |
SEG_D.set_pin_dir(true); | |
SEG_E.set_pin_dir(true); | |
SEG_F.set_pin_dir(true); | |
SEG_G.set_pin_dir(true); | |
SEG_DP.set_pin_dir(true); | |
seg_num(0); | |
} | |
let eight_segment = EightSegment1; | |
eight_segment.display(0); | |
eight_segment | |
} | |
fn display(&mut self, count: u32) { | |
unsafe { | |
let (SEG_E_on, SEG_D_on, SEG_C_on, SEG_DP_on, SEG_B_on, SEG_A_on, SEG_F_on, SEG_G_on) = match count { | |
0 => (true, true, true, false, true, true, true, false), | |
1 => (false, false, true, false, true, false, false, false), | |
2 => (true, true, false, false, true, true, false, true), | |
3 => (false, true, true, false, true, true, false, true), | |
4 => (false, false, true, false, true, false, true, true), | |
5 => (false, true, true, false, false, true, true, true), | |
6 => (true, true, true, false, false, true, true, true), | |
7 => (false, false, true, false, true, true, false, false), | |
8 => (true, true, true, false, true, true, true, true), | |
9 => (false, false, true, false, true, true, true, true), | |
_ => (true, true, true, true, true, true, true, true), | |
}; | |
SEG_A.set_pin_out(!SEG_A_on); | |
SEG_B.set_pin_out(!SEG_B_on); | |
SEG_C.set_pin_out(!SEG_C_on); | |
SEG_D.set_pin_out(!SEG_D_on); | |
SEG_E.set_pin_out(!SEG_E_on); | |
SEG_F.set_pin_out(!SEG_F_on); | |
SEG_G.set_pin_out(!SEG_G_on); | |
SEG_DP.set_pin_out(!SEG_DP_on); | |
} | |
} | |
} |
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
fn main() { | |
let mut digit = unsafe { | |
create_eight_segment!(EightSegment2, D7, D6, D3, D4, D5, D8, D9, D2) | |
}; | |
loop { | |
let s = ... | |
digit.display(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment