robert@464
|
1 /*
|
robert@464
|
2 * DboxSensors.cpp
|
robert@464
|
3 *
|
robert@464
|
4 * Created on: May 19, 2014
|
robert@464
|
5 * Author: Victor Zappi
|
robert@464
|
6 */
|
robert@464
|
7
|
robert@464
|
8
|
robert@464
|
9 #include "DboxSensors.h"
|
robert@464
|
10 #include "config.h"
|
robert@464
|
11
|
robert@464
|
12 using namespace std;
|
robert@464
|
13
|
robert@464
|
14
|
robert@464
|
15
|
robert@464
|
16 int DboxSensors::initSensors(int tk0_bus, int tk0_address, int tk1_bus, int tk1_address, int tk_file, int fsr_pin, int fsrmax, int sensorTypeToUse, int gpio_0, int gpio_1)
|
robert@464
|
17 {
|
robert@464
|
18 sensorType = sensorTypeToUse;
|
robert@464
|
19 // init first touch key on i2c bus
|
robert@464
|
20 if(tk0_address >= 0) {
|
robert@464
|
21 if(TK0.initI2C_RW(tk0_bus, tk0_address, tk_file)>0)
|
robert@464
|
22 return 1;
|
robert@464
|
23 if(TK0.initTouchKey(sensorType)>0)
|
robert@464
|
24 return 2;
|
robert@464
|
25 }
|
robert@464
|
26
|
robert@464
|
27 // init second touch key on i2c bus
|
robert@464
|
28 if(tk1_address >= 0) {
|
robert@464
|
29 if(TK1.initI2C_RW(tk1_bus, tk1_address, tk_file)>0)
|
robert@464
|
30 return 1;
|
robert@464
|
31 if(TK1.initTouchKey(sensorType)>0)
|
robert@464
|
32 return 2;
|
robert@464
|
33 }
|
robert@464
|
34
|
robert@464
|
35 // init fsr on analog input pin
|
robert@464
|
36 fsr_pinNum = fsr_pin;
|
robert@464
|
37 fsr_max = fsrmax;
|
robert@464
|
38
|
robert@464
|
39 if(FSR.initAnalogInputs()>0)
|
robert@464
|
40 return 3;
|
robert@464
|
41
|
robert@464
|
42 gpio[0] = gpio_0;
|
robert@464
|
43 if(gpio[0]!=-1)
|
robert@464
|
44 {
|
robert@464
|
45 fdDi[0] = gpio_export(gpio[0]);
|
robert@464
|
46 if(fdDi[0] == -1)
|
robert@464
|
47 return 4;
|
robert@464
|
48 }
|
robert@464
|
49 digitalIn[0] = 1;
|
robert@464
|
50
|
robert@464
|
51 return 0;
|
robert@464
|
52 }
|
robert@464
|
53
|
robert@464
|
54
|
robert@464
|
55 int DboxSensors::readSensors()
|
robert@464
|
56 {
|
robert@464
|
57 // write data into first touch key
|
robert@464
|
58 if(TK0.ready()) {
|
robert@464
|
59 if(TK0.readI2C()>0)
|
robert@464
|
60 return 1;
|
robert@464
|
61
|
robert@464
|
62 // retrieve data from first touch key
|
robert@464
|
63 tk0_touchCnt = TK0.getTouchCount();
|
robert@464
|
64 }
|
robert@464
|
65 else
|
robert@464
|
66 tk0_touchCnt = 0;
|
robert@464
|
67
|
robert@464
|
68 // write data into second touch key
|
robert@464
|
69 if(TK1.ready()) {
|
robert@464
|
70 if(TK1.readI2C()>0)
|
robert@464
|
71 return 1;
|
robert@464
|
72 // retrieve data from second touch key
|
robert@464
|
73 tk1_touchCnt = TK1.getTouchCount();
|
robert@464
|
74 }
|
robert@464
|
75 else
|
robert@464
|
76 tk1_touchCnt = 0;
|
robert@464
|
77
|
robert@464
|
78
|
robert@464
|
79 int max = 3;
|
robert@464
|
80 if(sensorType != kSensorTypeTouchKey)
|
robert@464
|
81 max = 5;
|
robert@464
|
82 // if touches detected on main touch key
|
robert@464
|
83 if(tk0_touchCnt == 0 && tk1_touchCnt == 0)
|
robert@464
|
84 resetSensorsData();
|
robert@464
|
85 else
|
robert@464
|
86 {
|
robert@464
|
87 for(int i=0; i<max; i++)
|
robert@464
|
88 {
|
robert@464
|
89 tk0_touchPosX[i] = TK0.getSliderPosition()[i];
|
robert@464
|
90 tk0_touchSize[i] = TK0.getSlidersize()[i];
|
robert@464
|
91
|
robert@464
|
92 tk1_touchPosX[i] = TK1.getSliderPosition()[i];
|
robert@464
|
93 tk1_touchSize[i] = TK1.getSlidersize()[i];
|
robert@464
|
94 }
|
robert@464
|
95 tk0_touchPosY = TK0.getSliderPositionH();
|
robert@464
|
96 tk1_touchPosY = TK1.getSliderPositionH();
|
robert@464
|
97 fsr_read = (double)FSR.read(fsr_pinNum);
|
robert@464
|
98 }
|
robert@464
|
99
|
robert@464
|
100 if(gpio[0]!=-1)
|
robert@464
|
101 {
|
robert@464
|
102 if(gpio_read(fdDi[0], &digitalIn[0])==-1)
|
robert@464
|
103 return 1;
|
robert@464
|
104 }
|
robert@464
|
105
|
robert@464
|
106 return 0;
|
robert@464
|
107 }
|
robert@464
|
108
|
robert@464
|
109
|
robert@464
|
110
|
robert@464
|
111 DboxSensors::DboxSensors()
|
robert@464
|
112 {
|
robert@464
|
113 resetSensorsData();
|
robert@464
|
114 }
|
robert@464
|
115
|
robert@464
|
116
|
robert@464
|
117
|
robert@464
|
118 DboxSensors::~DboxSensors()
|
robert@464
|
119 {
|
robert@464
|
120 if(gpio[0]!=-1)
|
robert@464
|
121 gpio_dismiss(fdDi[0], gpio[0]);
|
robert@464
|
122 }
|
robert@464
|
123
|
robert@464
|
124
|
robert@464
|
125
|
robert@464
|
126 //--------------------------------------------------------------------------------------------------------
|
robert@464
|
127 // private methods
|
robert@464
|
128 //--------------------------------------------------------------------------------------------------------
|
robert@464
|
129
|
robert@464
|
130 // idle values
|
robert@464
|
131 void DboxSensors::resetSensorsData()
|
robert@464
|
132 {
|
robert@464
|
133 int max = 3;
|
robert@464
|
134 if(sensorType != kSensorTypeTouchKey)
|
robert@464
|
135 max = 5;
|
robert@464
|
136
|
robert@464
|
137 for(int i=0; i<max; i++)
|
robert@464
|
138 {
|
robert@464
|
139 tk0_touchPosX[i] = -1;
|
robert@464
|
140 tk0_touchPosY = -1;
|
robert@464
|
141 tk0_touchSize[i] = 0;
|
robert@464
|
142
|
robert@464
|
143 tk1_touchPosX[i] = -1;
|
robert@464
|
144 tk1_touchPosY = -1;
|
robert@464
|
145 tk1_touchSize[i] = 0;
|
robert@464
|
146
|
robert@464
|
147 fsr_read = 0;
|
robert@464
|
148 }
|
robert@464
|
149
|
robert@464
|
150 return;
|
robert@464
|
151 }
|
robert@464
|
152
|
robert@464
|
153
|
robert@464
|
154
|
robert@464
|
155
|
robert@464
|
156
|
robert@464
|
157
|