projectwerk
Dependencies: NeoPixelString SimplyLog
Fork of NeoPixelI2cSlave by
neopixel_i2c_daemon.cpp@3:02931c2b1e82, 2016-01-09 (annotated)
- Committer:
- BramHeldenbergh
- Date:
- Sat Jan 09 13:10:19 2016 +0000
- Revision:
- 3:02931c2b1e82
- Parent:
- 2:e0269262d1f2
programma projectwerk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dwini | 0:3a31c84ed525 | 1 | #include "neopixel_i2c_daemon.h" |
BramHeldenbergh | 3:02931c2b1e82 | 2 | #include "strobe.h" |
dwini | 0:3a31c84ed525 | 3 | #define DEBUG_MODE 1 |
dwini | 0:3a31c84ed525 | 4 | #include "log.h" |
dwini | 2:e0269262d1f2 | 5 | NeoPixelI2cDaemon::NeoPixelI2cDaemon(I2cDevice * i2c, PinName alive_pin) : alive(alive_pin) { |
dwini | 0:3a31c84ed525 | 6 | this->i2c = i2c; |
dwini | 2:e0269262d1f2 | 7 | cAlive = 0; |
dwini | 0:3a31c84ed525 | 8 | } |
dwini | 0:3a31c84ed525 | 9 | void NeoPixelI2cDaemon::attachPixelString(NeoPixelString * pixelstring) { |
dwini | 0:3a31c84ed525 | 10 | pixelstrings.push_back(pixelstring); |
dwini | 0:3a31c84ed525 | 11 | } |
dwini | 0:3a31c84ed525 | 12 | void NeoPixelI2cDaemon::allOff(void) { |
dwini | 0:3a31c84ed525 | 13 | for (unsigned int i = 0; i < pixelstrings.size(); i++) { |
dwini | 0:3a31c84ed525 | 14 | pixelstrings[i]->update(Colors::Black); |
dwini | 0:3a31c84ed525 | 15 | } |
dwini | 0:3a31c84ed525 | 16 | } |
dwini | 0:3a31c84ed525 | 17 | void NeoPixelI2cDaemon::allSingleColor(neopixel::Pixel singlecolor) { |
dwini | 0:3a31c84ed525 | 18 | for (unsigned int i = 0; i < pixelstrings.size(); i++) { |
dwini | 0:3a31c84ed525 | 19 | pixelstrings[i]->update(singlecolor); |
dwini | 0:3a31c84ed525 | 20 | } |
dwini | 0:3a31c84ed525 | 21 | } |
dwini | 0:3a31c84ed525 | 22 | void NeoPixelI2cDaemon::diagnoseAll(void) { |
dwini | 0:3a31c84ed525 | 23 | for (unsigned int i = 0; i < pixelstrings.size(); i++) { |
dwini | 0:3a31c84ed525 | 24 | pixelstrings[i]->diagnose(); |
dwini | 0:3a31c84ed525 | 25 | } |
dwini | 0:3a31c84ed525 | 26 | } |
dwini | 0:3a31c84ed525 | 27 | void NeoPixelI2cDaemon::listen(bool blocking=false) { |
dwini | 0:3a31c84ed525 | 28 | char buffer[10]; |
dwini | 0:3a31c84ed525 | 29 | while (blocking) { |
dwini | 0:3a31c84ed525 | 30 | // Check for i2c message |
dwini | 0:3a31c84ed525 | 31 | int i = i2c->receive(); |
dwini | 0:3a31c84ed525 | 32 | // NoData - the slave has not been addressed |
dwini | 0:3a31c84ed525 | 33 | // ReadAddressed - the master has requested a read from this slave |
dwini | 0:3a31c84ed525 | 34 | // WriteAddressed - the master is writing to this slave |
dwini | 0:3a31c84ed525 | 35 | // WriteGeneral - the master is writing to all slave |
dwini | 0:3a31c84ed525 | 36 | switch (i) { |
dwini | 0:3a31c84ed525 | 37 | case I2cDevice::ReadAddressed: |
dwini | 0:3a31c84ed525 | 38 | char sendbuffer[1]; |
dwini | 0:3a31c84ed525 | 39 | sendbuffer[0] = pixelstrings.size(); |
dwini | 0:3a31c84ed525 | 40 | int result = i2c->write(sendbuffer, sizeof(sendbuffer)); |
dwini | 0:3a31c84ed525 | 41 | i2c->stop(); |
dwini | 0:3a31c84ed525 | 42 | if (!result) { // 0 on success, non-0 otherwise |
dwini | 0:3a31c84ed525 | 43 | SimplyLog::Log::i("Send info to master successfully\r\n"); |
dwini | 0:3a31c84ed525 | 44 | } else { |
dwini | 0:3a31c84ed525 | 45 | SimplyLog::Log::w("Send info to master failed\r\n"); |
dwini | 0:3a31c84ed525 | 46 | } |
dwini | 0:3a31c84ed525 | 47 | break; |
dwini | 0:3a31c84ed525 | 48 | case I2cDevice::WriteAddressed: |
dwini | 0:3a31c84ed525 | 49 | { |
dwini | 0:3a31c84ed525 | 50 | // First we read the command byte |
dwini | 0:3a31c84ed525 | 51 | int command = i2c->read(); |
dwini | 0:3a31c84ed525 | 52 | |
dwini | 0:3a31c84ed525 | 53 | // Check the command |
dwini | 0:3a31c84ed525 | 54 | switch (command) |
dwini | 0:3a31c84ed525 | 55 | { |
dwini | 0:3a31c84ed525 | 56 | case OFF: |
dwini | 0:3a31c84ed525 | 57 | i2c->stop(); // Necessary !! Otherwise write will fail on master end |
dwini | 0:3a31c84ed525 | 58 | SimplyLog::Log::i("Killing all neopixel leds\r\n"); |
dwini | 0:3a31c84ed525 | 59 | allOff(); |
BramHeldenbergh | 3:02931c2b1e82 | 60 | |
dwini | 0:3a31c84ed525 | 61 | break; |
dwini | 0:3a31c84ed525 | 62 | case DIAGNOSTIC: |
dwini | 0:3a31c84ed525 | 63 | i2c->stop(); // Necessary !! Otherwise write will fail on master end |
dwini | 0:3a31c84ed525 | 64 | SimplyLog::Log::i("Executing diagnostic of neopixel strings\r\n"); |
dwini | 0:3a31c84ed525 | 65 | diagnoseAll(); |
dwini | 0:3a31c84ed525 | 66 | SimplyLog::Log::i("Diagnostic finished\r\n"); |
BramHeldenbergh | 3:02931c2b1e82 | 67 | |
dwini | 0:3a31c84ed525 | 68 | break; |
dwini | 0:3a31c84ed525 | 69 | case SINGLE_COLOR: |
dwini | 0:3a31c84ed525 | 70 | // Expect 3 more bytes [red green blue] |
dwini | 0:3a31c84ed525 | 71 | if(!i2c->read(buffer, 3)) { //0 on success, non-0 otherwise |
dwini | 0:3a31c84ed525 | 72 | neopixel::Pixel color; |
dwini | 0:3a31c84ed525 | 73 | color.red = buffer[0]; |
dwini | 0:3a31c84ed525 | 74 | color.green = buffer[1]; |
dwini | 0:3a31c84ed525 | 75 | color.blue = buffer[2]; |
dwini | 0:3a31c84ed525 | 76 | allSingleColor(color); |
dwini | 0:3a31c84ed525 | 77 | SimplyLog::Log::i("Setting single color [r,g,b] = [%d,%d,%d]\r\n", color.red, color.green, color.blue); |
dwini | 0:3a31c84ed525 | 78 | } else { |
dwini | 0:3a31c84ed525 | 79 | SimplyLog::Log::w("Single color set received with missing params\r\n"); |
dwini | 0:3a31c84ed525 | 80 | } |
BramHeldenbergh | 3:02931c2b1e82 | 81 | |
BramHeldenbergh | 3:02931c2b1e82 | 82 | break; |
BramHeldenbergh | 3:02931c2b1e82 | 83 | |
BramHeldenbergh | 3:02931c2b1e82 | 84 | |
BramHeldenbergh | 3:02931c2b1e82 | 85 | case STROBO: |
BramHeldenbergh | 3:02931c2b1e82 | 86 | i2c->stop(); |
BramHeldenbergh | 3:02931c2b1e82 | 87 | SimplyLog::Log::w("We gaan een ticker aanroepen\r\n"); |
BramHeldenbergh | 3:02931c2b1e82 | 88 | hanzi= new Strobe(this); |
BramHeldenbergh | 3:02931c2b1e82 | 89 | hanzi->strstart(); |
BramHeldenbergh | 3:02931c2b1e82 | 90 | SimplyLog::Log::w("Single color set received with missing params\r\n"); |
dwini | 0:3a31c84ed525 | 91 | break; |
dwini | 0:3a31c84ed525 | 92 | |
dwini | 0:3a31c84ed525 | 93 | default: |
dwini | 0:3a31c84ed525 | 94 | i2c->stop(); // Necessary !! Otherwise write will fail on master end |
dwini | 0:3a31c84ed525 | 95 | SimplyLog::Log::w("Unknown command byte\r\n"); |
dwini | 0:3a31c84ed525 | 96 | } |
dwini | 0:3a31c84ed525 | 97 | } |
dwini | 0:3a31c84ed525 | 98 | } |
dwini | 0:3a31c84ed525 | 99 | for(int i = 0; i < 10; i++) { |
dwini | 0:3a31c84ed525 | 100 | buffer[i] = 0; // Clear buffer |
dwini | 0:3a31c84ed525 | 101 | } |
dwini | 2:e0269262d1f2 | 102 | |
dwini | 2:e0269262d1f2 | 103 | // Alive LED |
dwini | 2:e0269262d1f2 | 104 | cAlive = (cAlive + 1) % 100000; |
dwini | 2:e0269262d1f2 | 105 | if (!cAlive) { |
dwini | 2:e0269262d1f2 | 106 | alive = !alive; |
dwini | 2:e0269262d1f2 | 107 | } |
BramHeldenbergh | 3:02931c2b1e82 | 108 | |
BramHeldenbergh | 3:02931c2b1e82 | 109 | hanzi->execute(); |
dwini | 0:3a31c84ed525 | 110 | } |
dwini | 0:3a31c84ed525 | 111 | } |