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