Commit 786b9681 authored by Sam Guyer's avatar Sam Guyer
Browse files

Initial commit

Finally storing some of the library code on github
parent 88649b47
// == Accelerometer configuration =====
#define X_PIN A0
#define Y_PIN A1
#define Z_PIN A2
// X: 404-612, Y: 409-622, Z: 418-622
#define ACC_X_MIN 404
#define ACC_X_MAX 612
#define ACC_Y_MIN 409
#define ACC_Y_MAX 622
#define ACC_Z_MIN 418
#define ACC_Z_MAX 622
class Accelerometer
{
public:
enum Axis { X_AXIS, Y_AXIS, Z_AXIS };
enum Orientation { NONE, X_UP, X_DOWN, Y_UP, Y_DOWN, Z_UP, Z_DOWN };
private:
int m_x_pin;
int m_y_pin;
int m_z_pin;
int ReadRawAxis(int axisPin)
{
analogRead(axisPin);
delay(1);
// -- Take the average over four consecutive reads
long reading = 0;
for (int i = 0; i < 4; i++) {
reading += analogRead(axisPin);
}
return reading/4;
}
public:
Accelerometer(int x_pin, int y_pin, int z_pin)
: m_x_pin(x_pin),
m_y_pin(y_pin),
m_z_pin(z_pin)
{}
int ReadAxis(Axis axis)
{
int raw = 0;
int scaled = 0;
// -- Scale to "milli-Gs"
if (axis == X_AXIS) {
raw = ReadRawAxis(m_x_pin);
scaled = map(raw, ACC_X_MIN, ACC_X_MAX, -1000, 1000);
}
if (axis == Y_AXIS) {
raw = ReadRawAxis(m_y_pin);
scaled = map(raw, ACC_Y_MIN, ACC_Y_MAX, -1000, 1000);
}
if (axis == Z_AXIS) {
raw = ReadRawAxis(m_z_pin);
scaled = map(raw, ACC_Z_MIN, ACC_Z_MAX, -1000, 1000);
}
return scaled;
}
Orientation getOrientation()
{
int xScaled = ReadAxis(X_AXIS);
int yScaled = ReadAxis(Y_AXIS);
int zScaled = ReadAxis(Z_AXIS);
Orientation orient = NONE;
if (xScaled > 800) orient = X_UP;
if (xScaled < -800) orient = X_DOWN;
if (yScaled > 800) orient = Y_UP;
if (yScaled < -800) orient = Y_DOWN;
if (zScaled > 800) orient = Z_UP;
if (zScaled < -800) orient = Z_DOWN;
return orient;
}
};
// ----------------------------------------------------------------------
// Momentary button
// ----------------------------------------------------------------------
class Button
{
public:
enum EventKind { NONE, CLICK, HOLD, RELEASE };
protected:
enum State { UP, START, DOWN };
};
class MomentaryButton : public Button
{
private:
int m_pin;
State m_state;
uint32_t m_start_time;
uint32_t m_start_hold_time;
uint32_t m_hold_time;
uint32_t m_hold_interval;
public:
MomentaryButton(int pin, int hold_interval)
: m_pin(pin),
m_state(UP),
m_start_time(0),
m_start_hold_time(0),
m_hold_time(0),
m_hold_interval(hold_interval)
{}
void init()
{
pinMode(m_pin, INPUT);
}
EventKind getEvent(uint32_t current_time)
{
unsigned long elapsed_time = current_time - m_start_time;
// -- Read raw button
int button_raw = digitalRead(m_pin);
// --- Cap sense button
// long cs = CapSense.capacitiveSensor(1);
// if (cs > 20) button = HIGH;
// else button = LOW;
EventKind buttonEvent = NONE;
switch (m_state) {
// -- First detection of button down -- just record the time
case UP:
if (button_raw == HIGH) {
m_state = START;
m_start_time = current_time;
m_start_hold_time = current_time;
}
break;
// -- Debouncing: if we detected a button push, check again after 50ms
// to make sure it is real
case START:
if (elapsed_time > 50) {
if (button_raw == HIGH)
m_state = DOWN;
else
m_state = UP;
}
break;
// -- Button was clicked
// Depending on how long it stays down, we either return CLICK or HOLD
case DOWN:
if (button_raw == HIGH) {
if (elapsed_time > 500) {
m_hold_time = current_time - m_start_hold_time;
if (m_hold_interval > 0) {
if (m_hold_time > m_hold_interval) {
buttonEvent = HOLD;
m_start_hold_time = current_time;
}
} else {
buttonEvent = HOLD;
}
}
} else {
if (elapsed_time > 500) {
buttonEvent = RELEASE;
m_hold_time = current_time - m_start_hold_time;
} else {
buttonEvent = CLICK;
}
m_state = UP;
}
break;
}
/*
if (buttonEvent != NONE) {
Serial.print("State: ");
Serial.print(m_state);
Serial.print(" Event: ");
Serial.print(buttonEvent);
Serial.print(" Time: ");
Serial.print(elapsed_time);
Serial.print("\n");
}
*/
return buttonEvent;
}
uint32_t getHoldTime()
{
return m_hold_time;
}
};
// ------------------------------------------------------------
// Sensors
// My home-brewed code for various kinds of sensors
// ------------------------------------------------------------
#include <Buttons.h>
#include <Accelerometer.h>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment