-
Notifications
You must be signed in to change notification settings - Fork 1
/
EAButton.cpp
69 lines (61 loc) · 1.76 KB
/
EAButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "EAButton.h"
#ifdef DEBUG_EABUTTON
#define DBG_PRINTLN(a) Serial.println(a)
#define DBG_PRINT(a) Serial.print(a)
#else
#define DBG_PRINTLN(a)
#define DBG_PRINT(a)
#endif
//================================== class EAButton ======================
oid_t EAButton::init(const port_t port)
{
oid_t result = EDevice::init(port);
// currentLevel=0;
debounceTimer.init( DEBOUNCEDELAY );
// Serial.println("EAInput");
return result;
};
uint8_t EAButton::addLevel(const int newLevel)
{
if (this->levelCount<ABUTTONMAXLEVELS) {
this->levels[++this->levelCount] = newLevel;
}
return levelCount;
};
int EAButton::handleEvent(Event& tmpEvent)
{
return EObject::handleEvent(tmpEvent);
};
void EAButton::idle()
{
uint8_t lastLevel = this->currentLevel;
if ( this->isEnabled) {
getData();
if ( lastLevel != this->currentLevel ) {
//Level changed, let's generate event!
eventStack.pushEvent( evLevelChanged, this->getID(), 0, this->currentData );
DBG_PRINT( "EAButton::idle: data=" );
DBG_PRINT( this->currentData );
DBG_PRINT( " level=" );
DBG_PRINTLN( this->currentLevel );
}
}
};
void EAButton::getName( char* result ) const
{
sprintf( result, "EAButton: ID=%d port=%d ", getID(), this->port );
};
int EAButton::getData()
{
currentData=analogRead(this->port);
//выбрать значение уровня, при котором текущее значение не превышает
//порогового для данного уровня
for ( currentLevel = 0; currentLevel < this->levelCount; currentLevel++ ) {
if ( this->currentData < this->levels[currentLevel] ) break;
}
return currentLevel;
};
#ifdef DEBUG_EABUTTON
#undef DBG_PRINTLN(a)
#undef DBG_PRINT(a)
#endif