changed everything

This commit is contained in:
Ellie 2019-06-15 01:57:28 +00:00
parent f5a1724916
commit ff99c6e3ce
1 changed files with 117 additions and 44 deletions

View File

@ -4,8 +4,67 @@ use std::io::prelude::*;
use std::fs::File;
use std::io;
static FILTER_STRENGTH:u8 = 10;
static RESET_THRESH:usize = 100; // this many samples with no bits = reset byte position
static FILTER_STRENGTH:u8 = 10; // how severely to low-pass the incoming data
static RESET_THRESH:usize = 20; // this many samples with no bits = reset byte position
static OVERSAMPLE_FACTOR:usize = 10; // how many samples long are each bit in the decoder input?
static INVERT:bool = false; // whether or not the source inverts the samples
struct StreamCoder {
encoder:[u16;256],
decoder:[u8;4096],
}
fn streamcoder_gen() -> StreamCoder {
let mut output_a:[u16;256] = [0x0000;256];
let mut output_b:[u8;4096] = [0x20;4096];
let mut a:usize = 0;
let mut b:usize = 0;
while a < 256 {
if b.count_ones() == b.count_zeros() {
output_a[a] = b as u16;
output_b[b] = a as u8;
a += 1;
}
b += 1;
}
return StreamCoder {
encoder:output_a,
decoder:output_b,
};
}
fn code_bits(n:u16) -> [bool;12] {
let mut output:[bool;12] = [false;12];
for i in 0..12 {
output[i] = (n >> i) & 0x01 == 0x01;
}
return output;
}
fn bits_code(b:&[bool]) -> u16 {
let mut output:u16 = 0;
if b.len() < 12 {
return 0;
}
for i in 0..12 {
if b[i] {
output += (0x01 << i) as u16;
}
}
return output;
}
impl StreamCoder {
fn encode(&self,byte:u8) -> [bool;12] {
return code_bits(self.encoder[byte as usize]);
}
fn decode(&self,states:&[bool]) -> u8 {
return self.decoder[bits_code(states) as usize];
}
}
fn main() {
let argv = args().collect::<Vec<String>>();
@ -34,6 +93,7 @@ fn main() {
Ok(f) => Box::new(f),
},
};
let coder = streamcoder_gen();
if mode == "e" {
match iooutput.write_all(&[0x80;1000]) {
Err(why) => {
@ -54,26 +114,29 @@ fn main() {
},
Ok(_) => (),
};
for i in 0..8 {
let payload:[u8;8] = match ibuffer[0] & (0x80 >> i) != 0x00 {
true => [0x80,0x80,0x80,0xFF,0x80,0x00,0x80,0x80],
false => [0x80,0x80,0xFF,0x80,0x00,0x80,0x80,0x80],
};
match iooutput.write_all(&payload) {
Err(why) => {
eprintln!("write failed: {}",why);
exit(1);
},
Ok(_) => (),
let mut payload:[u8;16] = [0x80;16];
let bits = coder.encode(ibuffer[0]);
for i in 0..12 {
payload[i] = match bits[i] {
true => 0xFF,
false => 0x00,
};
}
match iooutput.write_all(&payload) {
Err(why) => {
eprintln!("write failed: {}",why);
exit(1);
},
Ok(_) => (),
};
}
} else if mode == "d" {
let mut ibuffer:[u8;2] = [0x00,0x00];
let mut filterstates:[u16;2] = [0x0000,0x0000];
let mut logicstates:[bool;2] = [false,false];
let mut outbits:Vec<bool> = Vec::with_capacity(8);
let mut idlecycles:usize = 0;
let mut ibuffer:[u8;1] = [0x00];
let mut filterstate:u16 = 0x0000;
let mut logicstate:i8 = 0;
let mut statecount:Option<(i8,usize)> = None;
let mut count:usize = 1;
let mut outbits:Vec<bool> = Vec::with_capacity(12);
loop {
match ioinput.read_exact(&mut ibuffer) {
Err(why) => match why.kind() {
@ -85,29 +148,45 @@ fn main() {
},
Ok(_) => (),
};
filterstates[0] = filterstates[0]*(FILTER_STRENGTH as u16)/0x00FF + ((0x00FF-FILTER_STRENGTH as u16)*ibuffer[0] as u16)/0x00FF;
filterstates[1] = filterstates[1]*(FILTER_STRENGTH as u16)/0x00FF + ((0x00FF-FILTER_STRENGTH as u16)*ibuffer[1] as u16)/0x00FF;
if filterstates[0] > 180 && !logicstates[0] {
logicstates[0] = true;
outbits.push(false);
idlecycles = 0;
} else if filterstates[0] < 170 && logicstates[0] {
logicstates[0] = false
filterstate = filterstate*(FILTER_STRENGTH as u16)/0x00FF + ((0x00FF-FILTER_STRENGTH as u16)*ibuffer[0] as u16)/0x00FF;
if filterstate > 170 {
if logicstate == 1 {
count += 1;
} else {
statecount = Some((logicstate,count));
count = 1;
logicstate = 1;
}
} else if filterstate < 85 {
if logicstate == -1 {
count += 1;
} else {
statecount = Some((logicstate,count));
count = 1;
logicstate = -1;
}
} else {
if logicstate != 0 {
count = 1;
logicstate = 0;
}
statecount = Some((logicstate,count));
}
if filterstates[1] > 180 && !logicstates[1] {
logicstates[1] = true;
outbits.push(true);
idlecycles = 0;
} else if filterstates[1] < 170 && logicstates[1] {
logicstates[1] = false
}
if outbits.len() >= 8 {
let mut outbyte:u8 = 0x00;
for i in 0..8 {
if outbits[i] {
outbyte |= 0x80 >> i;
if let Some((state,n)) = statecount {
if state == 0 {
if n >= RESET_THRESH {
outbits.clear();
}
} else {
for _ in 0..(n+OVERSAMPLE_FACTOR/2)/OVERSAMPLE_FACTOR {
outbits.push((state > 0) ^ INVERT);
}
}
statecount = None;
}
if outbits.len() >= 12 {
let outbyte:u8 = coder.decode(&outbits);
outbits.clear();
match iooutput.write_all(&[outbyte]) {
Err(why) => {
eprintln!("write failed: {}",why);
@ -115,12 +194,6 @@ fn main() {
},
Ok(_) => (),
};
outbits.clear();
}
if idlecycles < RESET_THRESH {
idlecycles += 1;
} else {
outbits.clear();
}
}
} else {