poi using dot stars as lights

Dependencies:   DotStarStrips

Fork of DotStarWall by Nathan Yonkee

Revision:
4:41434d1bd6cc
Parent:
0:eacf3b13ce5a
--- a/main.cpp	Fri Aug 11 18:41:21 2017 +0000
+++ b/main.cpp	Tue Aug 15 08:36:13 2017 -0600
@@ -1,156 +1,175 @@
-#include "mbed.h"
+#include "DotStar.h"
 #include "rtos.h"
 
-RawSerial  dev(PA_9, PA_10);
-SPI spiOne(PB_5, PB_4, PB_3);
-SPI spiTwo(PA_7, PA_6, PA_5);
-const int nLedsA = 455;
-const int nLedsB = 455;
-enum Init {brightness = 4, frequency = 500000, red = 35, green = 3, blue = 0, baud = 9600};
+enum Init {brightness = 4, i2cHz = 400000, baud = 9600};
+enum Register{ WHO_AM_I = 0x75, PWR_MGMT_1 = 0x6b, GYRO_CONFIG = 0x1b, CONFIG = 0x1a, GYRO_OFFSET = 0x13, GYRO = 0x43 };
 
-class DotStar {
-    SPI* const spi_;
-    const int ledNum_;
-    int brightness_;
-    int colors_[3];
-  public:
-    enum { red = 0, green = 1, blue = 2 };
-    enum { redIndex = 2, greenIndex = 3, blueIndex = 4 };
-    enum {off = 0, dim = 8, half = 16, brightest = 31};
-    enum {pause = 100, sleep = 2000};
-    enum cmdType : char {
-        setColor = '!',
-        onOff = 'o',
-        switchStrip = 's'
-    };
-    void set_color(const int color, const int val);
-    void set_leds();
-    DotStar(SPI* const spi, const int nLeds);
-    void set_brightness(const int brightness);
+Serial  dev(USBTX,USBRX);
+DigitalOut cts(PA_6, 0);
+SPI spiOne(PB_5, PB_4, PB_3);
+I2C accel(PA_10,PA_9);
+const int nLedsA = 65;
+const int accRead = 0x68 << 1;
+const int accWrite = accRead | 0x01;
+
+struct XYZAccel {
+    int16_t x,y,z;
 };
 
-struct RedGreenBlue {
-    int red, green, blue;
-};
-
-class DotStarPair {
-    DotStar* left;
-    DotStar* right;
-  public:
-    DotStarPair(DotStar* l, DotStar* r);
-    void set_brightness(const int brightness);
-    void set_rgb(const RedGreenBlue& rgb);
-};
-
-DotStarPair::DotStarPair(DotStar* l, DotStar* r) : left(l), right(r) {}
-
-void DotStarPair::set_brightness(const int brightness) {
-    left->set_brightness(brightness);
-    left->set_leds();
-    right->set_brightness(brightness);
-    right->set_leds();
-};
-
-void DotStarPair::set_rgb(const RedGreenBlue& rgb) {
-    left->set_color(DotStar::blue, rgb.blue);
-    left->set_color(DotStar::red, rgb.red);
-    left->set_color(DotStar::green, rgb.green);
-    left->set_leds();
-    right->set_color(DotStar::blue, rgb.blue);
-    right->set_color(DotStar::red, rgb.red);
-    right->set_color(DotStar::green, rgb.green);
-    right->set_leds();
+XYZAccel getXYZ() {
+    XYZAccel result;
+    char reg[1] = {Register::GYRO};
+    char data[6];
+    int ret = accel.write(accWrite,reg,1, true);
+    ret = accel.read(accRead,data,6);
+    result.x  = (((int16_t)data[0]) << 8) | data[1];
+    result.y  = (((int16_t)data[2]) << 8) | data[3];
+    result.z  = (((int16_t)data[4]) << 8) | data[5];
+    return result;
 };
 
-DotStar::DotStar(SPI* const spi, const int nLeds) : spi_(spi), ledNum_(nLeds),
-    brightness_(Init::brightness) {
-    spi_->frequency(Init::frequency);
-    colors_[DotStar::red] = Init::red;
-    colors_[DotStar::blue] = Init::blue;
-    colors_[DotStar::green] = Init::green;
-    set_leds();
-}
+int main() {
+    DotStar dotStarA(&spiOne, nLedsA);
+    Thread::wait(1000);
+    accel.frequency(Init::i2cHz);
+    dev.baud(Init::baud);
+    /* for (int i = 1; i < DotStar::brightest; i += 4) { */
+    /*     dotStarA.set_brightness(i); */
+    /* } */
+    dotStarA.set_brightness(DotStar::half);
+    int ret = 0;
+    char reg[1];
+    char data[7] = {};
+    reg[0] = Register::WHO_AM_I;
+    if(accel.write(accWrite,reg,1, true) != 0){
+        dev.printf("writing RA failed\n");
+    };
+    ret = accel.read(accRead,data,1);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    dev.printf("%i\n",data[0]);
 
+    reg[0] = PWR_MGMT_1;
+    ret = accel.write(accWrite,reg,1, true);
+    if (ret != 0) dev.printf("writing RA failed\n");
+    ret = accel.read(accRead,data,1);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    dev.printf("%i\n",data[0]);
+    data[0] &= ~(1<< 6);
+    dev.printf("%i\n",data[0]);
+    data[0] |= 1;
+    dev.printf("%i\n",data[0]);
+    data[1] = data[0];
+    data[0]= reg[0];
+    ret = accel.write(accWrite,data,2);
+    if (ret != 0) dev.printf("writing data failed\n");
 
-void DotStar::set_leds() {
-    const int brightnessFrame = (7<<5)|brightness_;
-    const int blueFrame = (colors_[DotStar::blue] ) & 0xff;
-    const int greenFrame = (colors_[DotStar::green]) & 0xff;
-    const int redFrame = colors_[DotStar::red] & 0xff;
-    int i;
-    Thread::wait(DotStar::pause);
-    for (i = 4; i --> 0; spi_->write(0)) { } // start frame
-    for (i = 0; i < ledNum_ ; ++i) {
-        spi_->write(brightnessFrame); // led frame
-        spi_->write(blueFrame); // B
-        spi_->write(greenFrame); // G
-        spi_->write(redFrame); // R
-    }
-    for (i = 4; i --> 0; spi_->write(1)) { } // end frame
-}
+    ret = accel.write(accWrite,reg,1, true);
+    if (ret != 0) dev.printf("writing RA failed\n");
+    ret = accel.read(accRead,data,1);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    dev.printf("%i\n",data[0]);
+
+    reg[0] = Register::GYRO_CONFIG;
+    ret = accel.write(accWrite,reg,1, true);
+    if (ret != 0) dev.printf("writing RA failed\n");
+    ret = accel.read(accRead,data,1);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    dev.printf("%i\n",data[0]);
+    data[0] |= 1<< 4;
+    data[0] |= 1<< 3;
+    data[0] &= ~1;
+    data[0] &= ~(1<<1);
+    dev.printf("%i\n",data[0]);
+    data[1] = data[0];
+    data[0]= reg[0];
+    ret = accel.write(accWrite,data,2);
+    if (ret != 0) dev.printf("writing data failed\n");
 
-void DotStar::set_brightness(const int brightness) {
-    brightness_ = brightness;
-}
-
-void DotStar::set_color(const int c, const int val) {
-    colors_[c] = val;
-};
+    reg[0] = Register::CONFIG;
+    ret = accel.write(accWrite,reg,1, true);
+    if (ret != 0) dev.printf("writing RA failed\n");
+    ret = accel.read(accRead,data,1);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    dev.printf("%i\n",data[0]);
+    data[0] &= ~(1<< 2);
+    data[0] &= ~1;
+    data[0] &= ~(1<< 1);
+    dev.printf("%i\n",data[0]);
+    data[1] = data[0];
+    data[0]= reg[0];
+    ret = accel.write(accWrite,data,2);
+    if (ret != 0) dev.printf("writing data failed\n");
 
-DotStar dotStarA(&spiOne, nLedsA);
-DotStar dotStarB(&spiTwo, nLedsB);
-
-DotStarPair strips(&dotStarA, &dotStarB);
-
-int main() {
-    dev.baud(Init::baud);
-    for (int i = 1; i < DotStar::brightest; i += 4) {
-        strips.set_brightness(i);
+    reg[0] = Register::GYRO_OFFSET;
+    ret = accel.write(accWrite,reg,1, true);
+    if (ret != 0) dev.printf("writing RA failed\n");
+    ret = accel.read(accRead,data,6);
+    if (ret != 0) dev.printf("reading RA failed\n");
+    for (int i = 6; i < 1; --i) {
+        data[i] = data[i-1];
     }
-    bool sleeping = false;
-#define STR_BUFF_SIZE_VAR 6
-    char bleCmdString[STR_BUFF_SIZE_VAR] = {};
-    for (; ; ) {
-        for (int i = 0; i < STR_BUFF_SIZE_VAR; i++) {
-            bleCmdString[i] = 0;
-        }
-        dev.putc('a');
-        strips.set_brightness(DotStar::half);
-        int i = 0;
-        int j = 0;
-        while (i < STR_BUFF_SIZE_VAR) {
-            while(dev.readable()) {
-                bleCmdString[i] = dev.getc();
-                ++i;
-            }
-            if(j++ > 30000000) break;
-        }
-        /* strips.set_brightness(DotStar::dim); */
-        auto cmdPrefix = bleCmdString[0];
-        if (cmdPrefix == DotStar::cmdType::setColor) {
-            RedGreenBlue rgb;
-            rgb.red = (int)(bleCmdString[DotStar::redIndex]);
-            rgb.green = (int)(bleCmdString[DotStar::greenIndex]);
-            rgb.blue = (int)(bleCmdString[DotStar::blueIndex]);
-            strips.set_rgb(rgb);
-            /* strips.set_brightness(DotStar::half); */
-            /* Thread::wait(DotStar::sleep); */
-        } else if (cmdPrefix == DotStar::cmdType::onOff) {
-            sleeping = true;
-            strips.set_brightness(DotStar::dim);
-            Thread::wait(DotStar::pause);
-        }
-        while (sleeping) {
-            strips.set_brightness(DotStar::off);
-            Thread::wait(DotStar::sleep);
-            while (dev.readable()) {
-                if (dev.getc() == DotStar::cmdType::onOff) {
-                    sleeping = false;
-                    strips.set_brightness(DotStar::dim);
-                    Thread::wait(DotStar::pause);
-                }
-            }
-        }
+    data[0] = reg[0];
+    data[1] = -1;
+    data[3] = -1;
+    data[5] = -1;
+    data[2] = -1;
+    data[4] = -8;
+    data[6] = -18;
+    ret = accel.write(accWrite,data,7);
+    if (ret != 0) dev.printf("writing data failed\n");
+    Thread::wait(500);
+
+    /* for(int i = 0; i < 5; ++i){ */
+    /* reg[0] = 0x43; */
+    /* ret = accel.write(accWrite,reg,1, true); */
+    /* if (ret != 0) dev.printf("writing RA failed\n"); */
+    /* ret = accel.read(accRead,data,6); */
+    /* if (ret != 0) dev.printf("reading RA failed\n"); */
+    /* int16_t xAcc = (((int16_t)data[0]) << 8) | (int16_t)(data[1]); */
+    /* int16_t yAcc = (((int16_t)data[2]) << 8) | (int16_t)(data[3]); */
+    /* int16_t zAcc = (((int16_t)data[4]) << 8) | (int16_t)(data[5]); */
+    /* dev.printf("x:%i  ",(xAcc)); */
+    /* dev.printf("y:%i  ",(yAcc)); */
+    /* dev.printf("z:%i\n",(zAcc)); */
+    /* Thread::wait(500); */
+    /* } */
+
+    /* Thread::wait(100); */
+    /* uint8_t dataInt = accel.read(0); */
+    /* accel.stop(); */
+    /* reg[0] = 0x75; */
+    /* reg[0] = 6; */
+    /* ret = accel.write(i2caddr,reg, 2); */
+    /* ret = accel.read(i2caddr,data,1); */
+    /* reg[0] = 0x1c; */
+    /* reg[1] = 0; */
+    /* ret = accel.write(i2caddr,reg,2); */
+    /* dotStarA.set_brightness(ret*10); */
+    /* reg[0] = 0x3b; */
+    /* ret = accel.write(i2caddr,reg,1); */
+    /* dotStarA.set_brightness(ret*10); */
+    /* ret = accel.read(i2caddr,data,6); */
+    /* dotStarA.set_brightness(ret*10); */
+    /* struct Dirs{float x, y, z;}; */
+    /* Dirs a; */
+    /* a.x = short( (((short)data[1]) << 8) | data[0] ); */
+    /* a.y = short( (((short)data[3]) << 8) | data[2] ); */
+    /* a.z = short( (((short)data[5]) << 8) | data[4] ); */
+    /* if(data[0] > -1 && data[0] < 4) dev.putc('y'); */
+    /* dev.printf("%f",(a.x)); */
+    /* dev.printf("%f",(a.y)); */
+    /* dev.printf("%f",(a.z)); */
+    dotStarA.set_brightness(DotStar::half);
+    for (;;) {
+        XYZAccel xyzData = getXYZ();
+        RedGreenBlue rgb;
+        rgb.red = abs(xyzData.x)>>6;
+        rgb.green = abs(xyzData.y)>>6;
+        rgb.blue = abs(xyzData.z)>>6;
+        if(rgb.red < rgb.green && rgb.red < rgb.blue) rgb.red = 0;
+        else if(rgb.blue < rgb.green && rgb.blue < rgb.red) rgb.blue = 0;
+        else rgb.green = 0;
+        dotStarA.set_rgb(rgb);
     }
 
 }