This is a project to replace the 8051 on a existing RGB LED board and add USB functionality. The original board relied on 8051 firmware to control the LEDs so any changes required software development. This version provides a USB interface for LED control. The USB interface is implemented as a HID with a 9 byte OutReport. In conjunction with a simple GUI the RGB LED board can now be controlled to set color patterns, intensity and pattern sequencing.
Dependencies: PCA9635-6 mbed USBDevice
main.cpp
00001 /************************************************************************************************ 00002 * 00003 * NXP MCU I2C RGB LED DEMO (6 x PCA9635) 00004 * Code ported from http://mbed.org/users/JimCarver/code/RGB2/ 00005 * Changes : Created a library for the LED DEMO board 00006 * LED control GUI : http://mbed.org/users/JimCarver/notebook/rgb-led-control-gui/ 00007 * 00008 ************************************************************************************************/ 00009 00010 #include "mbed.h" 00011 #include "USBHID.h" 00012 #include "PCA9635_6.h" 00013 00014 // We declare a USBHID device. By default input and output reports are 64 bytes long. 00015 // Note : HID connection on the KL25Z board is marked with 'USB' 00016 USBHID hid( 9, 1, 0x1Fc9, 0x0003, 0x0100, 1); // output_report_length, input_report_length, vendor_id, product_id, product_release 00017 00018 //Ledboard with 6 x PCA9635 - NXP MCU I2C RGB LED DEMO 00019 PCA9635_6 ledboard(PTE0, PTE1, PTD7); //SDA, SCL, EN 00020 00021 DigitalIn SW6(PTD0); // Pull low to run the test pattern at startup. 00022 DigitalIn SW5(PTD5); // Status of this pin is written to send_report.data[0] 00023 00024 //This report will contain data to be sent 00025 HID_REPORT send_report; 00026 HID_REPORT recv_report; 00027 00028 uint8_t dflag, tflag; 00029 00030 int dval; 00031 00032 typedef struct { 00033 uint8_t r, g, b; 00034 } RB_type; 00035 00036 RB_type Rainbow[] = { 00037 255, 0, 0, 00038 255, 23, 0, 00039 255, 64, 0, 00040 255, 127, 0, 00041 255, 193, 0, 00042 255, 255, 0, 00043 193, 255, 0, 00044 127, 255, 0, 00045 127, 255, 0, 00046 63, 255, 0, 00047 0, 255, 0, 00048 0, 255, 7, 00049 0, 255, 15, 00050 0, 255, 31, 00051 0, 255, 63, 00052 0, 255, 91, 00053 0, 255, 127, 00054 0, 255, 193, 00055 0, 255, 255, 00056 0, 193, 255, 00057 0, 127, 255, 00058 0, 63, 255, 00059 0, 0, 255, 00060 31, 0, 255, 00061 63, 0, 255, 00062 127, 0, 255, 00063 193, 0, 255, 00064 255, 0, 255, 00065 255, 0, 193, 00066 255, 0, 127, 00067 255, 0, 63, 00068 255, 0, 15 00069 }; 00070 00071 void rainbow_LED( void) 00072 { 00073 int i; 00074 for(i=0; i<32; i++) ledboard.set_LED( i, Rainbow[i].r, Rainbow[i].g, Rainbow[i].b ); 00075 } 00076 00077 void ripple_LED( void ) 00078 { 00079 uint32_t m; 00080 uint8_t LEDn, R, G, B, tR, tG, tB; 00081 if( dflag) { // Ripple LEDs to the right 00082 ledboard.read_LED( 0, &tR, &tG, &tB); // Save the first LED value 00083 for(LEDn = 0 , m = 1; LEDn < 31; LEDn++) { 00084 ledboard.read_LED( LEDn+1, &R, &G, &B); 00085 ledboard.set_LED( LEDn, R, G, B); 00086 m = m << 1; 00087 } 00088 ledboard.set_LED( 31, tR, tG, tB); 00089 } else { // Ripple LEDs to the left 00090 ledboard.read_LED( 31, &tR, &tG, &tB); 00091 for(LEDn = 31, m = 0x80000000 ; LEDn > 0; LEDn--) { 00092 ledboard.read_LED( LEDn - 1, &R, &G, &B); 00093 ledboard.set_LED( LEDn, R, G, B); 00094 m = m >> 1; 00095 } 00096 ledboard.set_LED( 0, tR, tG, tB); 00097 } 00098 } 00099 00100 00101 void test_pattern(void) 00102 { 00103 int l, r, g, b; 00104 // Dim Blue Color (from min to max) 00105 ledboard.Init_Buffers(); 00106 for (r = 0, g = 0, b = 0; b < 0xFF; b++) { 00107 for(l = 0; l < 32; l++) { 00108 ledboard.set_LED( l, r, g, b); 00109 } 00110 ledboard.update_LED(); 00111 } 00112 // Mix from Only Blue (Max going to off) to Only Green (off to Max) 00113 for (r = 0, g = 0, b = 0xFF; b >= 0x00; b--) { 00114 for(l = 0; l < 32; l++) { 00115 ledboard.set_LED( l, r, g, b); 00116 } 00117 ledboard.update_LED(); 00118 g++; 00119 } 00120 // Mix from Only Green (Max going to off) to Only Red (off to Max) 00121 for (r = 0, g = 0xFF, b = 0; g >= 0x00; g--) { 00122 for (l = 0; l < 32; l++) { 00123 ledboard.set_LED( l, r, g, b); 00124 } 00125 ledboard.update_LED(); 00126 r++; 00127 } 00128 // Mix from Only Red (Max going to off) to Only Blue (off to Max) 00129 for (r = 0xFF, g = 0, b = 0; r >= 0x00; r--) { 00130 for (l = 0; l < 32; l++) { 00131 ledboard.set_LED( l, r, g, b); 00132 } 00133 ledboard.update_LED(); 00134 b++; 00135 } 00136 rainbow_LED(); 00137 ledboard.update_LED(); 00138 } 00139 00140 void SetOutReport (void) 00141 { 00142 int LEDn; 00143 uint32_t led, m; 00144 uint8_t R, G, B, seq; 00145 00146 /* Check the bits of the "OutReport" data from the PC 00147 * and set the output port status. */ 00148 seq = recv_report.data[0]; 00149 //LED_Report = (LED_Report_type *) &recv_report.data[1]; 00150 ledboard.set_global_intensity(recv_report.data[8]); 00151 if(seq) { 00152 tflag = 2; 00153 dflag = seq & 1; 00154 dval = (seq & 0xFC) >> 1; // sets a range from 0x00 to 0x7E 00155 } else { 00156 led = recv_report.data[4]; 00157 led <<= 8; 00158 led |= recv_report.data[3]; 00159 led <<= 8; 00160 led |= recv_report.data[2]; 00161 led <<= 8; 00162 led |= recv_report.data[1]; 00163 00164 R = recv_report.data[5]; 00165 G = recv_report.data[6]; 00166 B = recv_report.data[7]; 00167 for(LEDn=0, m=1; LEDn < 32; LEDn++, m <<= 1) { 00168 if(led & m) { 00169 ledboard.set_LED( LEDn, R, G, B); 00170 } 00171 } 00172 tflag = 1; 00173 } 00174 } 00175 00176 /*********************************************************************** 00177 DESCRIPTION: Main function 00178 INPUT(S): None 00179 RETURNS: Nothing 00180 ************************************************************************/ 00181 int main (void) 00182 { 00183 if(!SW6) test_pattern(); 00184 dflag = 0; 00185 dval = 10; 00186 tflag = 0; 00187 send_report.length = 1; 00188 while (1) { 00189 send_report.data[0] = SW5; 00190 hid.send(&send_report); 00191 if(hid.readNB(&recv_report)) SetOutReport(); 00192 if(tflag == 2) { 00193 ripple_LED(); 00194 ledboard.update_LED(); 00195 ledboard.LED_INTENSITY(); 00196 wait_ms(dval); 00197 } 00198 00199 if(tflag == 1) { 00200 ledboard.update_LED(); 00201 ledboard.LED_INTENSITY(); 00202 tflag = 0; 00203 } 00204 } 00205 }
Generated on Wed Aug 10 2022 11:18:12 by 1.7.2