pls

Dependencies:   BLE_API mbed nRF51822

Fork of zach_thresholding by Zachary Newman

Committer:
nkosarek
Date:
Tue May 02 04:16:38 2017 +0000
Revision:
15:d117591084ff
Parent:
14:700ba072d1a0
Child:
16:799397f0d3a8
Working i2c to ble to pi without serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkosarek 15:d117591084ff 1 /*
nkosarek 15:d117591084ff 2
nkosarek 15:d117591084ff 3 Copyright (c) 2012-2014 RedBearLab
nkosarek 15:d117591084ff 4
nkosarek 15:d117591084ff 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nkosarek 15:d117591084ff 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
nkosarek 15:d117591084ff 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
nkosarek 15:d117591084ff 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
nkosarek 15:d117591084ff 9 subject to the following conditions:
nkosarek 15:d117591084ff 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
nkosarek 15:d117591084ff 11
nkosarek 15:d117591084ff 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
nkosarek 15:d117591084ff 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
nkosarek 15:d117591084ff 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
nkosarek 15:d117591084ff 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
nkosarek 15:d117591084ff 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nkosarek 15:d117591084ff 17
nkosarek 15:d117591084ff 18 */
nkosarek 15:d117591084ff 19
rgrover1 0:28f095301cb2 20 #include "mbed.h"
andresag 10:7943b5c1117a 21 #include "ble/BLE.h"
nkosarek 15:d117591084ff 22 #include "wire.h"
rgrover1 0:28f095301cb2 23 #include "ButtonService.h"
nkosarek 15:d117591084ff 24
nkosarek 15:d117591084ff 25 #define BLE_Nano
nkosarek 15:d117591084ff 26 //#define nRF_51822
nkosarek 15:d117591084ff 27
nkosarek 15:d117591084ff 28 #define LIS331HH
nkosarek 15:d117591084ff 29
nkosarek 15:d117591084ff 30 #ifdef nRF_51822
nkosarek 15:d117591084ff 31 #define SCL 28
nkosarek 15:d117591084ff 32 #define SDA 29
nkosarek 15:d117591084ff 33 #endif
nkosarek 15:d117591084ff 34
nkosarek 15:d117591084ff 35 #ifdef BLE_Nano
nkosarek 15:d117591084ff 36 #define SCL P0_8
nkosarek 15:d117591084ff 37 #define SDA P0_10
nkosarek 15:d117591084ff 38 #endif
nkosarek 15:d117591084ff 39
nkosarek 15:d117591084ff 40
nkosarek 15:d117591084ff 41 #ifdef LIS3DH
nkosarek 15:d117591084ff 42 #define ADDR_ONE 0x30
nkosarek 15:d117591084ff 43 #define ADDR_TWO 0x32
nkosarek 15:d117591084ff 44 #define AXIS_X 0x00
nkosarek 15:d117591084ff 45 #define AXIS_Y 0x01
nkosarek 15:d117591084ff 46 #define AXIS_Z 0x02
nkosarek 15:d117591084ff 47 #define REG_OUT_X_L 0x28
nkosarek 15:d117591084ff 48 #define REG_CTRL1 0x20
nkosarek 15:d117591084ff 49 #define REG_CTRL4 0x23
nkosarek 15:d117591084ff 50 #define REG_WHOAMI 0x0F
nkosarek 15:d117591084ff 51 #define RANGE_2G 0x00
nkosarek 15:d117591084ff 52 #define DEVICE_ID 0x33
nkosarek 15:d117591084ff 53
nkosarek 15:d117591084ff 54
nkosarek 15:d117591084ff 55 #define DATARATE_400HZ 0b0111 // 400Hz
nkosarek 15:d117591084ff 56 #define DATARATE_200HZ 0b0110 // 200Hz
nkosarek 15:d117591084ff 57 #define DATARATE_100HZ 0b0101 // 100Hz
nkosarek 15:d117591084ff 58 #define DATARATE_50HZ 0b0100 // 50Hz
nkosarek 15:d117591084ff 59 #define DATARATE_25HZ 0b0011 // 25Hz
nkosarek 15:d117591084ff 60 #define DATARATE_10HZ 0b0010 // 10Hz
nkosarek 15:d117591084ff 61 #define DATARATE_1HZ 0b0001 // 1Hz
nkosarek 15:d117591084ff 62 #define DATARATE_POWERDOWN 0 // Power down
nkosarek 15:d117591084ff 63 #define DATARATE_LOWPOWER_1K6HZ 0b1000 // Low power mode (1.6KHz)
nkosarek 15:d117591084ff 64 #define DATARATE_LOWPOWER_5KHZ 0b1001 // Low power mode (5KHz) / Normal power mode (1.25KHz)
nkosarek 15:d117591084ff 65 #endif
nkosarek 15:d117591084ff 66
nkosarek 15:d117591084ff 67
nkosarek 15:d117591084ff 68 #ifdef LIS331HH
nkosarek 15:d117591084ff 69 #define ADDR_ONE 0x30
nkosarek 15:d117591084ff 70 #define ADDR_TWO 0x32
nkosarek 15:d117591084ff 71 #define AXIS_X 0x00
nkosarek 15:d117591084ff 72 #define AXIS_Y 0x01
nkosarek 15:d117591084ff 73 #define AXIS_Z 0x02
nkosarek 15:d117591084ff 74 #define REG_OUT_X_L 0x28
nkosarek 15:d117591084ff 75 #define REG_CTRL1 0x20
nkosarek 15:d117591084ff 76 #define REG_CTRL4 0x23
nkosarek 15:d117591084ff 77 #define REG_WHOAMI 0x0F
nkosarek 15:d117591084ff 78 #define RANGE_2G 0x00
nkosarek 15:d117591084ff 79 #define DEVICE_ID 0x33
nkosarek 15:d117591084ff 80
nkosarek 15:d117591084ff 81
nkosarek 15:d117591084ff 82 #define DATARATE_1KHZ 0b11 // 1000Hz
nkosarek 15:d117591084ff 83 #define DATARATE_400HZ 0b10 // 400Hz
nkosarek 15:d117591084ff 84 #define DATARATE_100HZ 0b01 // 100Hz
nkosarek 15:d117591084ff 85 #define DATARATE_50HZ 0b00 // 50Hz
nkosarek 15:d117591084ff 86 #define DATARATE_POWERDOWN 0 // Power down
nkosarek 15:d117591084ff 87 #define DATARATE_NORMAL_MODE 0b001
nkosarek 15:d117591084ff 88 #define DATARATE_LOWPOWER_0.5HZ 0b010
nkosarek 15:d117591084ff 89 #define DATARATE_LOWPOWER_1HZ 0b011
nkosarek 15:d117591084ff 90 #define DATARATE_LOWPOWER_2HZ 0b100
nkosarek 15:d117591084ff 91 #define DATARATE_LOWPOWER_5HZ 0b101
nkosarek 15:d117591084ff 92 #define DATARATE_LOWPOWER_10HZ 0b110
nkosarek 15:d117591084ff 93 #endif
nkosarek 15:d117591084ff 94
nkosarek 15:d117591084ff 95 #define PACKET_SIZE 20
nkosarek 15:d117591084ff 96 #define QUEUE_SIZE 20
nkosarek 15:d117591084ff 97
nkosarek 15:d117591084ff 98
znew711 11:fd1cf9dbf3a4 99 const static char DEVICE_NAME[] = "LUMBERJACK_NANO";
rgrover1 0:28f095301cb2 100 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
nkosarek 15:d117591084ff 101
nkosarek 15:d117591084ff 102 struct packetQueue
nkosarek 15:d117591084ff 103 {
nkosarek 15:d117591084ff 104 uint16_t size;
nkosarek 15:d117591084ff 105 uint16_t nextPacketToSend;
nkosarek 15:d117591084ff 106 uint16_t nextSampleToSave;
nkosarek 15:d117591084ff 107 uint16_t liveSamples;
nkosarek 15:d117591084ff 108 uint8_t packets[QUEUE_SIZE][PACKET_SIZE];
nkosarek 15:d117591084ff 109 };
nkosarek 15:d117591084ff 110
nkosarek 15:d117591084ff 111 packetQueue pq;
nkosarek 15:d117591084ff 112
nkosarek 15:d117591084ff 113 void addToQueue(uint8_t* packet) {
nkosarek 15:d117591084ff 114 for (int i = 0; i < PACKET_SIZE; i++) {
nkosarek 15:d117591084ff 115 pq.packets[pq.nextSampleToSave][i] = packet[i];
nkosarek 15:d117591084ff 116 }
nkosarek 15:d117591084ff 117 if (pq.nextPacketToSend == pq.nextSampleToSave && pq.liveSamples > 0) {
nkosarek 15:d117591084ff 118 pq.nextSampleToSave = (pq.nextSampleToSave + 1) % QUEUE_SIZE;
nkosarek 15:d117591084ff 119 pq.nextPacketToSend = (pq.nextPacketToSend + 1) % QUEUE_SIZE;
nkosarek 15:d117591084ff 120 } else {
nkosarek 15:d117591084ff 121 pq.liveSamples += 1;
nkosarek 15:d117591084ff 122 pq.nextSampleToSave = (pq.nextSampleToSave + 1) % QUEUE_SIZE;
nkosarek 15:d117591084ff 123 }
nkosarek 15:d117591084ff 124 return;
nkosarek 15:d117591084ff 125 }
nkosarek 15:d117591084ff 126
nkosarek 15:d117591084ff 127 uint8_t* removeFromQueue() {
nkosarek 15:d117591084ff 128 if (pq.nextSampleToSave != pq.nextPacketToSend && pq.liveSamples > 0) {
nkosarek 15:d117591084ff 129 pq.liveSamples -= 1;
nkosarek 15:d117591084ff 130 uint8_t* old = pq.packets[pq.nextPacketToSend];
nkosarek 15:d117591084ff 131 pq.nextPacketToSend = (pq.nextPacketToSend + 1) % QUEUE_SIZE;
nkosarek 15:d117591084ff 132 return old;
nkosarek 15:d117591084ff 133 } else {
nkosarek 15:d117591084ff 134 return NULL;
nkosarek 15:d117591084ff 135 }
nkosarek 15:d117591084ff 136 }
nkosarek 15:d117591084ff 137
nkosarek 15:d117591084ff 138
nkosarek 15:d117591084ff 139 //Serial pc(USBTX, USBRX);
nkosarek 15:d117591084ff 140 TwoWire Wire = TwoWire(NRF_TWI0);
nkosarek 15:d117591084ff 141
andresag 10:7943b5c1117a 142 static ButtonService *buttonServicePtr;
znew711 11:fd1cf9dbf3a4 143 bool isThereAConnection = false;
nkosarek 15:d117591084ff 144
znew711 11:fd1cf9dbf3a4 145 void sleep(unsigned int mseconds)
rgrover1 0:28f095301cb2 146 {
znew711 11:fd1cf9dbf3a4 147 clock_t goal = mseconds + clock();
znew711 11:fd1cf9dbf3a4 148 while (goal > clock());
rgrover1 0:28f095301cb2 149 }
nkosarek 15:d117591084ff 150
rgrover1 8:a7ba7aaba460 151 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 152 {
andresag 10:7943b5c1117a 153 BLE::Instance().gap().startAdvertising();
znew711 11:fd1cf9dbf3a4 154 isThereAConnection = false;
rgrover1 0:28f095301cb2 155 }
nkosarek 15:d117591084ff 156
znew711 11:fd1cf9dbf3a4 157 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 158 {
nkosarek 15:d117591084ff 159 //pc.printf("Connection recieved!\r\n");
znew711 11:fd1cf9dbf3a4 160 isThereAConnection = true;
rgrover1 0:28f095301cb2 161 }
nkosarek 15:d117591084ff 162
andresag 10:7943b5c1117a 163 /**
andresag 10:7943b5c1117a 164 * This function is called when the ble initialization process has failled
andresag 10:7943b5c1117a 165 */
andresag 10:7943b5c1117a 166 void onBleInitError(BLE &ble, ble_error_t error)
andresag 10:7943b5c1117a 167 {
andresag 10:7943b5c1117a 168 /* Initialization error handling should go here */
andresag 10:7943b5c1117a 169 }
nkosarek 15:d117591084ff 170
andresag 10:7943b5c1117a 171 /**
andresag 10:7943b5c1117a 172 * Callback triggered when the ble initialization process has finished
andresag 10:7943b5c1117a 173 */
andresag 10:7943b5c1117a 174 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 10:7943b5c1117a 175 {
andresag 10:7943b5c1117a 176 BLE& ble = params->ble;
andresag 10:7943b5c1117a 177 ble_error_t error = params->error;
nkosarek 15:d117591084ff 178
andresag 10:7943b5c1117a 179 if (error != BLE_ERROR_NONE) {
andresag 10:7943b5c1117a 180 /* In case of error, forward the error handling to onBleInitError */
andresag 10:7943b5c1117a 181 onBleInitError(ble, error);
andresag 10:7943b5c1117a 182 return;
andresag 10:7943b5c1117a 183 }
nkosarek 15:d117591084ff 184
andresag 10:7943b5c1117a 185 /* Ensure that it is the default instance of BLE */
andresag 10:7943b5c1117a 186 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 10:7943b5c1117a 187 return;
andresag 10:7943b5c1117a 188 }
nkosarek 15:d117591084ff 189
andresag 10:7943b5c1117a 190 ble.gap().onDisconnection(disconnectionCallback);
znew711 11:fd1cf9dbf3a4 191 ble.gap().onConnection(connectionCallback);
nkosarek 15:d117591084ff 192
andresag 10:7943b5c1117a 193 /* Setup primary service */
nkosarek 15:d117591084ff 194 uint8_t initial_value[20] = {0, 0, 0, 0, 0,
nkosarek 15:d117591084ff 195 0, 0, 0, 0, 0,
nkosarek 15:d117591084ff 196 0, 0, 0, 0, 0,
nkosarek 15:d117591084ff 197 0, 0, 0, 0, 0};
nkosarek 15:d117591084ff 198 buttonServicePtr = new ButtonService(ble, initial_value);
nkosarek 15:d117591084ff 199
andresag 10:7943b5c1117a 200 /* setup advertising */
andresag 10:7943b5c1117a 201 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
andresag 10:7943b5c1117a 202 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
andresag 10:7943b5c1117a 203 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag 10:7943b5c1117a 204 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag 10:7943b5c1117a 205 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
nkosarek 15:d117591084ff 206 //pc.printf("start advertising now \r\n");
andresag 10:7943b5c1117a 207 ble.gap().startAdvertising();
andresag 10:7943b5c1117a 208 }
nkosarek 15:d117591084ff 209
nkosarek 15:d117591084ff 210 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
nkosarek 15:d117591084ff 211 {
nkosarek 15:d117591084ff 212 Wire.beginTransmission(i2cAddr);
nkosarek 15:d117591084ff 213 int err = Wire.write( (uint8_t)addr );
nkosarek 15:d117591084ff 214 Wire.write(pbuf, length);
nkosarek 15:d117591084ff 215 if (err != 0) {
nkosarek 15:d117591084ff 216 //pc.printf("error on write write! %d\r\n", err);
nkosarek 15:d117591084ff 217 }
nkosarek 15:d117591084ff 218 uint8_t err8 = Wire.endTransmission();
nkosarek 15:d117591084ff 219 if (err8 != 0) {
nkosarek 15:d117591084ff 220 //pc.printf("error on write end transmission! %d\r\n", err8);
nkosarek 15:d117591084ff 221 }
nkosarek 15:d117591084ff 222 }
nkosarek 15:d117591084ff 223
nkosarek 15:d117591084ff 224 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length, uint16_t i2cAddr)
nkosarek 15:d117591084ff 225 {
nkosarek 15:d117591084ff 226 Wire.beginTransmission(i2cAddr);
nkosarek 15:d117591084ff 227 int err= Wire.write( (uint8_t)addr );
nkosarek 15:d117591084ff 228 if (err != 0) {
nkosarek 15:d117591084ff 229 //pc.printf("error on read write! %d\r\n", err);
nkosarek 15:d117591084ff 230 }
nkosarek 15:d117591084ff 231 uint8_t err8 = Wire.endTransmission();
nkosarek 15:d117591084ff 232 if (err8 != 0) {
nkosarek 15:d117591084ff 233 //pc.printf("error on read end transmission! %d\r\n", err8);
nkosarek 15:d117591084ff 234 }
nkosarek 15:d117591084ff 235
nkosarek 15:d117591084ff 236 err8 = Wire.requestFrom(i2cAddr+1, length);
nkosarek 15:d117591084ff 237 if (err != 0) {
nkosarek 15:d117591084ff 238 //pc.printf("error on read request from! %d\r\n", err8);
nkosarek 15:d117591084ff 239 }
nkosarek 15:d117591084ff 240 while( Wire.available() > 0 )
nkosarek 15:d117591084ff 241 {
nkosarek 15:d117591084ff 242 *pbuf = Wire.read();
nkosarek 15:d117591084ff 243 pbuf++;
nkosarek 15:d117591084ff 244 }
nkosarek 15:d117591084ff 245 }
nkosarek 15:d117591084ff 246
nkosarek 15:d117591084ff 247 //Set the bit at index 'bit' to 'value' on 'input' and return
nkosarek 15:d117591084ff 248 uint8_t setBit(uint8_t input, uint8_t bit, uint8_t value) {
nkosarek 15:d117591084ff 249 uint8_t mask = 1 << bit;
nkosarek 15:d117591084ff 250 input &= ~mask;
nkosarek 15:d117591084ff 251 if (value == 1) {
nkosarek 15:d117591084ff 252 input |= mask;
nkosarek 15:d117591084ff 253 }
nkosarek 15:d117591084ff 254 return input;
nkosarek 15:d117591084ff 255 }
nkosarek 15:d117591084ff 256
nkosarek 15:d117591084ff 257 uint16_t getAxis(uint16_t axis, uint16_t i2cAddr)
nkosarek 15:d117591084ff 258 {
nkosarek 15:d117591084ff 259 uint8_t base = REG_OUT_X_L + (2 * axis);
nkosarek 15:d117591084ff 260 uint8_t* low = new uint8_t[1];
nkosarek 15:d117591084ff 261 uint8_t* high = new uint8_t[1];
nkosarek 15:d117591084ff 262 AT24C512_ReadBytes(base, low, 1, i2cAddr);
nkosarek 15:d117591084ff 263 AT24C512_ReadBytes(base + 1, high, 1, i2cAddr);
nkosarek 15:d117591084ff 264 uint16_t res = low[0] | (high[0] << 8);
nkosarek 15:d117591084ff 265 delete[] low;
nkosarek 15:d117591084ff 266 delete[] high;
nkosarek 15:d117591084ff 267 return res;
nkosarek 15:d117591084ff 268 }
nkosarek 15:d117591084ff 269
nkosarek 15:d117591084ff 270 void setRange(uint8_t range, uint16_t i2cAddr) {
nkosarek 15:d117591084ff 271 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 272 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
nkosarek 15:d117591084ff 273 val[0] &= ~(0b110000); //zero out lowest 2 bits of top 4 bits
nkosarek 15:d117591084ff 274 val[0] |= (range << 4); // write in our new range
nkosarek 15:d117591084ff 275 //pc.printf("REG_CTRL4 after setRange: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 276 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
nkosarek 15:d117591084ff 277 delete[] val;
nkosarek 15:d117591084ff 278 }
nkosarek 15:d117591084ff 279
nkosarek 15:d117591084ff 280 //Set whether we want to use high resolution or not
nkosarek 15:d117591084ff 281 void setHighResolution(bool highRes, uint16_t i2cAddr) {
nkosarek 15:d117591084ff 282 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 283 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
nkosarek 15:d117591084ff 284 uint8_t final;
nkosarek 15:d117591084ff 285 if (highRes) {
nkosarek 15:d117591084ff 286 final = setBit(val[0], 3, 1);
nkosarek 15:d117591084ff 287 } else {
nkosarek 15:d117591084ff 288 final = setBit(val[0], 3, 0);
nkosarek 15:d117591084ff 289 }
nkosarek 15:d117591084ff 290 val[0] = final;
nkosarek 15:d117591084ff 291 //pc.printf("REG_CTRL4 after setHiRes: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 292 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
nkosarek 15:d117591084ff 293 delete[] val;
nkosarek 15:d117591084ff 294 }
nkosarek 15:d117591084ff 295
nkosarek 15:d117591084ff 296 void setAxisStatus(uint8_t axis, bool enable, uint16_t i2cAddr) {
nkosarek 15:d117591084ff 297 uint8_t* current = new uint8_t[1];
nkosarek 15:d117591084ff 298 AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);//get value from the register
nkosarek 15:d117591084ff 299 uint8_t final;
nkosarek 15:d117591084ff 300 if (enable == 1) {
nkosarek 15:d117591084ff 301 final = setBit(current[0], axis, 1);
nkosarek 15:d117591084ff 302 } else {
nkosarek 15:d117591084ff 303 final = setBit(current[0], axis, 0);
nkosarek 15:d117591084ff 304 }
nkosarek 15:d117591084ff 305 current[0] = final;
nkosarek 15:d117591084ff 306 AT24C512_WriteBytes(REG_CTRL1, current, 1, i2cAddr);
nkosarek 15:d117591084ff 307
nkosarek 15:d117591084ff 308 AT24C512_ReadBytes(REG_CTRL1, current, 1, i2cAddr);
nkosarek 15:d117591084ff 309 //pc.printf("REG_CTRL1 after setAxisStatus: 0x%x\r\n", *current);
nkosarek 15:d117591084ff 310 delete[] current;
nkosarek 15:d117591084ff 311 }
nkosarek 15:d117591084ff 312
nkosarek 15:d117591084ff 313 void setDataRate(uint8_t dataRate, uint16_t i2cAddr) {
nkosarek 15:d117591084ff 314 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 315 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 316 //pc.printf("REG_CTRL1 before data rate set: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 317 val[0] &= 0b11100111; //d
nkosarek 15:d117591084ff 318 val[0] |= (dataRate << 3);
nkosarek 15:d117591084ff 319 AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 320
nkosarek 15:d117591084ff 321 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 322 //pc.printf("REG_CTRL1 after data rate set: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 323 delete[] val;
nkosarek 15:d117591084ff 324 }
nkosarek 15:d117591084ff 325
nkosarek 15:d117591084ff 326 void setPowerMode(uint8_t powerMode, uint16_t i2cAddr) {
nkosarek 15:d117591084ff 327 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 328 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 329 val[0] &= 0b11111;
nkosarek 15:d117591084ff 330 val[0] |= (powerMode << 5);
nkosarek 15:d117591084ff 331 //pc.printf("writing this to REG_CTRL1: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 332 AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 333
nkosarek 15:d117591084ff 334 AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr);
nkosarek 15:d117591084ff 335 //pc.printf("REG_CTRL1 after power mode set: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 336 delete[] val;
nkosarek 15:d117591084ff 337 }
nkosarek 15:d117591084ff 338
nkosarek 15:d117591084ff 339 void setBDU(bool bdu, uint16_t i2cAddr)
nkosarek 15:d117591084ff 340 {
nkosarek 15:d117591084ff 341 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 342 AT24C512_ReadBytes(REG_CTRL4, val, 1, i2cAddr);//get value from the register
nkosarek 15:d117591084ff 343 //pc.printf("REG_CTRL4: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 344 uint8_t final;
nkosarek 15:d117591084ff 345 if (bdu) {
nkosarek 15:d117591084ff 346 final = setBit(val[0], 7, 1);
nkosarek 15:d117591084ff 347 } else {
nkosarek 15:d117591084ff 348 final = setBit(val[0], 7, 0);
nkosarek 15:d117591084ff 349 }
nkosarek 15:d117591084ff 350 val[0] = final;
nkosarek 15:d117591084ff 351 //pc.printf("REG_CTRL4 after setBDU: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 352 AT24C512_WriteBytes(REG_CTRL4, val, 1, i2cAddr);
nkosarek 15:d117591084ff 353 delete[] val;
nkosarek 15:d117591084ff 354 }
nkosarek 15:d117591084ff 355
nkosarek 15:d117591084ff 356 uint16_t getX(uint16_t i2cAddr)
nkosarek 15:d117591084ff 357 {
nkosarek 15:d117591084ff 358 return getAxis(AXIS_X, i2cAddr);
nkosarek 15:d117591084ff 359 }
nkosarek 15:d117591084ff 360
nkosarek 15:d117591084ff 361 uint16_t getY(uint16_t i2cAddr)
nkosarek 15:d117591084ff 362 {
nkosarek 15:d117591084ff 363 return getAxis(AXIS_Y, i2cAddr);
nkosarek 15:d117591084ff 364 }
nkosarek 15:d117591084ff 365
nkosarek 15:d117591084ff 366 uint16_t getZ(uint16_t i2cAddr)
nkosarek 15:d117591084ff 367 {
nkosarek 15:d117591084ff 368 return getAxis(AXIS_Z, i2cAddr);
nkosarek 15:d117591084ff 369 }
nkosarek 15:d117591084ff 370
rgrover1 0:28f095301cb2 371 int main(void)
rgrover1 0:28f095301cb2 372 {
nkosarek 15:d117591084ff 373 //pc.baud(9600);
nkosarek 15:d117591084ff 374 wait(5);
nkosarek 15:d117591084ff 375 //Wire.begin();
nkosarek 15:d117591084ff 376 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
nkosarek 15:d117591084ff 377
nkosarek 15:d117591084ff 378 //pc.printf("\r\n\r\n\r\nStarting...\r\n");
nkosarek 15:d117591084ff 379
nkosarek 15:d117591084ff 380 wait(5);
nkosarek 15:d117591084ff 381
nkosarek 15:d117591084ff 382 setAxisStatus(AXIS_X, true, ADDR_ONE);
nkosarek 15:d117591084ff 383 setAxisStatus(AXIS_Y, true, ADDR_ONE);
nkosarek 15:d117591084ff 384 setAxisStatus(AXIS_Z, true, ADDR_ONE);
nkosarek 15:d117591084ff 385 setDataRate(DATARATE_400HZ, ADDR_ONE);
nkosarek 15:d117591084ff 386 setPowerMode(DATARATE_NORMAL_MODE, ADDR_ONE);
nkosarek 15:d117591084ff 387 //setHighResolution(true, ADDR_ONE);
nkosarek 15:d117591084ff 388 setBDU(true, ADDR_ONE);
nkosarek 15:d117591084ff 389 //setRange(RANGE_2G, ADDR_ONE);
nkosarek 15:d117591084ff 390
nkosarek 15:d117591084ff 391
nkosarek 15:d117591084ff 392 setAxisStatus(AXIS_X, true, ADDR_TWO);
nkosarek 15:d117591084ff 393 setAxisStatus(AXIS_Y, true, ADDR_TWO);
nkosarek 15:d117591084ff 394 setAxisStatus(AXIS_Z, true, ADDR_TWO);
nkosarek 15:d117591084ff 395 setDataRate(DATARATE_400HZ, ADDR_TWO);
nkosarek 15:d117591084ff 396 setPowerMode(DATARATE_NORMAL_MODE, ADDR_TWO);
nkosarek 15:d117591084ff 397 //setHighResolution(true, ADDR_TWO);
nkosarek 15:d117591084ff 398 setBDU(true, ADDR_TWO);
nkosarek 15:d117591084ff 399 //setRange(RANGE_2G, ADDR_TWO);
nkosarek 15:d117591084ff 400
nkosarek 15:d117591084ff 401 uint8_t* val = new uint8_t[1];
nkosarek 15:d117591084ff 402 *val = 0x80;
nkosarek 15:d117591084ff 403 AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_ONE);
nkosarek 15:d117591084ff 404 AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_TWO);
nkosarek 15:d117591084ff 405 AT24C512_ReadBytes(REG_CTRL4, val, 1, ADDR_ONE);
nkosarek 15:d117591084ff 406 //pc.printf("REG_CTRL4, should be 0x80: 0x%x\r\n", *val);
nkosarek 15:d117591084ff 407
nkosarek 15:d117591084ff 408 uint8_t* whoami = new uint8_t[1];
nkosarek 15:d117591084ff 409 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);
nkosarek 15:d117591084ff 410 //pc.printf("REG_WHOAMI should be 0x32: 0x%x\r\n", *whoami);
nkosarek 15:d117591084ff 411 AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_TWO);
nkosarek 15:d117591084ff 412 //pc.printf("REG_WHOAMI should be 0x32: 0x%x\r\n", *whoami);
nkosarek 15:d117591084ff 413 AT24C512_ReadBytes(0x1F, whoami, 1, ADDR_ONE);
nkosarek 15:d117591084ff 414
nkosarek 15:d117591084ff 415
andresag 10:7943b5c1117a 416 BLE &ble = BLE::Instance();
andresag 10:7943b5c1117a 417 ble.init(bleInitComplete);
nkosarek 15:d117591084ff 418
nkosarek 15:d117591084ff 419 //pc.printf("entering spin loop\r\n");
andresag 10:7943b5c1117a 420 /* SpinWait for initialization to complete. This is necessary because the
andresag 10:7943b5c1117a 421 * BLE object is used in the main loop below. */
andresag 10:7943b5c1117a 422 while (ble.hasInitialized() == false) { /* spin loop */ }
nkosarek 15:d117591084ff 423 //pc.printf("leaving spin loop\r\n");
nkosarek 15:d117591084ff 424
nkosarek 15:d117591084ff 425 pq.size = QUEUE_SIZE;
nkosarek 15:d117591084ff 426 pq.nextPacketToSend = 0;
nkosarek 15:d117591084ff 427 pq.nextSampleToSave = 0;
nkosarek 15:d117591084ff 428 pq.liveSamples = 0;
nkosarek 15:d117591084ff 429
nkosarek 15:d117591084ff 430 while(1)
nkosarek 15:d117591084ff 431 {
nkosarek 15:d117591084ff 432 //pc.printf("Read data from AT24C512\r\n");
nkosarek 15:d117591084ff 433 uint16_t x1 = getX(ADDR_ONE);
nkosarek 15:d117591084ff 434 uint16_t y1 = getY(ADDR_ONE);
nkosarek 15:d117591084ff 435 uint16_t z1 = getZ(ADDR_ONE);
nkosarek 15:d117591084ff 436
nkosarek 15:d117591084ff 437 uint16_t x2 = getX(ADDR_TWO);
nkosarek 15:d117591084ff 438 uint16_t y2 = getY(ADDR_TWO);
nkosarek 15:d117591084ff 439 uint16_t z2 = getZ(ADDR_TWO);
nkosarek 15:d117591084ff 440
nkosarek 15:d117591084ff 441 //pc.printf("Accel one: x %d y %d z %d\r\n", (int16_t)x1, (int16_t)y1, (int16_t)z1);
nkosarek 15:d117591084ff 442 //pc.printf("Accel two: x %d y %d z %d\r\n", (int16_t)x2, (int16_t)y2, (int16_t)z2);
nkosarek 15:d117591084ff 443 //pc.printf("\r\n");
nkosarek 15:d117591084ff 444
znew711 11:fd1cf9dbf3a4 445 if(isThereAConnection) {
nkosarek 15:d117591084ff 446 //pc.printf("sending Notification\r\n");
nkosarek 15:d117591084ff 447 uint8_t values[20] = {(uint8_t)x1, (uint8_t)(x1 >> 8), (uint8_t)y1, (uint8_t)(y1 >> 8), (uint8_t)z1, (uint8_t)(z1 >> 8),
nkosarek 15:d117591084ff 448 (uint8_t)x2, (uint8_t)(x2 >> 8), (uint8_t)y2, (uint8_t)(y2 >> 8), (uint8_t)z2, (uint8_t)(z2 >> 8),
nkosarek 15:d117591084ff 449 0, 0, 0, 0, 0, 0, 0, 0};
nkosarek 15:d117591084ff 450 buttonServicePtr->updateButtonState(values);
rgrover1 9:0f6951db24f1 451 }
nkosarek 15:d117591084ff 452
nkosarek 15:d117591084ff 453 wait(1);
rgrover1 0:28f095301cb2 454 }
nkosarek 15:d117591084ff 455
nkosarek 15:d117591084ff 456 }