The preloaded firmware shipped on the E-Dice.
Dependencies: mbed
Revision 0:7c263455b8b9, committed 2014-09-13
- Comitter:
- Experiment626
- Date:
- Sat Sep 13 03:17:50 2014 +0000
- Child:
- 1:a30065092967
- Commit message:
- Official E-Dice Firmware 1.0
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA845x.cpp Sat Sep 13 03:17:50 2014 +0000 @@ -0,0 +1,74 @@ +/* Copyright (c) 2014 GHI Electronics, LLC +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software +* and associated documentation files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or +* substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "MMA845x.h" + +#define X_AXIS 0x01 +#define Y_AXIS 0x03 +#define Z_AXIS 0x05 + +MMA845x::MMA845x(PinName sda, PinName scl, int i2cAddress) : i2cBus(sda, scl), i2cAddress(i2cAddress) +{ + uint8_t initializeDataCommand[2] = {0x2A, 0x01}; + i2cBus.write(i2cAddress, (char*)initializeDataCommand, 2); +} + +MMA845x::~MMA845x() +{ +} + +int16_t MMA845x::getX() +{ + return readAxisRegister(X_AXIS); +} + +int16_t MMA845x::getY() +{ + return readAxisRegister(Y_AXIS); +} + +int16_t MMA845x::getZ() +{ + return readAxisRegister(Z_AXIS); +} + +void MMA845x::GetXYZ(int& x, int& y, int& z) +{ +} + +int16_t MMA845x::readAxisRegister(uint8_t axisRegisterAddress) +{ + uint8_t axisData[2]; + readRegister(axisRegisterAddress, axisData, 2); + + int16_t rawAxisData = ((axisData[0] << 2) | (axisData[1] >> 6)); + + if (rawAxisData > 511) + rawAxisData = rawAxisData - 1024; + + return rawAxisData; +} + +void MMA845x::readRegister(uint8_t registerAddress, uint8_t* data, int length) +{ + char convertedRegisterAddress[] = {registerAddress}; + i2cBus.write(i2cAddress, convertedRegisterAddress, 1, true); + i2cBus.read(i2cAddress, (char*)data, length); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA845x.h Sat Sep 13 03:17:50 2014 +0000 @@ -0,0 +1,54 @@ +/* Copyright (c) 2014 GHI Electronics, LLC +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software +* and associated documentation files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or +* substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef MMA845X_H +#define MMA845X_H + +#include "mbed.h" + +class MMA845x +{ +public: + MMA845x(PinName sda, PinName scl, int i2cAddress); + ~MMA845x(); + + int16_t getX(); + int16_t getY(); + int16_t getZ(); + void GetXYZ(int& x, int& y, int& z); + + struct AccelerationAxes + { + int x; + int y; + int z; + }; + +private: + I2C i2cBus; + int i2cAddress; + void readRegister(uint8_t registerAddress, uint8_t* data, int length); + int16_t readAxisRegister(uint8_t axisRegisterAddress); + //void writeRegister(uint8_t * data, int length); + //int16_t getAccAxis(uint8_t address); + +}; + +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Sep 13 03:17:50 2014 +0000 @@ -0,0 +1,289 @@ +#include "mbed.h" +#include <stdlib.h> +#include "MMA845x.h" + +PinName const SDA = P0_5; +PinName const SCL = P0_4; + +#define MMA8451_I2C_ADDRESS (0x1C << 1) + +//enum DieBitMask { ONE = 8, TWO = 65, THREE = 73, FOUR = 85, FIVE = 93, SIX = 119 }; +enum DIE { LEFT = 0, RIGHT = 1 }; + +void SetDieNumber( DIE leftRight, int dieFaceNumber); +void soundBuzzer(int Hz); +void RandomRollCycle(int iteration); +void ClearLEDs(); +int SetSensitivityLevel(); +void ProgramSplash(); +void ProgramStart(); + +DigitalOut LeftDieLED[] = {(P0_19), (P0_18), (P0_17), (P1_15), (P0_23), (P0_14), (P0_15)};// declare 7 LEDs on Left Die +DigitalOut RightDieLED[] = {(P0_9), (P0_8), (P0_7), (P0_21), (P0_10), (P0_22), (P0_11)};// declare 7 LEDs on Right Die +DigitalOut Buzzer(P0_13); + +DigitalIn userButton1(P0_1, PullUp); +DigitalIn userButton2(P0_16); + +MMA845x acc(SDA, SCL, MMA8451_I2C_ADDRESS); + +int frequencyInHz = 400; +int sensitivityLevel = 1; + +main() +{ + ProgramSplash(); + + while(true) + { + if(!userButton2) + SetSensitivityLevel(); + + if(!userButton1) + ProgramStart(); + } +} + +void SetDieNumber( DIE leftRight, int dieFaceNumber) +{ + switch (dieFaceNumber) + { + case 1: + if(leftRight == LEFT) + { + LeftDieLED[3] = true; + } + else + { + RightDieLED[3] = true; + } + break; + case 2: + if(leftRight == LEFT) + { + LeftDieLED[0] = true;LeftDieLED[6] = true; + } + else + { + RightDieLED[0] = true;RightDieLED[6] = true; + } + break; + case 3: + if(leftRight == LEFT) + { + LeftDieLED[0] = true;LeftDieLED[3] = true;LeftDieLED[6] = true; + } + else + { + RightDieLED[0] = true;RightDieLED[3] = true;RightDieLED[6] = true; + } + break; + case 4: + if(leftRight == LEFT) + { + LeftDieLED[0] = true;LeftDieLED[2] = true;LeftDieLED[4] = true;LeftDieLED[6] = true; + } + else + { + RightDieLED[0] = true;RightDieLED[2] = true;RightDieLED[4] = true;RightDieLED[6] = true; + } + break; + case 5: + if(leftRight == LEFT) + { + LeftDieLED[0] = true;LeftDieLED[2] = true;LeftDieLED[3] = true;LeftDieLED[4] = true;LeftDieLED[6] = true; + } + else + { + RightDieLED[0] = true;RightDieLED[2] = true;RightDieLED[3] = true;RightDieLED[4] = true;RightDieLED[6] = true; + } + break; + case 6: + if(leftRight == LEFT) + { + LeftDieLED[0] = true;LeftDieLED[1] = true;LeftDieLED[2] = true;LeftDieLED[4] = true;LeftDieLED[5] = true;LeftDieLED[6] = true; + } + else + { + RightDieLED[0] = true;RightDieLED[1] = true;RightDieLED[2] = true;RightDieLED[4] = true;RightDieLED[5] = true;RightDieLED[6] = true; + } + break; + } +} + +void soundBuzzer(int Hz) +{ + int ticks = Hz/64; + int tickCount = 0; + float frequency = 1/(float)Hz; + + while(tickCount < ticks) + { + wait(frequency); + Buzzer = true; + wait(frequency); + Buzzer = false; + tickCount++; + } +} + +void RandomRollCycle(int iteration) +{ + int leftDieRoll = 0; + int rightDieRoll = 0; + + for(int soundLoop = iteration; soundLoop > 5; soundLoop--) + { + leftDieRoll = rand() % 6 + 1; + rightDieRoll = rand() % 6 + 1; + + ClearLEDs(); + + SetDieNumber(LEFT, leftDieRoll); + soundBuzzer(1000); + + SetDieNumber(RIGHT, rightDieRoll); + soundBuzzer(500); + + wait(1/(float)soundLoop); + } +} + +void ClearLEDs() +{ + for(int y = 0; y < 7; y++) + { + LeftDieLED[y] = false; + RightDieLED[y] = false; + } +} + +int SetSensitivityLevel() +{ + ClearLEDs(); + + sensitivityLevel++; + + if(sensitivityLevel > 6) + sensitivityLevel = 1; + + SetDieNumber(LEFT, sensitivityLevel); + + soundBuzzer(600); + wait(.25); +} + +void ProgramSplash() +{ + for(int x = 200; x < 1000; x+=20) + soundBuzzer(x); + + SetDieNumber(LEFT, sensitivityLevel); +} + +void ProgramStart() +{ + ClearLEDs(); + for(int g = 0; g < 4; g++) + { + soundBuzzer(1000); + wait(0.100); + } + SetDieNumber(RIGHT, 6); + SetDieNumber(LEFT, 6); + + + + int16_t zAxis = 0; + + int deltaX; + int X1; + int X2; + + int deltaY; + int Y1; + int Y2; + + int deltaZ; + int Z1; + int Z2; + + zAxis = acc.getZ(); + srand (abs(zAxis)); + +// int inactivityCounter = 0; + + while(true) + { + X1 = acc.getX(); + Y1 = acc.getY(); + Z1 = acc.getZ(); + wait(.25); + X2 = acc.getX(); + Y2 = acc.getY(); + Z2 = acc.getZ(); + + deltaX = X2 - X1; + deltaY = Y2 - Y1; + deltaZ = Z2 - Z1; + + if(abs(deltaZ) >> (sensitivityLevel + 2)) + RandomRollCycle(26); + +/* + inactivityCounter++; + if(inactivityCounter > 500) + { + inactivityCounter = 0; + deepsleep(); + } +*/ + } + +} + + + + + + + +/* + +intialize() +{ + set pins to outputs for example + maybe initialize teh accelerometer + AcelInit(); + } + +int Getsense() +{ + int sese =1; + while(button not pressed) + { + if(othe rbutton) + { + sense++; + if sense > 6) sense =1; + SetLEDs(LEFT, sense); + } + + } + return sense; +} +void main() +{ + Intialize(); + + sense = GetSensetivity(); + + while(1) + { + int x = getaccel(); + if(x >> (sense+3)) + NewDiceRandom(); + + } +} +*/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Sep 13 03:17:50 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file