File size: 8,672 Bytes
a9536d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring
 https://github.com/PaulStoffregen/CapacitiveSensor
 http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
 http://playground.arduino.cc/Main/CapacitiveSensor
 Copyright (c) 2009 Paul Bagder  All right reserved.
 Version 05 by Paul Stoffregen - Support non-AVR board: Teensy 3.x, Arduino Due
 Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix
 vim: set ts=4:
 */

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#include "WConstants.h"
#endif

#include "CPlay_CapacitiveSensor.h"

/**************************************************************************/
/*! 
    @brief  Function that handles the creation and setup of instances
    @param sendPin send pin for the sensor
    @param receivePin the receiving pin for the sensor
*/
/**************************************************************************/
CPlay_CapacitiveSensor::CPlay_CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
{
	// initialize this instance's variables
	error = 1;
	loopTimingFactor = 300;		// determined empirically -  a hack

	CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
	CS_AutocaL_Millis = 20000;
	
	_sendPin = sendPin;
	_receivePin = receivePin;

	// Serial.print("timeOut =  ");
	// Serial.println(CS_Timeout_Millis);
	// get pin mapping and port for send Pin - from PinMode function in core

#ifdef NUM_DIGITAL_PINS
	if ((sendPin != 255) && (sendPin >= NUM_DIGITAL_PINS)) error = -3;
	if (receivePin >= NUM_DIGITAL_PINS) error = -2;
#endif
       	if (sendPin != -1) {      // sendpin to OUTPUT if we have one
#ifdef ARDUINO_TEEONARDU_FLORA	  // terrible hack!
	  DDRD |= _BV(5);
#else
	  pinMode(sendPin, OUTPUT);
#endif
	}

	pinMode(receivePin, INPUT);  // receivePin to INPUT

#ifdef ARDUINO_TEEONARDU_FLORA
	// terrible hack!
	PORTD &= ~_BV(5);
#else
	digitalWrite(sendPin, LOW);
#endif

       	if (sendPin != -1) {
	  send_outport =  portOutputRegister(digitalPinToPort(sendPin));
	  send_mask = digitalPinToBitMask(sendPin);
	} else {
	  send_outport = NULL;
	  send_mask = 0;
	}

	recv_outport =  portOutputRegister(digitalPinToPort(receivePin));
	recv_inport =  portInputRegister(digitalPinToPort(receivePin));
	recv_mask = digitalPinToBitMask(receivePin);
	recv_direction = portModeRegister(digitalPinToPort(receivePin));

	// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
	leastTotal = 0x0FFFFFFFL;   // input large value for autocalibrate begin
	lastCal = millis();         // set millis for start
}

// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries


/**************************************************************************/
/*! 
    @brief  get a capacitive sensor reading
    @param samples number of samples to take
    @return the sensor reading
*/
/**************************************************************************/
long CPlay_CapacitiveSensor::capacitiveSensor(uint8_t samples)
{
  total = 0;
  if (samples == 0) return 0;
  if (error < 0) {
    return -1;            // bad pin
  }

  pinMode(_receivePin, INPUT);

  for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
    if (SenseOneCycle() < 0)  return -2;   // variable over timeout
  }
	
  // only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline
  // this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal
  
  if ( (millis() - lastCal > CS_AutocaL_Millis) && 
       abs(total  - leastTotal) < (int)(.10 * (float)leastTotal) ) {
    
    // Serial.println();               // debugging
    // Serial.println("auto-calibrate");
    // Serial.println();
    // delay(2000); */
    
    leastTotal = 0x0FFFFFFFL;          // reset for "autocalibrate"
    lastCal = millis();
  }
  /*else{                                // debugging
    Serial.print("  total =  ");
    Serial.print(total);
    
    Serial.print("   leastTotal  =  ");
    Serial.println(leastTotal);
    
    Serial.print("total - leastTotal =  ");
    x = total - leastTotal ;
    Serial.print(x);
    Serial.print("     .1 * leastTotal = ");
    x = (int)(.1 * (float)leastTotal);
    Serial.println(x);
    } */
  
  // routine to subtract baseline (non-sensed capacitance) from sensor return
  if (total < leastTotal) leastTotal = total;                 // set floor value to subtract from sensed value
  return(total - leastTotal);
}

/**************************************************************************/
/*! 
    @brief  get a raw sensor reading
    @param samples the number of samples to take
    @return -1 for error, -2 for timeout, other values are a raw sensor reading
*/
/**************************************************************************/
long CPlay_CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
{
  total = 0;
  if (samples == 0) return 0;
  if (error < 0) return -1;                  // bad pin - this appears not to work
  
  for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
    if (SenseOneCycle() < 0)  return -2;   // variable over timeout
  }
  
  return total;
}

/**************************************************************************/
/*! 
    @brief  reset the auto calibration
*/
/**************************************************************************/
void CPlay_CapacitiveSensor::reset_CS_AutoCal(void){
  leastTotal = 0x0FFFFFFFL;
}

/**************************************************************************/
/*! 
    @brief set the auto-calibration time
    @param autoCal_millis the desired calibration time in milliseconds
*/
/**************************************************************************/
void CPlay_CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){
  CS_AutocaL_Millis = autoCal_millis;
}

/**************************************************************************/
/*! 
    @brief  set the sensor timeout
    @param timeout_millis the number of milliseconds to set the timeout to
*/
/**************************************************************************/
void CPlay_CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
  CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000;  // floats to deal with large numbers
}

// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library

/**************************************************************************/
/*! 
    @brief  sense a single sicle
    @return the reading
*/
/**************************************************************************/
int CPlay_CapacitiveSensor::SenseOneCycle(void)
{
  if (send_mask != 0) {
    noInterrupts();
    *send_outport &= ~send_mask;           // sendPin Register low
    *recv_direction &= ~recv_mask;	// receivePin to input (pullups are off)
    *recv_direction |= recv_mask;       // receivePin to OUTPUT
    *recv_outport &= ~recv_mask;    	// pin is now LOW AND OUTPUT
    delayMicroseconds(10);
    *recv_direction &= ~recv_mask;	// receivePin to input (pullups are off)
    *send_outport |= send_mask;           // sendPin Register high
    interrupts();
    
    while ( !(*recv_inport & recv_mask) && (total < CS_Timeout_Millis) ) {  // while receive pin is LOW AND total is positive value
      total++;
    }
    //Serial.print("SenseOneCycle(1): "); Serial.println(total);
    
    if (total > CS_Timeout_Millis) {
      return -2;         //  total variable over timeout
    }
  }

  // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
  noInterrupts();
  *send_outport &= ~send_mask;           // sendPin Register low
  *recv_direction |= recv_mask;       // receivePin to OUTPUT
  *recv_outport |= recv_mask;    	// pin is now HIGH AND OUTPUT
  delayMicroseconds(10);
  *recv_direction &= ~recv_mask;	// receivePin to input (pullups are off)
  *recv_outport &= ~recv_mask;
  if (send_mask != 0) {
    *send_outport &= ~send_mask;           // sendPin Register high
  }
  interrupts();
  
  while ((*recv_inport & recv_mask) && (total < CS_Timeout_Millis) ) {
    total++; // while receive pin is HIGH  AND total is less than timeout
  }
  //Serial.print("SenseOneCycle(2): "); Serial.println(total);

  if (total >= CS_Timeout_Millis) {
    return -2;     // total variable over timeout
  } else {
    return 1;
  }
}