// Set the current state, one bit for each led // 0 = 0x0 = 0b000000000000000 = all leds off // 5 = 0x05 = 0b000000000000101 = leds 1 and 3 on, all others off // 341 = 0x155 = 0b000000101010101 = leds 1,3,5,7,9 on, 2,4,6,8,10 off // 1023 = 0x3ff = 0b000001111111111 = all leds on // | | // 10 1 void Grove_LED_Bar::setBits(unsigned int bits) { __state = bits & 0x3FF; sendData(CMDMODE); for (unsigned char i = 0; i < 10; i++) { if (__greenToRed) { // Bitwise AND the 10th bit (0x200) and left shift to cycle through all bits sendData((bits << i) & 0x200 ? ON : OFF); } else { // Bitwise AND the 1st bit (0x01) and right shift to cycle through all bits sendData((bits >> i) & 0x01 ? ON : OFF); } } // Two extra empty bits for padding the command to the correct length sendData(0x00); sendData(0x00); latchData(); }
int setLedNum(int n) { if(n<0 || n >10){ printf("n = [0, 10]\n"); return -1; } send16bitData(CmdMode); sendLED(pow(2,n)-1); // 2^n-1 latchData(); }
// set led single bit, red to green, one bit for each led // such as, index_bits = 0x05, then led 1 and led 3 will be on and the others will be off // 0x0 = 0b000000000000000 = all leds off // 0x05 = 0b000000000000101 = leds 1 and 3 on, the others off // 0x155 = 0b000000101010101 = leds 1,3,5,7,9 on, 2,4,6,8,10 off // 0x3ff = 0b000001111111111 = all leds on void led_bar_index_bit(unsigned int index_bits) { send16bitData(CMDMODE); for (int i=0;i<12;i++) { unsigned int state = (index_bits&0x0001) ? ON : SHUT; send16bitData(state); index_bits = index_bits>>1; } latchData(); }
// set level 0-10, red to green, where 1 is red // level 0 means all leds off while level 5 means leds 1-5 on and 6-10 will be off void led_bar_set_level(int level) { if(level>10)return; send16bitData(CMDMODE); for(int i=0;i<12;i++) { unsigned int state1 = (i<level) ? ON : SHUT; send16bitData(state1); } latchData(); }
void GroveLEDBar::setData(uint8_t bits[]) { sendData(GLB_CMDMODE); for (uint8_t i = 0; i < 10; i++) { if (__greenToRed) { // Go backward on __state sendData(__state[10 - i - 1]); } else { // Go forward on __state sendData(__state[i]); } } // Two extra empty bits for padding the command to the correct length sendData(0x00); sendData(0x00); latchData(); }
void MatrixDriver::updateLine() { unsigned char i, k, lineBits; // disable decoder while configuring the next line DEC_PORT &=~ _BV(DEC_E3); // clear the MY9221 before we send the data for the next line for (k=0;k<2;k++) { send16bitData(CmdMode); MY9221_PORT &=~ _BV(MY9221_DI); for(i=0;i<192;i++) { // toggle clock pin MY9221_PORT ^= _BV(MY9221_DCKI); } } latchData(); // now send the real data // first data segment for the 2nd MY9221 chip send16bitData(CmdMode); // BLUE segment send16bitData(0); // A3 --> BLUE8 send16bitData(255); // B3 --> BLUE7 send16bitData(0); // C3 --> BLUE6 send16bitData(255); // A2 --> BLUE5 send16bitData(0); // B2 --> BLUE4 send16bitData(255); // C2 --> BLUE3 send16bitData(0); // A1 --> BLUE2 send16bitData(255); // B1 --> BLUE1 // GREEN segment send16bitData(0); // C1 --> GREEN8 send16bitData(0); // A0 --> GREEN7 send16bitData(0); // B0 --> GREEN6 send16bitData(0); // C0 --> GREEN5 // second data segment for the 1nd MY9221 chip send16bitData(CmdMode); // GREEN segment send16bitData(0); // A3 --> GREEN4 send16bitData(0); // B3 --> GREEN3 send16bitData(0); // C3 --> GREEN2 send16bitData(0); // A2 --> GREEN1 // RED segment send16bitData(0); // B2 --> RED8 send16bitData(255); // C2 --> RED7 send16bitData(0); // A1 --> RED6 send16bitData(255); // B1 --> RED5 send16bitData(0); // C1 --> RED4 send16bitData(255); // A0 --> RED3 send16bitData(0); // B0 --> RED2 send16bitData(255); // C0 --> RED1 // data transfered and latch it latchData(); // change the line. we have 8 lines. m_currentLine uses therefor 3 bits // A0, A1, A2 used consecutive ports. we can use the linenumber to switch the ports lineBits = ((m_currentLine) << DEC_A0); // DEC_A0 is the first pin DEC_PORT &=~ lineBits; DEC_PORT |= lineBits; // enable the decoder DEC_PORT |= _BV(DEC_E3); // increment line number and enable decoder m_currentLine++; if (8 >= m_currentLine) { m_currentLine = 0; } }
void LEDBar::displayStack(uint16_t displayVal, bool direction) { mapped = map(displayVal, mapLow, mapHigh, 0, 10); if (direction == 0) { switch (mapped) { case 0: output = 0; break; case 1: output = 512; break; case 2: output = 768; break; case 3: output = 896; break; case 4: output = 960; break; case 5: output = 992; break; //green to red case 6: output = 1008; break; case 7: output = 1016; break; case 8: output = 1020; break; case 9: output = 1022; break; case 10: output = 1023; break; } } else { switch (mapped) { case 0: output = 0; break; case 1: output = 1; break; case 2: output = 3; break; case 3: output = 7; break; case 4: output = 15; break; case 5: output = 31; break; //red to green case 6: output = 63; break; case 7: output = 127; break; case 8: output = 255; break; case 9: output = 511; break; case 10: output = 1023; break; } } setCmdMode(); sendLED(output); latchData(); }
void LEDBar::displayCounter(uint16_t displayVal) { setCmdMode(); sendLED(displayVal); latchData(); }
void LEDBar::setSingleBar(uint8_t barnum) { setCmdMode(); sendLED(0x01 << barnum); latchData(); }