aboutsummaryrefslogtreecommitdiff
path: root/src/serial.rs
blob: 577723d8e7b0d58e467623d964ee63d4a5b51644 (plain)
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
use std::fs::File;
use std::io::{Read, Write};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;

pub trait Serial {
    // Should not be blocking
    fn write(&mut self, byte: u8);
    fn read(&mut self) -> u8;

    fn new_transfer(&mut self) -> bool; // since last read
    fn clock_master(&mut self) -> bool;

    fn set_clock_master(&mut self, clock_master: bool);
}

pub struct UnconnectedSerial {}

impl Serial for UnconnectedSerial {
    fn write(&mut self, byte: u8) {
        println!("Writing {} to unconnected serial", byte);
    }

    fn read(&mut self) -> u8 {
        println!("Reading 0 from unconnected serial");
        0
    }

    fn new_transfer(&mut self) -> bool {
        false
    }

    fn clock_master(&mut self) -> bool {
        false
    }

    fn set_clock_master(&mut self, clock_master: bool) {}
}

pub struct FIFOPackage {
    t: bool,
    value: u8,
}

pub struct FIFOSerial {
    input: Receiver<u8>,
    output: Sender<FIFOPackage>,
    clock_change: Receiver<bool>,
    last_read_byte: u8,
    clock_master: bool,
}

impl FIFOSerial {
    pub fn new(input_path: String, output_path: String) -> FIFOSerial {
        let (tx, input) = mpsc::channel::<u8>();
        let (clock_tx, clock_change) = mpsc::channel::<bool>();
        thread::spawn(move || {
            let mut input_f = File::open(input_path).unwrap();
            loop {
                let mut byte = [0, 0];

                input_f.read(&mut byte).unwrap();
                if byte[0] == 1 {
                    tx.send(byte[1]).unwrap();
                } else {
                    clock_tx.send(byte[1] == 0).unwrap();
                }
            }
        });

        let (output, rx) = mpsc::channel::<FIFOPackage>();
        thread::spawn(move || {
            let mut output_f = File::create(output_path).unwrap();
            for b in rx.iter() {
                if b.t {
                    output_f.write(&[1, b.value]).unwrap();
                } else {
                    output_f.write(&[0, b.value]).unwrap();
                }
            }
        });

        FIFOSerial {
            input,
            output,
            clock_change,
            last_read_byte: 0xff,
            clock_master: false,
        }
    }
}

impl Serial for FIFOSerial {
    fn write(&mut self, byte: u8) {
        println!("Writing {} to fifo serial", byte);
        self.output.send(FIFOPackage {
            t: true,
            value: byte,
        });
    }

    fn read(&mut self) -> u8 {
        println!("Reading {} from fifo serial", self.last_read_byte);
        self.last_read_byte
    }

    fn new_transfer(&mut self) -> bool {
        match self.input.try_recv() {
            Ok(byte) => {
                println!("Received: {}", byte);
                self.last_read_byte = byte;
                true
            }
            _ => false,
        }
    }
    fn clock_master(&mut self) -> bool {
        match self.clock_change.try_recv() {
            Ok(byte) => {
                println!("Received clock change, master: {}", byte);
                self.clock_master = byte;
            }
            _ => {}
        };
        self.clock_master
    }

    fn set_clock_master(&mut self, clock_master: bool) {
        self.clock_master = clock_master;
        self.output.send(FIFOPackage {
            t: false,
            value: (if clock_master { 1 } else { 0 }),
        });
    }
}