-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadcbtnph.cpp
More file actions
155 lines (133 loc) · 4.04 KB
/
adcbtnph.cpp
File metadata and controls
155 lines (133 loc) · 4.04 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// adcbtnph Class
//
// 1 ADC pin with 2 Buttons PullUp R 10k, R 1k
//
// used for 4 different events
//
// no button pressed ADC value ~1023
// event1, button 1 short press ADC value 0
// event2, button 2 short press ADC value ~91 1023 * 1k / (10k + 1k)
// event3, button 1 long press ADC value 0
// event4, button 2 long press ADC value ~91 1023 * 1k / (10k + 1k)
//
//
// typedef struct {
// oosmos_sEvent Event;
// unsigned Value;
// } sAdcButtonEvent;
//
// Event SubscriberListNotifyWithArgs event1: Value = 1, event2: Value = 2, event3: Value = 3, event4: Value = 4
//
// VCC
// |
// 10k
// / |
// +-----/ -----+---- ADC pin
// | button1 |
// | 1k
// | / |
// +-----/ -----+
// | button2
// |
// |
// GND
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2 of the License ("GPLv2").
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef adcbtnphMaxButtons
#define adcbtnphMaxButtons 1
#endif
#ifndef adcbtnphMaxPressedSubscribers
#define adcbtnphMaxPressedSubscribers 1
#endif
//===================================
#include "adcbtnph.h"
#include "oosmos.h"
#include <stdint.h>
#include <stddef.h>
struct adcbtnphTag
{
#if defined(ARDUINO)
adc *m_pAdc;
uint16_t m_HoldTimeMS;
uint16_t m_AdcVal;
oosmos_sObjectThread m_ObjectThread;
oosmos_sSubscriberList m_PressedEvent[adcbtnphMaxPressedSubscribers];
#else
#error adcbtnph.c: Unsupported platform.
#endif
};
#if defined(ARDUINO)
static void Thread(adcbtnph *pAdcButton, oosmos_sState *pState)
{
bool TimedOut = false;
oosmos_POINTER_GUARD(pAdcButton);
oosmos_POINTER_GUARD(pState);
oosmos_ThreadBegin();
for (;;)
{
oosmos_ThreadWaitCond(adcRead(pAdcButton->m_pAdc) < 1000);
pAdcButton->m_AdcVal = adcRead(pAdcButton->m_pAdc);
oosmos_ThreadWaitCond_TimeoutMS(adcRead(pAdcButton->m_pAdc) > 1000, pAdcButton->m_HoldTimeMS, &TimedOut);
// oosmos_DebugPrint("%d\n", pAdcButton->m_AdcVal);
sAdcButtonEvent sEvent;
sEvent.Event = oosmos_EMPTY_EVENT;
if (TimedOut)
{
if (pAdcButton->m_AdcVal < 50)
{
sEvent.Value = 2;
oosmos_SubscriberListNotifyWithArgs(pAdcButton->m_PressedEvent, sEvent);
}
if ((pAdcButton->m_AdcVal >= 70) && (pAdcButton->m_AdcVal <= 130))
{
sEvent.Value = 4;
oosmos_SubscriberListNotifyWithArgs(pAdcButton->m_PressedEvent, sEvent);
}
oosmos_ThreadWaitCond(adcRead(pAdcButton->m_pAdc) > 1000);
}
else
{
if (pAdcButton->m_AdcVal < 50)
{
sEvent.Value = 1;
oosmos_SubscriberListNotifyWithArgs(pAdcButton->m_PressedEvent, sEvent);
}
if ((pAdcButton->m_AdcVal >= 70) && (pAdcButton->m_AdcVal <= 130))
{
sEvent.Value = 3;
oosmos_SubscriberListNotifyWithArgs(pAdcButton->m_PressedEvent, sEvent);
}
}
}
oosmos_ThreadEnd();
}
extern void adcbtnphSubscribeButtonPress(adcbtnph *pAdcButton, oosmos_sQueue *pQueue, int PressedEventCode, void *pContext)
{
oosmos_POINTER_GUARD(pAdcButton);
oosmos_SubscriberListAdd(pAdcButton->m_PressedEvent, pQueue, PressedEventCode, pContext);
}
extern adcbtnph *adcbtnphNew(adc *pAdc, uint32_t HoldTimeMS)
{
oosmos_Allocate(pAdcButton, adcbtnph, adcbtnphMaxButtons, NULL);
pAdcButton->m_pAdc = pAdc;
pAdcButton->m_HoldTimeMS = HoldTimeMS;
oosmos_ObjectThreadInit(pAdcButton, m_ObjectThread, Thread, true);
oosmos_SubscriberListInit(pAdcButton->m_PressedEvent);
return pAdcButton;
}
#else
#error adcbtnph.c: Unsupported platform.
#endif