Tuer-Schild/Door/Door.cpp
2014-11-19 22:57:35 +00:00

193 lines
4.0 KiB
C++
Raw Blame History

/*
* Door.cpp
*
* Created: 04.09.2014 22:06:23
* Author: netz
*/
#include "peripheral.h"
#include <util/delay.h>
uartclass Serial;
stripeclass stripe;
ledclass led;
compclass comp;
//intclass ir;
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 30000
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20
uint16_t volatile pulses[100][2]; // pair is high and low pulse
uint8_t volatile currentpulse = 0;
void printpulses(void) {
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.printDec(pulses[i][0] * RESOLUTION);
Serial.print(" ");
Serial.printDec(pulses[i][1] * RESOLUTION);
Serial.println(" ");
}
// print it in a 'array' format
Serial.println("int IRsignal[] = {");
Serial.println("// OFF, ON (in 10's of microseconds)");
for (uint8_t i = 0; i < currentpulse-1; i++) {
Serial.print("\t"); // tab
Serial.printDec(pulses[i][0] * RESOLUTION / 10);
Serial.print(", ");
Serial.printDec(pulses[i][1] * RESOLUTION / 10);
Serial.println(",");
}
Serial.print("\t"); // tab
Serial.printDec(pulses[currentpulse-1][0] * RESOLUTION / 10);
Serial.print(", ");
Serial.printDec(pulses[currentpulse-1][1] * RESOLUTION / 10);
Serial.println("};");
}
void loop()
{
while(1) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
while (!(PIND & (1<<PIND2))) {
// pin is still LOW
lowpulse++;
_delay_us(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
return;
}
}
pulses[currentpulse][1] = lowpulse;
while (PIND & (1<<PIND2)) {
highpulse++;
_delay_us(RESOLUTION);
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
return;
}
}// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;
// we read one high-low pulse successfully, continue!
currentpulse++;
}
}
uint8_t read8bit() {
uint8_t r = 0;
for (uint8_t i=0;i<8;i++)
{
while (PIND & (1<<PIND2)) {}
led.red(1);
_delay_us(600);
if(!(PIND & (1<<PIND2))) {
r |= (1<<i);
}
while (!(PIND & (1<<PIND2))) {}
led.red(0);
_delay_us(10);
}
return r;
}
void recieve()
{
if(PIND & (1<<PIND2)) { //Is High, Abbort
return;
}
_delay_ms(4); //Wait 4ms
if(PIND & (1<<PIND2)) { //Is High, Abbort
return;
}
while (!(PIND & (1<<PIND2))) {} //Wait for the rest of the init.
uint8_t a = read8bit();
read8bit();
uint8_t b = read8bit();
Serial.printDec(0x0000|b); Serial.println(" 1Byte");
if(a != 0) { //Is first Byte not 0 Abbort;
return;
}
if(b != 239) { //Is second byte not 239 Abbort;
return;
}
uint8_t hb = read8bit(); //Read First Byte
uint8_t lb = read8bit(); //Read Second Byte
Serial.println("Read Data:");
Serial.printDec(hb); Serial.print(" HByte, "); Serial.printDec(lb); Serial.println(" LByte");
return;
}
int main(void)
{
sei();
/*uint8_t status = 0;
uint8_t gr = 0xFF;
uint8_t re = 0x00;*/
for(int i=0;i<50;i++) {
Serial.println("Geladen!");
}
led.green(1);
while(1)
{
while(PIND & (1<<PIND2)) {};
recieve();
//currentpulse=0;
//loop();
Serial.println(".");
_delay_ms(200);
led.red(0);
//_delay_ms(50);
}
}
/*ISR(INT0_vect) {
led.red(1);
}*/
/*for(int i=0; i<0xFE; i++) {
if() {
led.red(1);
} else {
led.red(0);
}
//led.red(0);
_delay_us(100);
}
led.red(0);
_delay_ms(500);*/
/*stripe.color(re, gr, 0);
if(comp.is_active()) {
if(status == 0) {
status = 1;
Serial.println("T<>r Zu...");
}
if(gr != 0) {
gr--;
re++;
_delay_ms(6);
}
} else {
if(status == 1) {
status = 0;
Serial.println("T<>r Offen...");
}
if(gr != 0xFF) {
gr++;
re--;
_delay_ms(2);
}
}*/