the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. please forgive... it my first c++ program

Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
VBDaniel
Date:
Sat Dec 13 22:26:44 2014 +0000
Commit message:
the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. ; please forgive... it my first c++ program

Changed in this revision

MMA845x.cpp Show annotated file Show diff for this revision Revisions of this file
MMA845x.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r e2c7e03219ab MMA845x.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA845x.cpp	Sat Dec 13 22:26:44 2014 +0000
@@ -0,0 +1,103 @@
+/* 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.
+*/
+/*edit by VBDaniel ...to by continued*/
+
+#include "MMA845x.h"
+
+#define X_AXIS  0x01
+#define Y_AXIS  0x03
+#define Z_AXIS  0x05
+#define SYSMOD  0x0B    // System Mode Register (Read Only)
+#define CRTL_REG1 0X2A  // ODR = 800 Hz, STANDBY Mode.
+#define CTRL_REG2 0x2B  // Sleep Enable, OS Modes, RST, ST
+#define CTRL_REG3 0x2C  // Wake from Sleep, IPOL, PP_OD
+#define CTRL_REG4 0x2D  // Interrupt enable register
+#define CTRL_REG5 0x2E  // Interrupt pin (INT1/INT2) map
+
+enum SYSMOD_BITS
+{
+    SYSMOD0 ,
+    SYSMOD1 ,
+    /*  SYSMOD[1:0]
+        System Mode. Default value: 00.
+        00: STANDBY mode
+        01: WAKE mode
+        10: SLEEP mode
+    */
+    FGT_1 ,
+    FGT_2 ,
+    FGT_3 ,
+    FGT_4 ,
+    FGERR ,
+};
+
+
+MMA845x::MMA845x(PinName sda, PinName scl, int i2cAddress, PinName intPin1, PinName intPin2) : i2cBus(sda, scl), i2cAddress(i2cAddress), int1(intPin1), int2(intPin2)
+{
+    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);
+}
+
+
+void MMA845x::writeRegister(uint8_t* data, int length)
+{
+}
diff -r 000000000000 -r e2c7e03219ab MMA845x.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA845x.h	Sat Dec 13 22:26:44 2014 +0000
@@ -0,0 +1,57 @@
+/* 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.
+*/
+/*edit by VBDaniel ...to by continued*/
+
+
+#ifndef MMA845X_H
+#define MMA845X_H
+
+#include "mbed.h"
+
+class MMA845x
+{
+public:
+ //   MMA845x(PinName sda, PinName scl, int i2cAddress);
+    MMA845x(PinName sda, PinName scl, int i2cAddress, PinName int1, PinName int2);
+    ~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;    
+    InterruptIn int1;
+    InterruptIn int2;
+    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
diff -r 000000000000 -r e2c7e03219ab USBDevice.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Sat Dec 13 22:26:44 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#dfe51ad5cacf
diff -r 000000000000 -r e2c7e03219ab main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Dec 13 22:26:44 2014 +0000
@@ -0,0 +1,172 @@
+#include "mbed.h"
+#include "USBSerial.h"
+#include <stdlib.h>
+#include "MMA845x.h"
+
+//Virtual serial port over USB
+USBSerial serial;
+
+//accelerometer
+PinName const SDA = P0_5;
+PinName const SCL = P0_4;
+PinName const INT1 = P0_20;
+PinName const INT2 = P0_2;
+#define MMA8451_I2C_ADDRESS (0x1C << 1)
+
+MMA845x acc(SDA, SCL, MMA8451_I2C_ADDRESS, INT1, INT2 );
+
+//Struct to represent the neighbors of an led
+//stored clockwise, starting up side
+struct circuit
+{
+    int up;
+    int right;
+    int down;
+    int left;
+};
+struct circuit layout[] = {{-1,-1, 1,-1},  // LED 0
+                           { 0, 3, 2,-1},  // LED 1
+                           { 1,-1,-1,-1},  // LED 2
+                           {-1, 5,-1, 1},  // LED 3
+                           {-1, 7, 5,-1},  // LED 4
+                           { 4, 8, 6, 3},  // LED 5
+                           { 5, 9,-1,-1},  // LED 6
+                           {-1,-1, 8, 4},  // LED 7 , LED0 rigth
+                           { 7,10, 9, 5},  // LED 8
+                           { 8,-1,-1, 6},  // LED 9
+                           {-1,12,-1, 8},  // LED 10
+                           {-1,-1,12,-1},  // LED 11
+                           {11,-1,13,10},  // LED 12
+                           {12,-1,-1,-1},  // LED 13
+                          }; 
+ 
+DigitalOut LEDs[] = {(P0_19), (P0_18), (P0_17), (P1_15), (P0_23), (P0_14), (P0_15),
+                     (P0_9), (P0_8), (P0_7), (P0_21), (P0_10), (P0_22), (P0_11)
+                    };// declare 10 LEDs
+    
+DigitalOut Buzzer(P0_13);
+
+//catch the blinking LED
+volatile int destLed = 13;
+void blink()
+{
+    LEDs[destLed] = !LEDs[destLed];
+}
+
+void change_dest()
+{
+    int new_dest ;
+    do {
+        new_dest = rand() % 14 ;
+        }
+    while ( LEDs[new_dest] );
+    
+    LEDs[destLed] = false;
+    destLed = new_dest;
+}                      
+                           
+main()
+{
+    srand((unsigned)(acc.getX()+acc.getY()));
+    change_dest();
+
+    int X1;
+    int Y1;
+    int Z1;
+    
+    int i;
+     
+    Ticker blink_timer;
+    blink_timer.attach(&blink,0.15);
+    
+    Ticker dest_timer;
+    dest_timer.attach(&change_dest,1.0);
+
+    for (i=0;i<14;i++)
+    {
+        LEDs[i] = false;
+        i++;
+    }
+    Buzzer = false;
+    
+    int activeLED = 0;
+    int nextLED = -1;
+    LEDs[activeLED] = true;
+    int speed = 300;
+    int speedfactor = 4;
+ 
+    i = 0;
+    do {
+        i++;         
+        
+        X1=0;
+        Y1=0;
+        Z1=0;
+        
+        for(int j=1;j<10;j++) {
+            X1 += acc.getX();
+            Y1 += acc.getY();
+            Z1 += acc.getZ();
+            };
+             
+        X1 /= 10;
+        Y1 /= 10;
+        Z1 /= 10;
+        
+ /*     double c = sqrt( pow((double) X1,2.0)+pow((double) Y1,2.0)+pow((double) Z1,2.0)  );
+
+        serial.printf("Nr : %d \n",i);
+        serial.printf("X-Axis: %d \n",X1);
+        serial.printf("Y-Axis: %d \n",Y1);
+        serial.printf("Z-Axis: %d \n",Z1);
+        serial.printf("G: %f \n",c);   */
+        
+        nextLED = -1;
+        if ((abs(X1)>10) or (abs(Y1)>10)) {
+            if (abs(X1) > abs(Y1)) {
+                speed = 300 - abs(X1);
+                if (X1 > 0 ) nextLED = layout[activeLED].right ;
+                else nextLED = layout[activeLED].left;
+                }
+            else {
+                speed = 300 - abs(Y1);
+                if (Y1 < 0 ) nextLED = layout[activeLED].up ;
+                else nextLED = layout[activeLED].down;
+            };
+            if (nextLED > -1) {
+                LEDs[activeLED]= false;
+                activeLED = nextLED;
+                LEDs[activeLED]= true;
+                 
+                if ( speed < 150 ) speed=150;
+                wait_ms(speed/speedfactor);
+                Buzzer = false;
+            }
+            else  {
+                for (int j=0;j<128;j++) {
+                    Buzzer = !Buzzer ;
+                    wait_ms(speed/(speedfactor*32));
+                }
+            }
+        }
+        else {
+            speed=300;
+            wait_ms(speed/speedfactor);
+        }; 
+                           
+    }
+    while (activeLED != destLed);
+    dest_timer.detach();
+    blink_timer.detach();
+    destLed = activeLED;
+    blink_timer.attach(&blink,0.1);
+    
+    for (int j=0;j<50;j++) {
+        Buzzer = !Buzzer ;
+        wait_ms(50);
+    }
+    blink_timer.detach();
+    LEDs[activeLED] = false;    
+    deepsleep();    
+}
+
diff -r 000000000000 -r e2c7e03219ab mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Dec 13 22:26:44 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file