133 lines
2.6 KiB
C++
133 lines
2.6 KiB
C++
|
/*
|
||
|
* funk.hpp
|
||
|
*
|
||
|
* Created: 10.05.2014 10:00:08
|
||
|
* Author: netz
|
||
|
*/
|
||
|
|
||
|
#ifndef FUNK_H_
|
||
|
#define FUNK_H_
|
||
|
|
||
|
#include "Snap.hpp"
|
||
|
|
||
|
template <typename Rfm12, typename Input_Beweg, typename Input_Start, typename Servo, typename Led>
|
||
|
class Funk : public Snap {
|
||
|
public:
|
||
|
volatile uint8_t status;
|
||
|
Funk() {
|
||
|
this->init();
|
||
|
}
|
||
|
void timer() {
|
||
|
if(this->status == this->SLEEP) {
|
||
|
led.color(led.GREEN);
|
||
|
} else if(this->status == this->ACTIVE) {
|
||
|
led.color(led.RED);
|
||
|
} else if(this->status == this->RUN) {
|
||
|
led.color(led.YELLOW);
|
||
|
}
|
||
|
servo.timer();
|
||
|
}
|
||
|
void worker() {
|
||
|
uint8_t data[3];
|
||
|
this->get_packet(data, 3);
|
||
|
if(data[0] == ALL || data[0] == DEVICE || data[0] == GROUP) {
|
||
|
if(data[1] == this->MASTER) {
|
||
|
switch(data[2]) {
|
||
|
case this->PING: {
|
||
|
break;
|
||
|
}
|
||
|
case this->SETSLEEP: {
|
||
|
this->status = this->SLEEP;
|
||
|
break;
|
||
|
}
|
||
|
case this->SETACTIVE: {
|
||
|
this->status = this->ACTIVE;
|
||
|
break;
|
||
|
}
|
||
|
case this->SETRUN: {
|
||
|
this->hook_turn();
|
||
|
this->send_packet(this->RUN);
|
||
|
return;
|
||
|
}
|
||
|
default: {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
this->wait();
|
||
|
this->send_packet(this->status);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private:
|
||
|
Input_Start st;
|
||
|
Input_Beweg mc;
|
||
|
Servo servo;
|
||
|
Led led;
|
||
|
Rfm12 rfm12;
|
||
|
void init()
|
||
|
{
|
||
|
led.color(led.RED);
|
||
|
this->status = this->SLEEP;
|
||
|
}
|
||
|
void hook_turn()
|
||
|
{
|
||
|
if(this->status == this->RUN){
|
||
|
return;
|
||
|
}
|
||
|
this->status = this->RUN;
|
||
|
|
||
|
servo.turn(servo.LEFT);
|
||
|
servo.on();
|
||
|
this->wait();
|
||
|
_delay_ms(400);
|
||
|
servo.turn(servo.MIDDLE);
|
||
|
_delay_ms(300);
|
||
|
servo.turn(servo.RIGHT);
|
||
|
_delay_ms(700);
|
||
|
servo.off();
|
||
|
}
|
||
|
|
||
|
void hook_poll()
|
||
|
{
|
||
|
if(this->status == this->ACTIVE) {
|
||
|
if(mc.is_pressed()) {
|
||
|
this->funk_end();
|
||
|
this->hook_turn();
|
||
|
this->send_packet(this->RUN);
|
||
|
this->funk_begin();
|
||
|
}
|
||
|
}
|
||
|
if(this->status == this->SLEEP) {
|
||
|
if(mc.is_pressed()) {
|
||
|
this->funk_end();
|
||
|
this->send_packet(this->DEDECT);
|
||
|
this->funk_begin();
|
||
|
}
|
||
|
}
|
||
|
if(st.is_pressed()) {
|
||
|
if(this->status == this->SLEEP) {
|
||
|
this->status = this->RUN;
|
||
|
_delay_ms(15000);
|
||
|
this->status = this->ACTIVE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
void funk_begin() {
|
||
|
rfm12.beginasyncrx();
|
||
|
}
|
||
|
void funk_end() {
|
||
|
rfm12.endasyncrx();
|
||
|
}
|
||
|
uint8_t funk_has_data() {
|
||
|
return rfm12.hasdata();
|
||
|
}
|
||
|
uint8_t funk_get_byte() {
|
||
|
return rfm12.rxbyte();
|
||
|
}
|
||
|
void funk_send_packet(uint8_t addr, uint8_t from, uint8_t data) {
|
||
|
rfm12.txpacket(addr, from, data);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
#endif /* FUNK_H_ */
|