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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
use std::str::FromStr; use std::result; use thiserror::Error; use apigpio::*; use crate::ledpins; struct Sequence { desc : String, pulses : Vec<Pulse>, } type SeqTime = u32; static START_BIT : SeqTime = 1540; static START_DELAY : SeqTime = 80; static SPACE : SeqTime = 1040; static ZERO_BIT : SeqTime = 220; static ZERO_DELAY : SeqTime = SPACE - ZERO_BIT; static ONE_BIT : SeqTime = 740; static ONE_DELAY : SeqTime = SPACE - ONE_BIT; static EOS_DELAY : SeqTime = 7600; pub const GPIO : u32 = ledpins::XMIT; #[derive(Copy,Clone,Debug,Eq,PartialEq)] pub struct CollarKey (pub u16); #[derive(Error,Debug,Copy,Clone,Eq,PartialEq)] pub enum CollarKeyParseError { #[error("collar key is wrong length, should be 4 characters")] WrongLength, #[error("collar contains invalid hex digits")] BadCharacters, } impl FromStr for CollarKey { type Err = CollarKeyParseError; fn from_str(s : &str) -> result::Result<Self, Self::Err> { use CollarKeyParseError::*; if s.len() != 4 { Err(WrongLength)? } Ok(CollarKey( u16::from_str_radix(s, 16).map_err(|_| BadCharacters)? )) } } impl Sequence { fn new() -> Sequence { let mut s = Sequence { desc : String::new(), pulses : Vec::with_capacity(100), }; s.pulse_onoff(START_BIT, START_DELAY, "START"); s } fn finish(mut self) -> (Vec<Pulse>, String) { self.desc += " EOS"; self.pulses.push(Pulse { on_mask : 0, off_mask : 0, us_delay : EOS_DELAY }); (self.pulses, self.desc) } fn pulse_on(&mut self, us : SeqTime) { self.pulses.push(Pulse { on_mask : 1 << GPIO, off_mask : 0, us_delay : us }); } fn pulse_off(&mut self, us : SeqTime) { self.pulses.push(Pulse { on_mask : 0, off_mask : 1 << GPIO, us_delay : us }); } fn pulse_onoff(&mut self, on_time : SeqTime, off_time : SeqTime, what : &str) { self.desc += what; self.pulse_on(on_time); self.pulse_off(off_time); } fn pulse_bit(&mut self, bit : bool) { match bit { false => self.pulse_onoff(ZERO_BIT, ZERO_DELAY, "0"), true => self.pulse_onoff(ONE_BIT, ONE_DELAY, "1"), } } fn pulse_bits(&mut self, count : usize, bits : u32) { self.desc += " "; assert!(count > 0); for i in (0..count).rev() { self.pulse_bit( bits & (1 << i) != 0 ); } } fn pulse_bits_rev(&mut self, count : usize, bits : u32) { self.desc += " "; assert!(count > 0); for i in 0..count { self.pulse_bit( bits & (1 << i) == 0 ); } } } #[derive(Debug,Copy,Clone)] pub enum Mode { Flash, Beep, Vibrate, Shock, } use Mode::*; impl Mode { pub fn needs_power(self) -> bool { match self { Flash => false, Vibrate => true, Beep => false, Shock => true, } } } #[derive(Debug)] pub struct ParseModeError (); impl std::str::FromStr for Mode { type Err = ParseModeError; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Ok(match s.to_ascii_lowercase().as_ref() { "f" | "flash" => Flash, "b" | "beep" => Beep, "v" | "vibrate" => Vibrate, "shock" => Shock, _ => Err(ParseModeError())?, }) } } pub struct Command { pub key : CollarKey, pub channel : u8, pub mode : Mode, pub power : u8, } pub fn mk_pulses(c : &Command) -> (Vec<Pulse>, String) { let chanseq : u32 = match c.channel { 0 => 0b000, 1 => 0b111, _ => panic!("bad channel"), }; let modeseq : u32 = match c.mode { Flash => 0b1000, Beep => 0b0100, Vibrate => 0b0010, Shock => 0b0001, }; assert!( c.power <= 100 ); let mut seq = Sequence::new(); seq.pulse_bits(1, 0b1); seq.pulse_bits(3, chanseq); seq.pulse_bits(4, modeseq); seq.pulse_bits(16, c.key.0 as u32); seq.pulse_bits(8, c.power as u32); seq.pulse_bits_rev(4, modeseq); seq.pulse_bits_rev(3, chanseq); seq.pulse_bits(2, 0b00); seq.finish() }