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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/// Copyright © 2019 Felix Obenhuber
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
//
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.

use error::Error;
use log::debug;
use std::marker::PhantomData;
use std::str;

/// Error
pub mod error {
    use failure::Fail;

    #[derive(Debug, Fail)]
    pub enum Error {
        #[fail(display = "invalid group identifier: {}", _0)]
        InvalidGroup(String),
        #[fail(display = "invalid device identifier: {}", _0)]
        InvalidDevice(String),
        #[fail(display = "invalid state: {}. Try on, off, 1, 0, true, false", _0)]
        InvalidState(String),
    }
}

/// A Device
#[derive(Clone, Debug, PartialEq)]
pub enum Device {
    A,
    B,
    C,
    D,
    E,
}

impl From<Device> for u8 {
    fn from(d: Device) -> u8 {
        match d {
            Device::A => 1,
            Device::B => 2,
            Device::C => 3,
            Device::D => 4,
            Device::E => 5,
        }
    }
}

impl str::FromStr for Device {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "0" | "a" | "A" | "10000" => Ok(Device::A),
            "1" | "b" | "B" | "01000" => Ok(Device::B),
            "2" | "c" | "C" | "00100" => Ok(Device::C),
            "3" | "d" | "D" | "00010" => Ok(Device::D),
            "4" | "e" | "E" | "00001" => Ok(Device::E),
            _ => Err(Error::InvalidDevice(s.into())),
        }
    }
}

/// State to switch a socket to
#[derive(Clone, Debug, PartialEq)]
pub enum State {
    On,
    Off,
}

impl str::FromStr for State {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "On" | "on" | "1" | "true" => Ok(State::On),
            "Off" | "off" | "0" | "false" => Ok(State::Off),
            _ => Err(Error::InvalidState(s.into())),
        }
    }
}

/// Value to set a GPIO to
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Low,
    High,
}

/// Encoding
pub trait Encoding {
    fn encode(group: &str, device: &Device, state: &State) -> Result<Vec<u8>, Error>;
}

/// Encoding A - check [rc-switch](https://github.com/sui77/rc-switch/) for details
pub struct EncodingA;

impl Encoding for EncodingA {
    fn encode(group: &str, device: &Device, state: &State) -> Result<Vec<u8>, Error> {
        if group.len() != 5 || group.chars().any(|c| c != '0' && c != '1') {
            return Err(Error::InvalidGroup(group.into()));
        }
        let chars = group.chars();

        let device = match device {
            Device::A => "10000",
            Device::B => "01000",
            Device::C => "00100",
            Device::D => "00010",
            Device::E => "00001",
        };

        let chars = chars.chain(device.chars());

        let chars = match *state {
            State::On => chars.chain("10".chars()),
            State::Off => chars.chain("01".chars()),
        };

        Ok(chars
            .map(|c| match c {
                '0' => b'F',
                _ => b'0',
            })
            .collect())
    }
}

/// Encoding B - check [rc-switch](https://github.com/sui77/rc-switch/) for details
pub struct EncodingB;

impl Encoding for EncodingB {
    fn encode(_group: &str, _device: &Device, _state: &State) -> Result<Vec<u8>, Error> {
        unimplemented!()
    }
}

/// Encoding C - check [rc-switch](https://github.com/sui77/rc-switch/) for details
pub struct EncodingC;

impl Encoding for EncodingC {
    fn encode(_group: &str, _device: &Device, _state: &State) -> Result<Vec<u8>, Error> {
        unimplemented!()
    }
}

/// Interface for GPIO control
pub trait Pin {
    fn set(&self, value: &Value) -> Result<(), Error>;
}

/// Handle to a Funksteckdose system
#[derive(Debug)]
pub struct Funksteckdose<T: Pin, E: Encoding, P: Protocol> {
    pin: T,
    repeat_transmit: usize,
    protocol: PhantomData<P>,
    encoding: PhantomData<E>,
}

impl<T: Pin, E: Encoding, P: Protocol> Funksteckdose<T, E, P> {
    /// Create a new instance with a given pin and default protocol
    /// ```
    /// type Funksteckdose = funksteckdose::Funksteckdose<WiringPiPin, EncodingA, Protocol1>;
    /// let pin = WiringPiPin::new(0);
    /// let d: Funksteckdose = Funksteckdose::new(pin);
    /// ```
    pub fn new(pin: T) -> Funksteckdose<T, E, P> {
        Self::with_repeat_transmit(pin, 10)
    }

    /// Create a new instance with a given pin and transmit count
    /// ```
    /// type Funksteckdose = funksteckdose::Funksteckdose<WiringPiPin, EncodingA, Protocol1>;
    /// let pin = WiringPiPin::new(0);
    /// let d: Funksteckdose = Funksteckdose::with_repeat_transmit(pin, 5);
    /// ```
    pub fn with_repeat_transmit(pin: T, repeat_transmit: usize) -> Funksteckdose<T, E, P> {
        Funksteckdose {
            pin,
            repeat_transmit,
            protocol: PhantomData,
            encoding: PhantomData,
        }
    }

    /// Send a control sequence to give group and device.
    /// The group is coded like the dip switches in the devices e.g "10010"
    /// ```
    /// type Funksteckdose = funksteckdose::Funksteckdose<WiringPiPin, EncodingA, Protocol1>;
    /// let pin = WiringPiPin::new(0);
    /// let d: Funksteckdose = Funksteckdose::with_repeat_transmit(pin, 5);
    /// d.send("10001", &Device::A, &State::On).expect("Failed to send");
    /// ```
    pub fn send(&self, group: &str, device: &Device, state: &State) -> Result<(), Error> {
        let code_word = E::encode(group, device, state)?;
        self.send_tri_state(&code_word)
    }

    fn send_tri_state(&self, code_word: &[u8]) -> Result<(), Error> {
        let code = code_word.iter().fold(0u64, |mut code, c| {
            code <<= 2u64;
            match c {
                b'0' => (),           // bit pattern 00
                b'F' => code |= 1u64, // bit pattern 01
                b'1' => code |= 3u64, // bit pattern 11
                _ => unreachable!(),
            }
            code
        });

        // Transmit the first 'length' bits of the integer 'code'. The
        // bits are sent from MSB to LSB, i.e., first the bit at position length-1,
        // then the bit at position length-2, and so on, till finally the bit at position 0.
        let (first, second) = if P::values().inverted_signal {
            (Value::Low, Value::High)
        } else {
            (Value::High, Value::Low)
        };
        let length = code_word.len() * 2;
        for _ in 0..self.repeat_transmit {
            debug!("Sending code: {:#X} length: {}", code, length);
            let one = P::values().one;
            let zero = P::values().zero;
            for i in (0..length).rev() {
                let s = if code & (1 << i) != 0 { &one } else { &zero };
                self.transmit(s, &first, &second)?;
            }
            self.transmit(&P::values().sync_factor, &first, &second)?;
        }

        // Disable transmit after sending (i.e., for inverted protocols)
        self.pin.set(&Value::Low)?;
        Ok(())
    }

    fn transmit(&self, pulses: &HighLow, first: &Value, second: &Value) -> Result<(), Error> {
        self.pin.set(first)?;
        Self::delay((P::values().pulse_length * pulses.high) as u32);
        self.pin.set(second)?;
        Self::delay((P::values().pulse_length * pulses.low) as u32);
        Ok(())
    }

    fn delay(micros: u32) {
        if micros > 0 {
            let now = std::time::Instant::now();
            let micros = u128::from(micros);
            while now.elapsed().as_micros() < micros {}
        }
    }
}

/// Number of pulses
#[derive(Clone, Debug)]
pub struct HighLow {
    pub high: u64,
    pub low: u64,
}

impl HighLow {
    fn new(high: u64, low: u64) -> HighLow {
        HighLow { high, low }
    }
}

/// Format for protocol definitions
#[derive(Clone, Debug)]
pub struct ProtocolValues {
    pulse_length: u64,
    sync_factor: HighLow,
    zero: HighLow,
    one: HighLow,
    inverted_signal: bool,
}

/// A protocol definition
pub trait Protocol {
    fn values() -> ProtocolValues;
}

/// Protocol 1
pub struct Protocol1;

impl Protocol for Protocol1 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 350,
            sync_factor: HighLow::new(1, 31),
            zero: HighLow::new(1, 3),
            one: HighLow::new(3, 1),
            inverted_signal: false,
        }
    }
}

/// Protocol 2
pub struct Protocol2;

impl Protocol for Protocol2 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 650,
            sync_factor: HighLow::new(1, 10),
            zero: HighLow::new(1, 2),
            one: HighLow::new(2, 1),
            inverted_signal: false,
        }
    }
}

/// Protocol 3
pub struct Protocol3;

impl Protocol for Protocol3 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 100,
            sync_factor: HighLow::new(30, 71),
            zero: HighLow::new(4, 11),
            one: HighLow::new(9, 6),
            inverted_signal: false,
        }
    }
}

/// Protocol 4
pub struct Protocol4;

impl Protocol for Protocol4 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 380,
            sync_factor: HighLow::new(1, 6),
            zero: HighLow::new(1, 3),
            one: HighLow::new(3, 1),
            inverted_signal: false,
        }
    }
}

/// Protocol 4
pub struct Protocol5;

impl Protocol for Protocol5 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 500,
            sync_factor: HighLow::new(6, 14),
            zero: HighLow::new(1, 2),
            one: HighLow::new(2, 1),
            inverted_signal: false,
        }
    }
}


/// Protocol HT6P20B
pub struct ProtocolHT6P20B;

impl Protocol for ProtocolHT6P20B {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 450,
            sync_factor: HighLow::new(23, 1),
            zero: HighLow::new(1, 2),
            one: HighLow::new(2, 1),
            inverted_signal: true,
        }
    }
}

/// Protocol HS2303-PT, i. e. used in AUKEY Remote
pub struct ProtocolHS2303;

impl Protocol for ProtocolHS2303 {
    fn values() -> ProtocolValues {
        ProtocolValues {
            pulse_length: 150,
            sync_factor: HighLow::new(2, 62),
            zero: HighLow::new(1, 6),
            one: HighLow::new(6, 1),
            inverted_signal: false,
        }
    }
}

/// A implementation of Pin to be used with wiringpi on a Raspberry
///
///```
/// let pin = WiringPiPin::new(0);
/// let funksteckdose = Funksteckdose::new(pin, 1).unwrap();
/// funksteckdose.send("10011", "10000", State::On);
///```
#[cfg(feature = "wiringpi")]
pub mod wiringpi {
    use super::{Error, Pin, Value};

    pub struct WiringPiPin {
        pin: wiringpi::pin::OutputPin<wiringpi::pin::WiringPi>,
    }

    impl WiringPiPin {
        pub fn new(pin: u16) -> WiringPiPin {
            let pi = wiringpi::setup();
            WiringPiPin {
                pin: pi.output_pin(pin),
            }
        }
    }

    impl Pin for WiringPiPin {
        fn set(&self, value: &Value) -> Result<(), Error> {
            match value {
                Value::High => self.pin.digital_write(wiringpi::pin::Value::High),
                Value::Low => self.pin.digital_write(wiringpi::pin::Value::Low),
            }
            Ok(())
        }
    }
}