annotate projects/d-box/DboxSensors.cpp @ 12:a6beeba3a648

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