forked from TinkerKit/TinkerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinkerKit.h
More file actions
executable file
·330 lines (264 loc) · 6.96 KB
/
TinkerKit.h
File metadata and controls
executable file
·330 lines (264 loc) · 6.96 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* TinkerKit Library v0.1
*
*
*
* created on Dec 2011
* by Federico Vanzati
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "Arduino.h"
#ifndef TinkerKit_h
#define TinkerKit_h
// Minimum Analog In/Out that each platform have
#define I0 A0
#define I1 A1
#define I2 A2
#define I3 A3
#define I4 A4
#define I5 A5
#define O0 11
#define O1 10
#define O2 9
#define O3 6
#define O4 5
#define O5 3
// Mega have more I/O
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
#define I6 A6
#define I7 A7
#define I8 A8
#define I9 A9
#define D13 13
#define D12 12
#define D8 8
#define D7 7
#define D4 4
#define D2 2
#endif
#define TK_MAX 1023
#define TK_X1 0 // identifies the 1x TKGyro model
#define TK_X4 1 // identifies the 4x TKGyro model
#define NORTH 1 // north pole: used in TKHallSensor
#define SOUTH 0 // south pole: used in TKHallSensor
/*
-----------------------------------------------------------------------------
Generic Classes
-----------------------------------------------------------------------------
*/
class TKDigital
{
public:
TKDigital(uint8_t _pin);
boolean read();
protected:
uint8_t pin;
};
class TKAnalog
{
public:
TKAnalog(uint8_t _pin);
int read();
boolean increasing();
boolean decreasing();
protected:
uint8_t pin;
int _oldVal;
boolean _increasing;
boolean _decreasing;
};
class TKAnalog2
{
public:
TKAnalog2(uint8_t _pinX, uint8_t _pinY);
TKAnalog2(uint8_t _pinX, uint8_t _pinY, uint8_t _pinZ);
int readX();
int readY();
int readZ();
protected:
uint8_t pinX, pinY, pinZ;
};
class TKOutput
{
public:
TKOutput (uint8_t _pin);
void write(int value);
inline int state() { return _state; }
void on() {
write(1023);
_state = HIGH;
}
void off() {
write(0);
_state = LOW;
}
void blink(int delay);
void blink(int delay1, int delay2);
protected:
uint8_t pin;
int _state;
};
/*
-----------------------------------------------------------------------------
Digital Inputs
-----------------------------------------------------------------------------
*/
/* Button */
class TKButton: public TKDigital
{
public:
TKButton(uint8_t _pin);
boolean readSwitch();
boolean pressed();
boolean held();
boolean released();
protected:
boolean _toggleState, _oldState;
boolean _pressedState, _releasedState;
boolean _heldState;
int _heldTime;
int _millisMark;
void update();
};
/* Tilt Sensor */
class TKTiltSensor: public TKDigital
{
public:
TKTiltSensor(uint8_t pin);
};
/* Touch Sensor */
class TKTouchSensor : public TKButton
{
public:
TKTouchSensor(uint8_t _pin);
};
/*
-----------------------------------------------------------------------------
Analog Inputs
-----------------------------------------------------------------------------
*/
/* Potentiometer */
class TKPotentiometer: public TKAnalog
{
public:
TKPotentiometer(uint8_t pin);
int read();
int readStep(int steps);
protected:
int _minVal, _maxVal;
int _mappedVal;
int _steps;
};
/* Light Sensor */
class TKLightSensor : public TKAnalog
{
public:
TKLightSensor(uint8_t _pin);
};
/* Temperature Sensor */
class TKThermistor : public TKAnalog
{
public:
TKThermistor(uint8_t _pin);
float readCelsius();
float readFahrenheit();
protected:
const static float ADCres = 1023.0;
const static int Beta = 3950; // Beta parameter
const static float Kelvin = 273.15; // 0°C = 273.15 K
const static int Rb = 10000; // 10 kOhm
const static float Ginf = 120.6685; // Ginf = 1/Rinf
// Rinf = R0*e^(-Beta/T0) = 4700*e^(-3950/298.15)
};
/* Hall Sensor */
class TKHallSensor : public TKAnalog
{
public:
TKHallSensor(uint8_t _pin);
boolean polarity();
protected:
const static uint16_t _zeroValue = 512;
};
/* Joystick */
class TKJoystick : public TKAnalog2
{
public:
TKJoystick(uint8_t _pinX, uint8_t _pinY);
int readX();
int readY();
protected:
int _minVal, _maxVal;
int _mappedVal;
};
/* Gyro Sensor */
class TKGyro : public TKAnalog2
{
public:
TKGyro(uint8_t _pinX, uint8_t _pinY, boolean model);
void calibrate();
long readXAxisRate();
long readYAxisRate();
protected:
boolean model;
//const static int _ADCresolution = 4880; // [mV/count] multiplierd by 1000 to avoid float numbers
// minimum sensitivity for the 1x module value (from datasheet is 0.167 mV/deg/s but the TinkerKit module has the outputs amplified 2x)
//const static int _sensitivity = 334; // Sensitivity is expressed in mV/degree/seconds, multiplierd by 1000 to avoid float numbers.
// This value represent the sensitivity of the 1x module. The sensitivity of the 4x module is 4x of this one
long _sensitivityInCount; // we obtain the sensitivity expressed in ADC counts
// [counts/dps]
int _yZeroVoltage;
int _xZeroVoltage;
};
/* Accelerometer */
class TKAccelerometer : public TKAnalog2
{
public:
TKAccelerometer(uint8_t _pinX, uint8_t _pinY);
TKAccelerometer(uint8_t _pinX, uint8_t _pinY, uint8_t _pinZ);
inline float readXinG() { return (float)(readX() - _zeroOffset)/96; }
inline float readYinG() { return (float)(readY() - _zeroOffset)/96; }
inline float readZinG() { return (float)(readZ() - _zeroOffset)/96; }
int inclination();
protected:
const static float _gain = 1.414;
const static int _zeroOffset = 478;
};
/*
-----------------------------------------------------------------------------
Outputs
-----------------------------------------------------------------------------
*/
/* LED */
class TKLed : public TKOutput
{
public:
TKLed(uint8_t _pin);
inline void brightness(int value) { write(value); }
};
/* MosFet */
class TKMosFet : public TKOutput
{
public:
TKMosFet(uint8_t _pin);
};
/* Relay */
class TKRelay : public TKOutput
{
public:
TKRelay(uint8_t _pin);
};
#endif