//! Blinks an LED
//!
//! This assumes that a LED is connected to GPIO4.
//! Depending on your target and the board you are using you should change the pin.
//! If your board doesn't have on-board LEDs don't forget to add an appropriate resistor.
//!
use std::thread;
use std::time::Duration;
use embedded_hal::digital::blocking::OutputPin;
use esp_idf_hal::peripherals::Peripherals;
pub fn calculate(data: &[u8]) -> u8 {
const CRC8_POLYNOMIAL: u8 = 0x31;
let mut crc: u8 = 0xff;
for byte in data {
crc ^= byte;
println!("crc tmp: {:02x}", crc);
for _ in 0..8 {
if (crc & 0x80) > 0 {
println!("top bit is high");
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
} else {
println!("top bit is low");
crc <<= 1;
}
}
}
crc
}
pub fn calculate2(data: &[u8]) -> u8 {
const CRC8_POLYNOMIAL: u8 = 0x31;
let mut crc: u8 = 0xff;
for byte in data {
crc ^= byte;
for _ in 0..8 {
if (crc & 0x80) > 0 {
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
} else {
crc <<= 1;
}
}
}
crc
}
fn main() -> anyhow::Result<()> {
let buff: [u8; 2] = [9, 94];
let buff2: [u8; 2] = [9, 94];
let crc = calculate(&buff);
let crc2 = calculate2(&buff2);
println!("CRC {}", crc);
println!("CRC2 {}", crc2);
if crc != crc2 {
println!("HOW IS THIS POSSIBLE?");
}
let peripherals = Peripherals::take().unwrap();
let mut led = peripherals.pins.gpio4.into_output()?;
loop {
led.set_high()?;
// we are using thread::sleep here to make sure the watchdog isn't triggered
thread::sleep(Duration::from_millis(1000));
led.set_low()?;
thread::sleep(Duration::from_millis(1000));
}
}