Created
April 12, 2016 14:40
-
-
Save makotoshimazu/4471f9c7b731abf9a6b217a824002aed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("----"); | |
pinMode(13, INPUT); | |
} | |
enum State { | |
OFF = 0, | |
ON, | |
OUT | |
}; | |
State current_state = OUT; | |
uint64 last_on = 0; | |
const uint64_t OFF_US = 50 * 1000; /* 50ms */ | |
const uint64_t TH_LEADER = 3000; /* 3ms */ | |
const uint64_t TH_ON = 1350; /* 0: 9ms, 1: 1.8ms */ | |
const int pin = 13; | |
uint64_t previous_us = 0; | |
int pos = 0; | |
long intervals[1024] = {}; | |
void loop() { | |
uint64_t current_us; | |
while((current_us = micros()) - previous_us < 100); | |
previous_us = current_us; | |
if (current_state == OUT) { | |
if (digitalRead(pin) == HIGH) { | |
current_state = ON; | |
last_on = current_us; | |
} | |
return; | |
} | |
if (current_us - last_on > OFF_US) { | |
current_state = OUT; | |
/* Parse */ | |
for (int i = 1; i < pos; i+=8) { | |
if (intervals[i] > TH_LEADER) | |
break; | |
byte b = 0; | |
for (int j = 0; j < 8; j++) { | |
if (intervals[i + j] > TH_ON) | |
b |= (1 << j); | |
} | |
if (b < 0x10) | |
Serial.print('0'); | |
Serial.print(b, HEX); | |
Serial.print(" "); | |
} | |
Serial.println(""); | |
pos = 0; | |
return; | |
} | |
if (current_state == ON) { | |
/* ON */ | |
if (digitalRead(pin) == LOW) | |
current_state = OFF; | |
} else { | |
/* OFF */ | |
if (digitalRead(pin) == HIGH) { | |
if (current_us - last_on > 400) { | |
intervals[pos++] = current_us - last_on; | |
last_on = current_us; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment