20140521-181001
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Snap.hpp
|
||||
*
|
||||
* Created: 10.05.2014 13:28:04
|
||||
* Author: netz
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SNAP_H_
|
||||
#define SNAP_H_
|
||||
|
||||
class Snap {
|
||||
public:
|
||||
static const uint8_t DEVICE = 30;
|
||||
|
||||
static const uint8_t ALL = 0;
|
||||
static const uint8_t GROUP = 240;
|
||||
static const uint8_t MASTER = 1;
|
||||
|
||||
static const uint8_t PING = 1;
|
||||
static const uint8_t SLEEP = 2;
|
||||
static const uint8_t ACTIVE = 3;
|
||||
static const uint8_t SETSLEEP = 4;
|
||||
static const uint8_t SETACTIVE = 5;
|
||||
static const uint8_t RUN = 6;
|
||||
static const uint8_t SETRUN = 7;
|
||||
static const uint8_t DEDECT = 8;
|
||||
static const uint8_t SETDEDECT = 9;
|
||||
|
||||
protected:
|
||||
virtual void hook_turn() = 0;
|
||||
virtual void hook_poll() = 0;
|
||||
virtual void funk_begin() = 0;
|
||||
virtual void funk_end() = 0;
|
||||
virtual uint8_t funk_has_data() = 0;
|
||||
virtual uint8_t funk_get_byte() = 0;
|
||||
virtual void funk_send_packet(uint8_t addr, uint8_t from, uint8_t data) = 0;
|
||||
|
||||
void wait() {
|
||||
_delay_ms(10);
|
||||
for(uint8_t i=0;i<this->DEVICE;i++) {
|
||||
_delay_ms(5);
|
||||
}
|
||||
}
|
||||
void get_packet(uint8_t data[], uint8_t len){
|
||||
this->funk_begin();
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
{
|
||||
while(this->funk_has_data()) {
|
||||
this->hook_poll();
|
||||
}
|
||||
data[i] = this->funk_get_byte();
|
||||
}
|
||||
this->funk_end();
|
||||
}
|
||||
|
||||
void send_packet(uint8_t data) {
|
||||
this->funk_send_packet(this->MASTER, this->DEVICE, data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SNAP_H_ */
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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_ */
|
||||
Reference in New Issue
Block a user