├── CorsairKeyboardRainbow.cbp ├── CorsairKeyboardRainbow.depend ├── CorsairKeyboardRainbow.layout ├── CorsairKeyboardVisualizer.cpp ├── FanBusAudio.depend ├── FanBusAudio.layout ├── README.md ├── RzChromaSDKDefines.h ├── RzChromaSDKTypes.h ├── RzErrors.h ├── chuck_fft.c ├── chuck_fft.h ├── fanbus.cpp ├── fanbus.h ├── serial_port.cpp └── serial_port.h /CorsairKeyboardRainbow.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 75 | 76 | -------------------------------------------------------------------------------- /CorsairKeyboardRainbow.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1345315300 source:z:\programming\corsairkeyboardvisualizer\chuck_fft.c 3 | "chuck_fft.h" 4 | 5 | 6 | 7 | 1345315311 z:\programming\corsairkeyboardvisualizer\chuck_fft.h 8 | 9 | 1358835704 source:z:\programming\corsairkeyboardvisualizer\fanbus.cpp 10 | "fanbus.h" 11 | 12 | 1344729837 z:\programming\corsairkeyboardvisualizer\fanbus.h 13 | "serial_port.h" 14 | 15 | 1358836569 z:\programming\corsairkeyboardvisualizer\serial_port.h 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 1412990859 source:z:\programming\corsairkeyboardvisualizer\serial_port.cpp 26 | "serial_port.h" 27 | 28 | 1413672817 source:z:\programming\corsairkeyboardvisualizer\corsairkeyboardvisualizer.cpp 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | "chuck_fft.h" 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | "serial_port.h" 46 | "fanbus.h" 47 | 48 | 1413331484 c:\libraries\libusb-win32-bin-1.2.6.0\include\usb.h 49 | "lusb0_usb.h" 50 | 51 | 1413331367 c:\libraries\libusb-win32-bin-1.2.6.0\include\lusb0_usb.h 52 | 53 | 54 | 55 | 56 | 57 | 1345335872 c:\libraries\openal-soft-1.14-bin\include\al\al.h 58 | 59 | 1345335872 c:\libraries\openal-soft-1.14-bin\include\al\alc.h 60 | 61 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl.h 62 | "SDL_main.h" 63 | "SDL_stdinc.h" 64 | "SDL_audio.h" 65 | "SDL_cdrom.h" 66 | "SDL_cpuinfo.h" 67 | "SDL_endian.h" 68 | "SDL_error.h" 69 | "SDL_events.h" 70 | "SDL_loadso.h" 71 | "SDL_mutex.h" 72 | "SDL_rwops.h" 73 | "SDL_thread.h" 74 | "SDL_timer.h" 75 | "SDL_video.h" 76 | "SDL_version.h" 77 | "begin_code.h" 78 | "close_code.h" 79 | 80 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_main.h 81 | "SDL_stdinc.h" 82 | "begin_code.h" 83 | "close_code.h" 84 | "begin_code.h" 85 | "close_code.h" 86 | 87 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_stdinc.h 88 | "SDL_config.h" 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | "begin_code.h" 106 | 107 | 108 | 109 | 110 | 111 | "close_code.h" 112 | 113 | 1326114587 c:\libraries\sdl-1.2.15\include\sdl\sdl_config.h 114 | "SDL_platform.h" 115 | "SDL_config_dreamcast.h" 116 | "SDL_config_macos.h" 117 | "SDL_config_macosx.h" 118 | "SDL_config_symbian.h" 119 | "SDL_config_win32.h" 120 | "SDL_config_os2.h" 121 | "SDL_config_minimal.h" 122 | 123 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_platform.h 124 | 125 | 1326114594 c:\libraries\sdl-1.2.15\include\sdl\sdl_config_win32.h 126 | "SDL_platform.h" 127 | 128 | 1326114600 c:\libraries\sdl-1.2.15\include\sdl\begin_code.h 129 | 130 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\close_code.h 131 | 132 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_audio.h 133 | "SDL_stdinc.h" 134 | "SDL_error.h" 135 | "SDL_endian.h" 136 | "SDL_mutex.h" 137 | "SDL_thread.h" 138 | "SDL_rwops.h" 139 | "begin_code.h" 140 | "close_code.h" 141 | 142 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_error.h 143 | "SDL_stdinc.h" 144 | "begin_code.h" 145 | "close_code.h" 146 | 147 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_endian.h 148 | "SDL_stdinc.h" 149 | 150 | "begin_code.h" 151 | "close_code.h" 152 | 153 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_mutex.h 154 | "SDL_stdinc.h" 155 | "SDL_error.h" 156 | "begin_code.h" 157 | "close_code.h" 158 | 159 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_thread.h 160 | "SDL_stdinc.h" 161 | "SDL_error.h" 162 | "SDL_mutex.h" 163 | "begin_code.h" 164 | 165 | "close_code.h" 166 | 167 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_rwops.h 168 | "SDL_stdinc.h" 169 | "SDL_error.h" 170 | "begin_code.h" 171 | "close_code.h" 172 | 173 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_cdrom.h 174 | "SDL_stdinc.h" 175 | "SDL_error.h" 176 | "begin_code.h" 177 | "close_code.h" 178 | 179 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_cpuinfo.h 180 | "SDL_stdinc.h" 181 | "begin_code.h" 182 | "close_code.h" 183 | 184 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_events.h 185 | "SDL_stdinc.h" 186 | "SDL_error.h" 187 | "SDL_active.h" 188 | "SDL_keyboard.h" 189 | "SDL_mouse.h" 190 | "SDL_joystick.h" 191 | "SDL_quit.h" 192 | "begin_code.h" 193 | "close_code.h" 194 | 195 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_active.h 196 | "SDL_stdinc.h" 197 | "SDL_error.h" 198 | "begin_code.h" 199 | "close_code.h" 200 | 201 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_keyboard.h 202 | "SDL_stdinc.h" 203 | "SDL_error.h" 204 | "SDL_keysym.h" 205 | "begin_code.h" 206 | "close_code.h" 207 | 208 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_keysym.h 209 | 210 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_mouse.h 211 | "SDL_stdinc.h" 212 | "SDL_error.h" 213 | "SDL_video.h" 214 | "begin_code.h" 215 | "close_code.h" 216 | 217 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_video.h 218 | "SDL_stdinc.h" 219 | "SDL_error.h" 220 | "SDL_rwops.h" 221 | "begin_code.h" 222 | "close_code.h" 223 | 224 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_joystick.h 225 | "SDL_stdinc.h" 226 | "SDL_error.h" 227 | "begin_code.h" 228 | "close_code.h" 229 | 230 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_quit.h 231 | "SDL_stdinc.h" 232 | "SDL_error.h" 233 | 234 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_loadso.h 235 | "SDL_stdinc.h" 236 | "SDL_error.h" 237 | "begin_code.h" 238 | "close_code.h" 239 | 240 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_timer.h 241 | "SDL_stdinc.h" 242 | "SDL_error.h" 243 | "begin_code.h" 244 | "close_code.h" 245 | 246 | 1325742239 c:\libraries\sdl-1.2.15\include\sdl\sdl_version.h 247 | "SDL_stdinc.h" 248 | "begin_code.h" 249 | "close_code.h" 250 | 251 | 1413331537 c:\libraries\sdl_gfx-2.0.24\include\sdl\sdl_gfxprimitives.h 252 | 253 | "SDL/SDL.h" 254 | 255 | 1345315300 source:z:\programming\razerkeyboardvisualizer\chuck_fft.c 256 | "chuck_fft.h" 257 | 258 | 259 | 260 | 1345315311 z:\programming\razerkeyboardvisualizer\chuck_fft.h 261 | 262 | 1358835704 source:z:\programming\razerkeyboardvisualizer\fanbus.cpp 263 | "fanbus.h" 264 | 265 | 1344729837 z:\programming\razerkeyboardvisualizer\fanbus.h 266 | "serial_port.h" 267 | 268 | 1358836569 z:\programming\razerkeyboardvisualizer\serial_port.h 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 1412990859 source:z:\programming\razerkeyboardvisualizer\serial_port.cpp 279 | "serial_port.h" 280 | 281 | -------------------------------------------------------------------------------- /CorsairKeyboardRainbow.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /CorsairKeyboardVisualizer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "chuck_fft.h" 8 | 9 | #ifdef WIN32 10 | #define USE_WINDOWS_USB 11 | #ifdef USE_WINDOWS_USB 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #endif // USE_WINDOWS_USB 23 | #endif 24 | 25 | #define FALSE 0 26 | 27 | #define ENABLE_SDL_DISPLAY 28 | #define ENABLE_CORSAIR_RGB 29 | //#define ENABLE_FANBUS 30 | #define ENABLE_RAZER_SDK 31 | 32 | #ifdef ENABLE_SDL_DISPLAY 33 | #include 34 | #include 35 | #endif // ENABLE_SDL_DISPLAY 36 | 37 | #ifdef ENABLE_RAZER_SDK 38 | #include "RzChromaSDKDefines.h" 39 | #include "RzChromaSDKTypes.h" 40 | #include "RzErrors.h" 41 | 42 | using namespace ChromaSDK::Keyboard; 43 | using namespace ChromaSDK::Mouse; 44 | 45 | typedef RZRESULT (*INIT)(void); 46 | typedef RZRESULT (*UNINIT)(void); 47 | typedef RZRESULT (*CREATEKEYBOARDCUSTOMGRIDEFFECTS)(ChromaSDK::Keyboard::CUSTOM_GRID_EFFECT_TYPE CustomEffects, RZEFFECTID *pEffectId); 48 | typedef RZRESULT (*CREATEMOUSECUSTOMEFFECTS)(ChromaSDK::Mouse::CUSTOM_EFFECT_TYPE CustomEffect, RZEFFECTID *pEffectId); 49 | typedef RZRESULT (*DELETEEFFECT)(RZEFFECTID EffectId); 50 | typedef RZRESULT (*SETEFFECT)(RZEFFECTID EffectId); 51 | 52 | #ifdef _WIN64 53 | #define CHROMASDKDLL _T("RzChromaSDK64.dll") 54 | #else 55 | #define CHROMASDKDLL _T("RzChromaSDK.dll") 56 | #endif 57 | 58 | HMODULE hModule = NULL; // Chroma SDK module handle. 59 | #endif // ENABLE_RAZER_SDK 60 | 61 | #ifdef ENABLE_FANBUS 62 | #include "serial_port.h" 63 | #include "fanbus.h" 64 | serial_port ports[2]; 65 | fanbus bus1; 66 | 67 | static int red_leds[] = { 0x10, 0x13, 0x16, 0x19 }; 68 | static int grn_leds[] = { 0x11, 0x14, 0x17, 0x1A }; 69 | static int blu_leds[] = { 0x12, 0x15, 0x18, 0x1B }; 70 | #endif // ENABLE_FANBUS 71 | 72 | #ifdef ENABLE_CORSAIR_RGB 73 | 74 | #ifdef USE_WINDOWS_USB 75 | HANDLE dev; 76 | #else 77 | static struct usb_device *dev; 78 | struct usb_dev_handle *handle; 79 | #endif 80 | 81 | static int init_keyboard(); 82 | 83 | static void update_keyboard(); 84 | 85 | static void set_led( int x, int y, int r, int g, int b ); 86 | 87 | #ifdef USE_WINDOWS_USB 88 | HANDLE GetDeviceHandle(unsigned int uiVID, unsigned int uiPID, unsigned int uiMI); 89 | #else 90 | static struct usb_device *find_device(uint16_t vendor, uint16_t product); 91 | #endif 92 | 93 | char red_val[144]; 94 | char grn_val[144]; 95 | char blu_val[144]; 96 | 97 | 98 | char data_pkt[5][64] = { 0 }; 99 | 100 | unsigned char position_map[] = { 101 | 137, 8, 20,255, 102 | 0, 12, 24, 36, 48, 60, 72, 84, 96,108,120,132, 6, 18, 30, 42, 32, 44, 56, 68,255, 103 | 1, 13, 25, 37, 49, 61, 73, 85, 97,109,121,133, 7, 31, 54, 66, 78, 80, 92,104,116,255, 104 | 2, 14, 26, 38, 50, 62, 74, 86, 98,110,122,134, 90,102, 43, 55, 67, 9, 21, 33,128,255, 105 | 3, 15, 27, 39, 51, 63, 75, 87, 99,111,123,135,126, 57, 69, 81,128,255, 106 | 4, 28, 40, 52, 64, 76, 88,100,112,124,136, 79,103, 93,105,117,140,255, 107 | 5, 17, 29, 53, 89,101,113, 91,115,127,139,129,141,140,255, 108 | }; 109 | 110 | float size_map[] = { 111 | -15.5, 1, 1, -2.5, 1, -2, 0, 112 | 1, -.5, 1, 1, 1, 1, -.75, 1, 1, 1, 1, -.75, 1, 1, 1, 1, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0, 113 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0, 114 | 1.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.5, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0, 115 | 1.75, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.25, -4, 1, 1, 1, 1, 0, 116 | 2.25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.75, -1.5, 1, -1.5, 1, 1, 1, 1, 0, 117 | 1.5, 1, 1.25, 6.5, 1.25, 1, 1, 1.5, -.5, 1, 1, 1, -.5, 2, 1, 1, 0, 118 | }; 119 | 120 | unsigned char led_matrix[7][92]; 121 | 122 | unsigned char led_waveform[7][92]; 123 | #endif // ENABLE_CORSAIR_RGB 124 | 125 | float normalizeFFT(float fftin) 126 | { 127 | if(fftin > 0) 128 | { 129 | return 20.0f*log10(fftin); 130 | } 131 | else 132 | { 133 | return 0; 134 | } 135 | } 136 | 137 | int main(int argc, char *argv[]) 138 | { 139 | float amplitude = 10.0f; 140 | float red, grn, blu; 141 | unsigned char buffer[512]; 142 | float fft[512]; 143 | unsigned char charfft[512]; 144 | float win[512]; 145 | float fftavg; 146 | int level; 147 | 148 | #ifdef ENABLE_SDL_DISPLAY 149 | SDL_Surface* wavs = NULL; 150 | SDL_Surface* screen = NULL; 151 | SDL_Event event; 152 | 153 | SDL_Init( SDL_INIT_EVERYTHING ); 154 | screen = SDL_SetVideoMode( 512, 256, 32, SDL_HWSURFACE ); 155 | wavs = SDL_SetVideoMode( 512, 256, 32, SDL_HWSURFACE ); 156 | SDL_WM_SetCaption("Audio Visualizer", NULL); 157 | #endif // ENABLE_SDL_DISPLAY 158 | 159 | ALCdevice *device = alcCaptureOpenDevice(NULL, 16000, AL_FORMAT_MONO8, 256); 160 | alcCaptureStart(device); 161 | 162 | blackman(win, 512); 163 | 164 | #ifdef ENABLE_FANBUS 165 | #ifdef WIN32 166 | if( ports[0].serial_open("COM2", 38400) == FALSE ) 167 | #else 168 | if( ports[0].serial_open("/dev/ttyS4", 38400) == FALSE ) 169 | #endif 170 | { 171 | return 0; 172 | } 173 | 174 | bus1.fanbus_set_port(&ports[0]); 175 | #endif 176 | 177 | #ifdef ENABLE_CORSAIR_RGB 178 | init_keyboard(); 179 | #endif // ENABLE_CORSAIR_RGB 180 | 181 | #ifdef ENABLE_RAZER_SDK 182 | // Dynamically loads the Chroma SDK library. 183 | hModule = LoadLibrary(CHROMASDKDLL); 184 | if(hModule) 185 | { 186 | INIT Init = (INIT)GetProcAddress(hModule, "Init"); 187 | if(Init) 188 | { 189 | Init(); 190 | } 191 | } 192 | CREATEKEYBOARDCUSTOMGRIDEFFECTS CreateKeyboardCustomGridEffects = (CREATEKEYBOARDCUSTOMGRIDEFFECTS)GetProcAddress(hModule, "CreateKeyboardCustomGridEffects"); 193 | if(CreateKeyboardCustomGridEffects == NULL) 194 | { 195 | return 0; 196 | } 197 | 198 | CUSTOM_GRID_EFFECT_TYPE Grid = {}; 199 | #endif // ENABLE_RAZER_SDK 200 | 201 | while(1) 202 | { 203 | for(int j = 0; j < 100; j++) 204 | { 205 | for(int i = 0; i < 512; i++) 206 | { 207 | buffer[i] = 0; 208 | charfft[i] = 0; 209 | fft[i] = 0; 210 | } 211 | 212 | alcCaptureSamples(device, (ALCvoid *)buffer, 512); 213 | level = 0; 214 | for(int i = 0; i < 512; i++) 215 | { 216 | level += abs((int)buffer[i]-128); 217 | fft[i] = (buffer[i]-128)*amplitude; 218 | } 219 | 220 | rfft(fft, 512, 1); 221 | apply_window(fft, win, 512); 222 | 223 | for(int i = 0; i < 512; i+=2) 224 | { 225 | float fftmag = 80*(log10(sqrt((fft[i]*fft[i])+(fft[i+1]*fft[i+1])))); 226 | if( fftmag < 0 ) 227 | { 228 | fftmag = 0; 229 | } 230 | fftavg += fftmag; 231 | charfft[i] = (unsigned char)fftmag; 232 | charfft[i+1] = charfft[i]; 233 | } 234 | fftavg /= 10; 235 | 236 | #ifdef ENABLE_SDL_DISPLAY 237 | boxRGBA(wavs, 0, 0, 512, 255, 0, 0, 0, 255); 238 | for(int i = 0; i < 512; i++) 239 | { 240 | lineRGBA(wavs, i, 512, i, 255-charfft[i], 0, 255, 0, 255); 241 | pixelRGBA(wavs, i, 255- (unsigned char)buffer[i], 255, 0, 0, 255); 242 | } 243 | SDL_BlitSurface(wavs, NULL, screen, NULL); 244 | SDL_Flip(screen); 245 | SDL_PollEvent(&event); 246 | if(event.type == SDL_QUIT) 247 | { 248 | return 0; 249 | } 250 | #endif // ENABLE_SDL_DISPLAY 251 | 252 | #ifdef ENABLE_FANBUS 253 | for(int i = 0; i < 4; i++) 254 | { 255 | red = 64 * ( sin( ( ( ( j + 23 * i ) / 92.0f ) *6.28 ) ) + 1 ); 256 | grn = 64 * ( sin( ( ( ( j + 23 * i ) / 92.0f ) *6.28 ) + ( 6.28 / 3 ) ) + 1 ); 257 | blu = 64 * ( sin( ( ( ( j + 23 * i ) / 92.0f ) *6.28 ) - ( 6.28 / 3 ) ) + 1 ); 258 | 259 | bus1.fanbus_write(red_leds[i], 0x02, red); 260 | bus1.fanbus_write(grn_leds[i], 0x02, grn); 261 | bus1.fanbus_write(blu_leds[i], 0x02, blu); 262 | 263 | bus1.fanbus_write(0x0C, 0x02, 0x01); 264 | } 265 | #endif 266 | 267 | for(int x = 0; x < 100; x++) 268 | { 269 | for(int y = 0; y < 7; y++) 270 | { 271 | red = ( sin( ( ( ( ( x - j ) % 100 ) / 100.0f ) * 2 * 3.14f ) ) + 1 ); 272 | grn = ( sin( ( ( ( ( x - j ) % 100 ) / 100.0f ) * 2 * 3.14f ) - ( 6.28f / 3 ) ) + 1 ); 273 | blu = ( sin( ( ( ( ( x - j ) % 100 ) / 100.0f ) * 2 * 3.14f ) + ( 6.28f / 3 ) ) + 1 ); 274 | #ifdef ENABLE_RAZER_SDK 275 | int razer_x = (x * (22/100.0f)); 276 | int razer_j = (j * (22/100.0f)); 277 | 278 | Grid.Key[y][((razer_x ) % 22)] = RGB(16*red, 16*grn, 16*blu); 279 | 280 | if( charfft[(int)(x * (255.0f/100))] > (255/(y + 1))) 281 | { 282 | Grid.Key[y][razer_x] = RGB(255, 255, 255); 283 | } 284 | #endif // ENABLE_RAZER_SDK 285 | 286 | #ifdef ENABLE_CORSAIR_RGB 287 | int corsair_x = (x * (92/100.0f)); 288 | int corsair_j = (j * (92/100.0f)); 289 | 290 | set_led( ( corsair_x ) % 92, y, 2*red, 2*grn, 2*blu ); 291 | 292 | if( charfft[(int)(x * (255.0f/100))] > (255/(y + 1))) 293 | { 294 | set_led( corsair_x, y, 0x07, 0x07, 0x07); 295 | } 296 | #endif // ENABLE_CORSAIR_RGB 297 | } 298 | } 299 | 300 | #ifdef ENABLE_RAZER_SDK 301 | CreateKeyboardCustomGridEffects(Grid, NULL); 302 | #endif // ENABLE_RAZER_SDK 303 | 304 | #ifdef ENABLE_CORSAIR_RGB 305 | update_keyboard(); 306 | #endif // ENABLE_CORSAIR_RGB 307 | 308 | #ifdef ENABLE_SDL_DISPLAY 309 | SDL_Delay(25); 310 | #elif defined( WIN32 ) 311 | Sleep(25); 312 | #else 313 | usleep(250000); 314 | #endif 315 | } 316 | } 317 | return 0; 318 | } 319 | 320 | #ifdef ENABLE_CORSAIR_RGB 321 | static void set_led( int x, int y, int r, int g, int b ) 322 | { 323 | int led = led_matrix[y][x]; 324 | 325 | if(led >= 144) 326 | { 327 | return; 328 | } 329 | 330 | if( r > 7 ) r = 7; 331 | if( g > 7 ) g = 7; 332 | if( b > 7 ) b = 7; 333 | 334 | r = 7 - r; 335 | g = 7 - g; 336 | b = 7 - b; 337 | 338 | red_val[led] = r; 339 | grn_val[led] = g; 340 | blu_val[led] = b; 341 | } 342 | 343 | 344 | static int init_keyboard() 345 | { 346 | int status = 0; 347 | 348 | printf("Searching for Corsair K70 RGB keyboard...\n"); 349 | 350 | #ifdef USE_WINDOWS_USB 351 | dev = GetDeviceHandle(0x1B1C, 0x1B13, 0x3); 352 | #else 353 | dev = find_device(0x1B1C, 0x1B13); 354 | #endif 355 | 356 | if(dev == NULL) 357 | { 358 | printf("Searching for Corsair K95 RGB keyboard...\n"); 359 | #ifdef USE_WINDOWS_USB 360 | dev = GetDeviceHandle(0x1B1C, 0x1B11, 0x3); 361 | #else 362 | dev = find_device(0x1B1C, 0x1B11); 363 | #endif 364 | } 365 | 366 | if(dev == NULL) 367 | { 368 | printf("Corsair K70 RGB keyboard not detected :(\n"); 369 | return 1; 370 | } 371 | else 372 | { 373 | printf("Corsair K70 RGB keyboard detected successfully :)\n"); 374 | } 375 | 376 | #ifndef USE_WINDOWS_USB 377 | handle = usb_open(dev); 378 | 379 | if(handle == NULL) 380 | { 381 | printf("USB device handle did not open :(\n"); 382 | return 1; 383 | } 384 | else 385 | { 386 | printf("USB device handle opened successfully :)\n"); 387 | } 388 | 389 | #ifndef WIN32 390 | status = usb_claim_interface(handle, 3); 391 | 392 | 393 | status = usb_detach_kernel_driver_np(handle, 3); 394 | 395 | if(status == 0) 396 | { 397 | printf("USB interface claimed successfully :)\n"); 398 | } 399 | else 400 | { 401 | printf("USB interface claim failed with error %d :(\n", status); 402 | return 1; 403 | } 404 | #endif 405 | #endif 406 | 407 | // Construct XY lookup table 408 | unsigned char *keys = position_map; 409 | float *sizes = size_map; 410 | for (int y = 0; y < 7; y++) 411 | { 412 | unsigned char key; 413 | int size = 0; 414 | 415 | for (int x = 0; x < 92; x++) 416 | { 417 | if (size == 0) 418 | { 419 | float sizef = *sizes++; 420 | if (sizef < 0) 421 | { 422 | size = -sizef * 4; 423 | key = 255; 424 | } 425 | else 426 | { 427 | key = *keys++; 428 | size = sizef * 4; 429 | } 430 | } 431 | 432 | led_matrix[y][x] = key; 433 | size--; 434 | } 435 | 436 | if (*keys++ != 255 || *sizes++ != 0) 437 | { 438 | printf("Bad line %d\n", y); 439 | return 1; 440 | } 441 | } 442 | 443 | return 0; 444 | } 445 | 446 | #ifdef USE_WINDOWS_USB 447 | static void send_usb_msg(char * data_pkt) 448 | { 449 | char usb_pkt[65]; 450 | for(int i = 1; i < 65; i++) 451 | { 452 | usb_pkt[i] = data_pkt[i-1]; 453 | } 454 | HidD_SetFeature(dev, usb_pkt, 65); 455 | } 456 | #endif 457 | 458 | static void update_keyboard() 459 | { 460 | // Perform USB control message to keyboard 461 | // 462 | // Request Type: 0x21 463 | // Request: 0x09 464 | // Value 0x0300 465 | // Index: 0x03 466 | // Size: 64 467 | 468 | data_pkt[0][0] = 0x7F; 469 | data_pkt[0][1] = 0x01; 470 | data_pkt[0][2] = 0x3C; 471 | 472 | data_pkt[1][0] = 0x7F; 473 | data_pkt[1][1] = 0x02; 474 | data_pkt[1][2] = 0x3C; 475 | 476 | data_pkt[2][0] = 0x7F; 477 | data_pkt[2][1] = 0x03; 478 | data_pkt[2][2] = 0x3C; 479 | 480 | data_pkt[3][0] = 0x7F; 481 | data_pkt[3][1] = 0x04; 482 | data_pkt[3][2] = 0x24; 483 | 484 | data_pkt[4][0] = 0x07; 485 | data_pkt[4][1] = 0x27; 486 | data_pkt[4][4] = 0xD8; 487 | 488 | for(int i = 0; i < 60; i++) 489 | { 490 | data_pkt[0][i+4] = red_val[i*2+1] << 4 | red_val[i*2]; 491 | } 492 | 493 | for(int i = 0; i < 12; i++) 494 | { 495 | data_pkt[1][i+4] = red_val[i*2+121] << 4 | red_val[i*2+120]; 496 | } 497 | 498 | for(int i = 0; i < 48; i++) 499 | { 500 | data_pkt[1][i+16] = grn_val[i*2+1] << 4 | grn_val[i*2]; 501 | } 502 | 503 | for(int i = 0; i < 24; i++) 504 | { 505 | data_pkt[2][i+4] = grn_val[i*2+97] << 4 | grn_val[i*2+96]; 506 | } 507 | 508 | for(int i = 0; i < 36; i++) 509 | { 510 | data_pkt[2][i+28] = blu_val[i*2+1] << 4 | blu_val[i*2]; 511 | } 512 | 513 | for(int i = 0; i < 36; i++) 514 | { 515 | data_pkt[3][i+4] = blu_val[i*2+73] << 4 | blu_val[i*2+72]; 516 | } 517 | 518 | #ifdef USE_WINDOWS_USB 519 | send_usb_msg(data_pkt[0]); 520 | send_usb_msg(data_pkt[1]); 521 | send_usb_msg(data_pkt[2]); 522 | send_usb_msg(data_pkt[3]); 523 | send_usb_msg(data_pkt[4]); 524 | #else 525 | usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[0], 64, 1000); 526 | usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[1], 64, 1000); 527 | usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[2], 64, 1000); 528 | usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[3], 64, 1000); 529 | usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[4], 64, 1000); 530 | #endif 531 | } 532 | 533 | #ifndef USE_WINDOWS_USB 534 | static struct usb_device *find_device(uint16_t vendor, uint16_t product) 535 | { 536 | struct usb_bus *bus; 537 | struct usb_device *dev; 538 | struct usb_bus *busses; 539 | 540 | usb_init(); 541 | usb_find_busses(); 542 | usb_find_devices(); 543 | 544 | busses = usb_get_busses(); 545 | 546 | for(bus = busses; bus; bus = bus->next) 547 | { 548 | for(dev = bus->devices; dev; dev = dev->next) 549 | { 550 | if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product)) 551 | { 552 | return dev; 553 | } 554 | } 555 | } 556 | 557 | return NULL; 558 | } 559 | #endif 560 | 561 | #ifdef USE_WINDOWS_USB 562 | //================================================================================================== 563 | // Code by http://www.reddit.com/user/chrisgzy 564 | //================================================================================================== 565 | bool IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI) 566 | { 567 | unsigned int uiLocalVID = 0, uiLocalPID = 0, uiLocalMI = 0; 568 | 569 | LPWSTR pszNextToken = 0; 570 | LPWSTR pszToken = wcstok(pDeviceID, L"\\#&"); 571 | while (pszToken) 572 | { 573 | std::wstring tokenStr(pszToken); 574 | if (tokenStr.find(L"VID_", 0, 4) != std::wstring::npos) 575 | { 576 | std::wistringstream iss(tokenStr.substr(4)); 577 | iss >> std::hex >> uiLocalVID; 578 | } 579 | else if (tokenStr.find(L"PID_", 0, 4) != std::wstring::npos) 580 | { 581 | std::wistringstream iss(tokenStr.substr(4)); 582 | iss >> std::hex >> uiLocalPID; 583 | } 584 | else if (tokenStr.find(L"MI_", 0, 3) != std::wstring::npos) 585 | { 586 | std::wistringstream iss(tokenStr.substr(3)); 587 | iss >> std::hex >> uiLocalMI; 588 | } 589 | 590 | pszToken = wcstok(0, L"\\#&"); 591 | } 592 | 593 | if (uiVID != uiLocalVID || uiPID != uiLocalPID || uiMI != uiLocalMI) 594 | return false; 595 | 596 | return true; 597 | } 598 | 599 | 600 | //================================================================================================== 601 | // Code by http://www.reddit.com/user/chrisgzy 602 | //================================================================================================== 603 | HANDLE GetDeviceHandle(unsigned int uiVID, unsigned int uiPID, unsigned int uiMI) 604 | { 605 | const GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 }; 606 | HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_HID, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); 607 | if (hDevInfo == INVALID_HANDLE_VALUE) 608 | return 0; 609 | 610 | HANDLE hReturn = 0; 611 | 612 | SP_DEVINFO_DATA deviceData = { 0 }; 613 | deviceData.cbSize = sizeof(SP_DEVINFO_DATA); 614 | 615 | for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) 616 | { 617 | wchar_t wszDeviceID[MAX_DEVICE_ID_LEN]; 618 | if (CM_Get_Device_IDW(deviceData.DevInst, wszDeviceID, MAX_DEVICE_ID_LEN, 0)) 619 | continue; 620 | 621 | if (!IsMatchingDevice(wszDeviceID, uiVID, uiPID, uiMI)) 622 | continue; 623 | 624 | SP_INTERFACE_DEVICE_DATA interfaceData = { 0 }; 625 | interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA); 626 | 627 | if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE_HID, 0, &interfaceData)) 628 | break; 629 | 630 | DWORD dwRequiredSize = 0; 631 | SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0); 632 | 633 | SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize]; 634 | pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); 635 | 636 | if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0)) 637 | { 638 | delete pData; 639 | break; 640 | } 641 | 642 | HANDLE hDevice = CreateFile(pData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); 643 | if (hDevice == INVALID_HANDLE_VALUE) 644 | { 645 | delete pData; 646 | break; 647 | } 648 | 649 | hReturn = hDevice; 650 | break; 651 | } 652 | 653 | SetupDiDestroyDeviceInfoList(hDevInfo); 654 | 655 | return hReturn; 656 | } 657 | #endif 658 | #endif // ENABLE_CORSAIR_RGB 659 | -------------------------------------------------------------------------------- /FanBusAudio.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1345311700 source:z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\chuck_fft.c 3 | "chuck_fft.h" 4 | 5 | 6 | 7 | 1345952647 8 | 9 | 1345311711 z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\chuck_fft.h 10 | 11 | 1358832104 source:z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\fanbus.cpp 12 | "fanbus.h" 13 | 14 | 1344726237 z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\fanbus.h 15 | "serial_port.h" 16 | 17 | 1345337556 rt.h" 18 | 19 | 1358832969 z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\serial_port.h 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 1189868550 source:z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\main.cpp 30 | 31 | 32 | 33 | 34 | 35 | 1189868550 > 36 | 37 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl.h 38 | "SDL_main.h" 39 | "SDL_stdinc.h" 40 | "SDL_audio.h" 41 | "SDL_cdrom.h" 42 | "SDL_cpuinfo.h" 43 | "SDL_endian.h" 44 | "SDL_error.h" 45 | "SDL_events.h" 46 | "SDL_loadso.h" 47 | "SDL_mutex.h" 48 | "SDL_rwops.h" 49 | "SDL_thread.h" 50 | "SDL_timer.h" 51 | "SDL_video.h" 52 | "SDL_version.h" 53 | "begin_code.h" 54 | "close_code.h" 55 | 56 | 1325738639 e.h" 57 | 58 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_main.h 59 | "SDL_stdinc.h" 60 | "begin_code.h" 61 | "close_code.h" 62 | "begin_code.h" 63 | "close_code.h" 64 | 65 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_stdinc.h 66 | "SDL_config.h" 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | "begin_code.h" 84 | 85 | 86 | 87 | 88 | 89 | "close_code.h" 90 | 91 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config.h 92 | "SDL_platform.h" 93 | "SDL_config_dreamcast.h" 94 | "SDL_config_macos.h" 95 | "SDL_config_macosx.h" 96 | "SDL_config_symbian.h" 97 | "SDL_config_win32.h" 98 | "SDL_config_os2.h" 99 | "SDL_config_minimal.h" 100 | 101 | 1326110987 g_minimal.h" 102 | 103 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_platform.h 104 | 105 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_dreamcast.h 106 | "SDL_platform.h" 107 | 108 | 1326110994 orm.h" 109 | 110 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_macos.h 111 | "SDL_platform.h" 112 | 113 | 114 | 1345335283 h> 115 | 116 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_macosx.h 117 | "SDL_platform.h" 118 | 119 | 120 | 1345335283 ityMacros.h> 121 | 122 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_symbian.h 123 | "SDL_platform.h" 124 | 125 | 126 | 127 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_win32.h 128 | "SDL_platform.h" 129 | 130 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_os2.h 131 | "SDL_platform.h" 132 | 133 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_config_minimal.h 134 | "SDL_platform.h" 135 | 136 | 137 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\begin_code.h 138 | 139 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\close_code.h 140 | 141 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_audio.h 142 | "SDL_stdinc.h" 143 | "SDL_error.h" 144 | "SDL_endian.h" 145 | "SDL_mutex.h" 146 | "SDL_thread.h" 147 | "SDL_rwops.h" 148 | "begin_code.h" 149 | "close_code.h" 150 | 151 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_error.h 152 | "SDL_stdinc.h" 153 | "begin_code.h" 154 | "close_code.h" 155 | 156 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_endian.h 157 | "SDL_stdinc.h" 158 | 159 | "begin_code.h" 160 | "close_code.h" 161 | 162 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_mutex.h 163 | "SDL_stdinc.h" 164 | "SDL_error.h" 165 | "begin_code.h" 166 | "close_code.h" 167 | 168 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_thread.h 169 | "SDL_stdinc.h" 170 | "SDL_error.h" 171 | "SDL_mutex.h" 172 | "begin_code.h" 173 | 174 | "close_code.h" 175 | 176 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_rwops.h 177 | "SDL_stdinc.h" 178 | "SDL_error.h" 179 | "begin_code.h" 180 | "close_code.h" 181 | 182 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_cdrom.h 183 | "SDL_stdinc.h" 184 | "SDL_error.h" 185 | "begin_code.h" 186 | "close_code.h" 187 | 188 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_cpuinfo.h 189 | "SDL_stdinc.h" 190 | "begin_code.h" 191 | "close_code.h" 192 | 193 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_events.h 194 | "SDL_stdinc.h" 195 | "SDL_error.h" 196 | "SDL_active.h" 197 | "SDL_keyboard.h" 198 | "SDL_mouse.h" 199 | "SDL_joystick.h" 200 | "SDL_quit.h" 201 | "begin_code.h" 202 | "close_code.h" 203 | 204 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_active.h 205 | "SDL_stdinc.h" 206 | "SDL_error.h" 207 | "begin_code.h" 208 | "close_code.h" 209 | 210 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_keyboard.h 211 | "SDL_stdinc.h" 212 | "SDL_error.h" 213 | "SDL_keysym.h" 214 | "begin_code.h" 215 | "close_code.h" 216 | 217 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_keysym.h 218 | 219 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_mouse.h 220 | "SDL_stdinc.h" 221 | "SDL_error.h" 222 | "SDL_video.h" 223 | "begin_code.h" 224 | "close_code.h" 225 | 226 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_video.h 227 | "SDL_stdinc.h" 228 | "SDL_error.h" 229 | "SDL_rwops.h" 230 | "begin_code.h" 231 | "close_code.h" 232 | 233 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_joystick.h 234 | "SDL_stdinc.h" 235 | "SDL_error.h" 236 | "begin_code.h" 237 | "close_code.h" 238 | 239 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_quit.h 240 | "SDL_stdinc.h" 241 | "SDL_error.h" 242 | 243 | 1325738639 .h" 244 | 245 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_loadso.h 246 | "SDL_stdinc.h" 247 | "SDL_error.h" 248 | "begin_code.h" 249 | "close_code.h" 250 | 251 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_timer.h 252 | "SDL_stdinc.h" 253 | "SDL_error.h" 254 | "begin_code.h" 255 | "close_code.h" 256 | 257 | 1345331683 c:\program files (x86)\codeblocks\devpaks\include\sdl_version.h 258 | "SDL_stdinc.h" 259 | "begin_code.h" 260 | "close_code.h" 261 | 262 | 1358832961 source:z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\serial_port.cpp 263 | "serial_port.h" 264 | 265 | 1360195853 source:z:\programming\fanbus fan controller\fanbus-fan-controller\pc applications\fanbusaudio\fanbusaudio.cpp 266 | 267 | 268 | 269 | 270 | "chuck_fft.h" 271 | 272 | "serial_port.h" 273 | "fanbus.h" 274 | 275 | 276 | 1345335872 c:\openal-soft-1.14-bin\include\al\al.h 277 | 278 | 1345335872 c:\openal-soft-1.14-bin\include\al\alc.h 279 | 280 | 1345605572 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/FanBusAudio.cpp 281 | 282 | 283 | 284 | 285 | "chuck_fft.h" 286 | 287 | "serial_port.h" 288 | "fanbus.h" 289 | 290 | 291 | 1345315311 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/chuck_fft.h 292 | 293 | 1345337612 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/serial_port.h 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 1344729837 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/fanbus.h 312 | "serial_port.h" 313 | "serial_port.h" 314 | 315 | 1345315300 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/chuck_fft.c 316 | "chuck_fft.h" 317 | 318 | 319 | "chuck_fft.h" 320 | 321 | 322 | 323 | 1345952647 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/fanbus.cpp 324 | "fanbus.h" 325 | "fanbus.h" 326 | 327 | 1345337556 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/serial_port.cpp 328 | "serial_port.h" 329 | "serial_port.h" 330 | 331 | 1345332272 c:\libraries\openal-soft-1.14-bin\include\al\al.h 332 | 333 | 1345332272 c:\libraries\openal-soft-1.14-bin\include\al\alc.h 334 | 335 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl.h 336 | "SDL_main.h" 337 | "SDL_stdinc.h" 338 | "SDL_audio.h" 339 | "SDL_cdrom.h" 340 | "SDL_cpuinfo.h" 341 | "SDL_endian.h" 342 | "SDL_error.h" 343 | "SDL_events.h" 344 | "SDL_loadso.h" 345 | "SDL_mutex.h" 346 | "SDL_rwops.h" 347 | "SDL_thread.h" 348 | "SDL_timer.h" 349 | "SDL_video.h" 350 | "SDL_version.h" 351 | "begin_code.h" 352 | "close_code.h" 353 | 354 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_main.h 355 | "SDL_stdinc.h" 356 | "begin_code.h" 357 | "close_code.h" 358 | "begin_code.h" 359 | "close_code.h" 360 | 361 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_stdinc.h 362 | "SDL_config.h" 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | "begin_code.h" 380 | 381 | 382 | 383 | 384 | 385 | "close_code.h" 386 | 387 | 1326110987 c:\libraries\sdl-1.2.15\include\sdl\sdl_config.h 388 | "SDL_platform.h" 389 | "SDL_config_dreamcast.h" 390 | "SDL_config_macos.h" 391 | "SDL_config_macosx.h" 392 | "SDL_config_symbian.h" 393 | "SDL_config_win32.h" 394 | "SDL_config_os2.h" 395 | "SDL_config_minimal.h" 396 | 397 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_platform.h 398 | 399 | 1326110994 c:\libraries\sdl-1.2.15\include\sdl\sdl_config_win32.h 400 | "SDL_platform.h" 401 | 402 | 1326111000 c:\libraries\sdl-1.2.15\include\sdl\begin_code.h 403 | 404 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\close_code.h 405 | 406 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_audio.h 407 | "SDL_stdinc.h" 408 | "SDL_error.h" 409 | "SDL_endian.h" 410 | "SDL_mutex.h" 411 | "SDL_thread.h" 412 | "SDL_rwops.h" 413 | "begin_code.h" 414 | "close_code.h" 415 | 416 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_error.h 417 | "SDL_stdinc.h" 418 | "begin_code.h" 419 | "close_code.h" 420 | 421 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_endian.h 422 | "SDL_stdinc.h" 423 | 424 | "begin_code.h" 425 | "close_code.h" 426 | 427 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_mutex.h 428 | "SDL_stdinc.h" 429 | "SDL_error.h" 430 | "begin_code.h" 431 | "close_code.h" 432 | 433 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_thread.h 434 | "SDL_stdinc.h" 435 | "SDL_error.h" 436 | "SDL_mutex.h" 437 | "begin_code.h" 438 | 439 | "close_code.h" 440 | 441 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_rwops.h 442 | "SDL_stdinc.h" 443 | "SDL_error.h" 444 | "begin_code.h" 445 | "close_code.h" 446 | 447 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_cdrom.h 448 | "SDL_stdinc.h" 449 | "SDL_error.h" 450 | "begin_code.h" 451 | "close_code.h" 452 | 453 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_cpuinfo.h 454 | "SDL_stdinc.h" 455 | "begin_code.h" 456 | "close_code.h" 457 | 458 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_events.h 459 | "SDL_stdinc.h" 460 | "SDL_error.h" 461 | "SDL_active.h" 462 | "SDL_keyboard.h" 463 | "SDL_mouse.h" 464 | "SDL_joystick.h" 465 | "SDL_quit.h" 466 | "begin_code.h" 467 | "close_code.h" 468 | 469 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_active.h 470 | "SDL_stdinc.h" 471 | "SDL_error.h" 472 | "begin_code.h" 473 | "close_code.h" 474 | 475 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_keyboard.h 476 | "SDL_stdinc.h" 477 | "SDL_error.h" 478 | "SDL_keysym.h" 479 | "begin_code.h" 480 | "close_code.h" 481 | 482 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_keysym.h 483 | 484 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_mouse.h 485 | "SDL_stdinc.h" 486 | "SDL_error.h" 487 | "SDL_video.h" 488 | "begin_code.h" 489 | "close_code.h" 490 | 491 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_video.h 492 | "SDL_stdinc.h" 493 | "SDL_error.h" 494 | "SDL_rwops.h" 495 | "begin_code.h" 496 | "close_code.h" 497 | 498 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_joystick.h 499 | "SDL_stdinc.h" 500 | "SDL_error.h" 501 | "begin_code.h" 502 | "close_code.h" 503 | 504 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_quit.h 505 | "SDL_stdinc.h" 506 | "SDL_error.h" 507 | 508 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_loadso.h 509 | "SDL_stdinc.h" 510 | "SDL_error.h" 511 | "begin_code.h" 512 | "close_code.h" 513 | 514 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_timer.h 515 | "SDL_stdinc.h" 516 | "SDL_error.h" 517 | "begin_code.h" 518 | "close_code.h" 519 | 520 | 1325738639 c:\libraries\sdl-1.2.15\include\sdl\sdl_version.h 521 | "SDL_stdinc.h" 522 | "begin_code.h" 523 | "close_code.h" 524 | 525 | 1336970605 c:\libraries\sdl_gfx-2.0.24\include\sdl\sdl_gfxprimitives.h 526 | 527 | "SDL.h" 528 | 529 | 1345315300 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/chuck_fft.c 530 | "chuck_fft.h" 531 | 532 | 533 | 534 | 1345315311 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/chuck_fft.h 535 | 536 | 1358832104 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/fanbus.cpp 537 | "fanbus.h" 538 | 539 | 1344729837 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/fanbus.h 540 | "serial_port.h" 541 | 542 | 1358832969 /media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/serial_port.h 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 1360196167 source:/media/RAID5/Programming/FanBus Fan Controller/FanBus-Fan-Controller/PC Applications/FanBusAudio/serial_port.cpp 553 | "serial_port.h" 554 | 555 | 1345315300 source:/media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/chuck_fft.c 556 | "chuck_fft.h" 557 | 558 | 559 | 560 | 1345315311 /media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/chuck_fft.h 561 | 562 | 1358832104 source:/media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/fanbus.cpp 563 | "fanbus.h" 564 | 565 | 1344729837 /media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/fanbus.h 566 | "serial_port.h" 567 | 568 | 1358832969 /media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/serial_port.h 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 1412989098 source:/media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/FanBusAudio.cpp 579 | 580 | 581 | 582 | 583 | "chuck_fft.h" 584 | 585 | "serial_port.h" 586 | "fanbus.h" 587 | 588 | 589 | 1360196167 source:/media/RAID5/Programming/FanBus Fan Controller/PC Applications/FanBusAudio - Copy/serial_port.cpp 590 | "serial_port.h" 591 | 592 | -------------------------------------------------------------------------------- /FanBusAudio.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CorsairKeyboardSpectrograph 2 | =========================== 3 | 4 | Spectrograph audio visualizer for Corsair K70 RGB keyboard for Windows and Linux. Now also supports the Razer BlackWidow Chroma in Windows via the official SDK. 5 | 6 | 7 | See these Reddit threads for more information about Corsair K70 USB protocol decoding: 8 | 9 | http://www.reddit.com/r/MechanicalKeyboards/comments/2ij2um/corsair_k70_rgb_usb_protocol_reverse_engineering/ 10 | 11 | http://www.reddit.com/r/MechanicalKeyboards/comments/2imkdx/corsair_k70_rgb_music_visualizer_in_linux/ 12 | 13 | http://www.reddit.com/r/MechanicalKeyboards/comments/2iwv1i/my_k70_rgb_and_my_rgb_case_fans_rgb_battlestation/ 14 | 15 | http://www.reddit.com/r/MechanicalKeyboards/comments/2ipync/corsair_k70_rgb_usb_protocol_reverse_engineering/ 16 | 17 | http://www.reddit.com/r/MechanicalKeyboards/comments/2j6j1w/corsair_k70_rgb_linux_music_visualizer_2/ 18 | 19 | What is it 20 | ---------- 21 | 22 | This is a simple audio input visualization that uses the K70 LEDs as a "bar graph" of sorts for the audio spectrum. It also continuously cycles through a dim rainbow pattern as the background for this visualization. Audio comes from the default input device, so if you want to use it to visualize the output audio you must use some form of a loopback device. In Linux you can use PulseAudio's monitor inputs to get a digital loopback while in Windows many audio chipsets have an internal "Stereo Mix" or similar loopback. Failing that, there are programs like Virtual Audio Cable that can do a softare loopback in Windows. 23 | 24 | Dependencies 25 | ------------ 26 | 27 | - libusb-0.1 (libusb-win32) - This library is used for communicating with the keyboard USB interface to send the LED commands. On Windows you must install the libusb filter driver for endpoint 3 of the keyboard while on Linux you must add a udev rule for it or run the program as root. 28 | 29 | - libSDL - This library is used for drawing the on-screen spectrum and waveform graph. This is mostly for development/debugging purposes 30 | 31 | - libSDL_gfx - This library provides drawing routines to use with SDL, used for drawing lines and bars 32 | 33 | 34 | - OpenAL - This library provides the audio input for the program. 35 | 36 | Compiling 37 | --------- 38 | 39 | I made the project in Code::Blocks and included my .cbp project file. In Linux you just need to install the requisite dependencies from your package manager and it should work. In Windows, download the libraries and extract to C:\libraries\, you may need to edit the paths in the Code::Blocks project to reflect the names of your libraries directory if you're building with different versions of the libraries than I used. 40 | -------------------------------------------------------------------------------- /RzChromaSDKDefines.h: -------------------------------------------------------------------------------- 1 | //! \file RzChromaSDKDefines.h 2 | //! \brief Defines. 3 | //! 4 | //! Definitions of global and static variables. 5 | 6 | #ifndef _RZSCHROMADKDEFINES_H_ 7 | #define _RZSCHROMADKDEFINES_H_ 8 | 9 | #pragma once 10 | 11 | #ifndef GUID_DEFINED 12 | #include 13 | #endif 14 | 15 | namespace ChromaSDK 16 | { 17 | 18 | //! Razer Blackwidow Chroma device Id. 19 | // {2EA1BB63-CA28-428D-9F06-196B88330BBB} 20 | DEFINE_GUID(RZ_BLACKWIDOW_CHROMA, 21 | 0x2ea1bb63, 0xca28, 0x428d, 0x9f, 0x6, 0x19, 0x6b, 0x88, 0x33, 0xb, 0xbb); 22 | 23 | } 24 | 25 | 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /RzChromaSDKTypes.h: -------------------------------------------------------------------------------- 1 | //! \file RzChromaSDKTypes.h 2 | //! \brief Data types. 3 | 4 | #ifndef _RZCHROMASDKTYPES_H_ 5 | #define _RZCHROMASDKTYPES_H_ 6 | 7 | #pragma once 8 | 9 | typedef LONG RZRESULT; //!< Return result. 10 | typedef GUID RZEFFECTID; //!< Effect Id. 11 | typedef unsigned int RZDURATION; //!< Milliseconds. 12 | typedef size_t RZSIZE; //!< Size. 13 | typedef void* PRZPARAM; //!< Context sensitive pointer. 14 | typedef DWORD RZID; //!< Generic data type for Identifier. 15 | 16 | namespace ChromaSDK 17 | { 18 | //! Chroma supported products 19 | typedef enum PRODUCTTYPE 20 | { 21 | PRODUCT_NONE = 0, 22 | PRODUCT_BLACKWIDOW_CHROMA, // Device Id = 2EA1BB63-CA28-428D-9F06-196B88330BBB 23 | PRODUCT_INVALID 24 | } PRODUCTTYPE; 25 | 26 | //! Event notification Window message 27 | const UINT WM_CHROMA_EVENT = WM_APP+0x2000; 28 | 29 | namespace Keyboard 30 | { 31 | //! Definitions of keys. 32 | typedef enum RZKEY 33 | { 34 | RZKEY_ESC = 0x0001, /*!< Esc */ 35 | RZKEY_F1 = 0x0003, /*!< F1 */ 36 | RZKEY_F2 = 0x0004, /*!< F2 */ 37 | RZKEY_F3 = 0x0005, /*!< F3 */ 38 | RZKEY_F4 = 0x0006, /*!< F4 */ 39 | RZKEY_F5 = 0x0007, /*!< F5 */ 40 | RZKEY_F6 = 0x0008, /*!< F6 */ 41 | RZKEY_F7 = 0x0009, /*!< F7 */ 42 | RZKEY_F8 = 0x000A, /*!< F8 */ 43 | RZKEY_F9 = 0x000B, /*!< F9 */ 44 | RZKEY_F10 = 0x000C, /*!< F10 */ 45 | RZKEY_F11 = 0x000D, /*!< F11 */ 46 | RZKEY_F12 = 0x000E, /*!< F12 */ 47 | RZKEY_1 = 0x0102, /*!< 1 */ 48 | RZKEY_2 = 0x0103, /*!< 2 */ 49 | RZKEY_3 = 0x0104, /*!< 3 */ 50 | RZKEY_4 = 0x0105, /*!< 4 */ 51 | RZKEY_5 = 0x0106, /*!< 5 */ 52 | RZKEY_6 = 0x0107, /*!< 6 */ 53 | RZKEY_7 = 0x0108, /*!< 7 */ 54 | RZKEY_8 = 0x0109, /*!< 8 */ 55 | RZKEY_9 = 0x010A, /*!< 9 */ 56 | RZKEY_0 = 0x010B, /*!< 0 */ 57 | RZKEY_A = 0x0302, /*!< A */ 58 | RZKEY_B = 0x0407, /*!< B */ 59 | RZKEY_C = 0x0405, /*!< C */ 60 | RZKEY_D = 0x0304, /*!< D */ 61 | RZKEY_E = 0x0204, /*!< E */ 62 | RZKEY_F = 0x0305, /*!< F */ 63 | RZKEY_G = 0x0306, /*!< G */ 64 | RZKEY_H = 0x0307, /*!< H */ 65 | RZKEY_I = 0x0209, /*!< I */ 66 | RZKEY_J = 0x0308, /*!< J */ 67 | RZKEY_K = 0x0309, /*!< K */ 68 | RZKEY_L = 0x030A, /*!< L */ 69 | RZKEY_M = 0x0409, /*!< M */ 70 | RZKEY_N = 0x0408, /*!< N */ 71 | RZKEY_O = 0x020A, /*!< O */ 72 | RZKEY_P = 0x020B, /*!< P */ 73 | RZKEY_Q = 0x0202, /*!< Q */ 74 | RZKEY_R = 0x0205, /*!< R */ 75 | RZKEY_S = 0x0303, /*!< S */ 76 | RZKEY_T = 0x0206, /*!< T */ 77 | RZKEY_U = 0x0208, /*!< U */ 78 | RZKEY_V = 0x0406, /*!< V */ 79 | RZKEY_W = 0x0203, /*!< W */ 80 | RZKEY_X = 0x0404, /*!< X */ 81 | RZKEY_Y = 0x0207, /*!< Y */ 82 | RZKEY_Z = 0x0403, /*!< Z */ 83 | RZKEY_NUMLOCK = 0x0112, /*!< Numlock */ 84 | RZKEY_NUMPAD0 = 0x0513, /*!< Numpad 0 */ 85 | RZKEY_NUMPAD1 = 0x0412, /*!< Numpad 1 */ 86 | RZKEY_NUMPAD2 = 0x0413, /*!< Numpad 2 */ 87 | RZKEY_NUMPAD3 = 0x0414, /*!< Numpad 3 */ 88 | RZKEY_NUMPAD4 = 0x0312, /*!< Numpad 4 */ 89 | RZKEY_NUMPAD5 = 0x0313, /*!< Numpad 5 */ 90 | RZKEY_NUMPAD6 = 0x0314, /*!< Numpad 6 */ 91 | RZKEY_NUMPAD7 = 0x0212, /*!< Numpad 7 */ 92 | RZKEY_NUMPAD8 = 0x0213, /*!< Numpad 8 */ 93 | RZKEY_NUMPAD9 = 0x0214, /*!< Numpad 9*/ 94 | RZKEY_NUMPAD_DIVIDE = 0x0113, /*!< Divide */ 95 | RZKEY_NUMPAD_MULTIPLY = 0x0114, /*!< Multiply */ 96 | RZKEY_NUMPAD_SUBTRACT = 0x0115, /*!< Subtract */ 97 | RZKEY_NUMPAD_ADD = 0x0215, /*!< Add */ 98 | RZKEY_NUMPAD_ENTER = 0x0415, /*!< Enter */ 99 | RZKEY_NUMPAD_DECIMAL = 0x0514, /*!< Decimal */ 100 | RZKEY_PRINTSCREEN = 0x000F, /*!< Print Screen */ 101 | RZKEY_SCROLL = 0x0010, /*!< Scroll Lock */ 102 | RZKEY_PAUSE = 0x0011, /*!< Pause */ 103 | RZKEY_INSERT = 0x010F, /*!< Insert */ 104 | RZKEY_HOME = 0x0110, /*!< Home */ 105 | RZKEY_PAGEUP = 0x0111, /*!< Page Up */ 106 | RZKEY_DELETE = 0x020f, /*!< Delete */ 107 | RZKEY_END = 0x0210, /*!< End */ 108 | RZKEY_PAGEDOWN = 0x0211, /*!< Page Down */ 109 | RZKEY_UP = 0x0410, /*!< Up */ 110 | RZKEY_LEFT = 0x050F, /*!< Left */ 111 | RZKEY_DOWN = 0x0510, /*!< Down */ 112 | RZKEY_RIGHT = 0x0511, /*!< Right */ 113 | RZKEY_TAB = 0x0201, /*!< Tab */ 114 | RZKEY_CAPSLOCK = 0x0301, /*!< Caps Lock*/ 115 | RZKEY_BACKSPACE = 0x010E, /*!< Backspace */ 116 | RZKEY_ENTER = 0x030E, /*!< Enter */ 117 | RZKEY_LCTRL = 0x0501, /*!< Left Control*/ 118 | RZKEY_LWIN = 0x0502, /*!< Left Window */ 119 | RZKEY_LALT = 0x0503, /*!< Left Alt */ 120 | RZKEY_SPACE = 0x0507, /*!< Spacebar */ 121 | RZKEY_RALT = 0x050B, /*!< Right Alt */ 122 | RZKEY_FN = 0x050C, /*!< 'Fn' function key. */ 123 | RZKEY_RMENU = 0x050D, /*!< Right Menu */ 124 | RZKEY_RCTRL = 0x050E, /*!< Right Control */ 125 | RZKEY_LSHIFT = 0x0401, /*!< Left Shift */ 126 | RZKEY_RSHIFT = 0x040E, /*!< Right Shift */ 127 | RZKEY_MACRO1 = 0x0100, /*!< Macro Key 1*/ 128 | RZKEY_MACRO2 = 0x0200, /*!< Macro Key 2*/ 129 | RZKEY_MACRO3 = 0x0300, /*!< Macro Key 3 */ 130 | RZKEY_MACRO4 = 0x0400, /*!< Macro Key 4*/ 131 | RZKEY_MACRO5 = 0x0500, /*!< Macro Key 5 */ 132 | RZKEY_OEM_1 = 0x0101, /*!< ~ tilde. 半角/全角 */ 133 | RZKEY_OEM_2 = 0x010C, /*!< - minus. */ 134 | RZKEY_OEM_3 = 0x010D, /*!< = equal. */ 135 | RZKEY_OEM_4 = 0x020C, /*!< [ left sqaure bracket. */ 136 | RZKEY_OEM_5 = 0x020D, /*!< ] right square bracket. */ 137 | RZKEY_OEM_6 = 0x020E, /*!< \ forward slash. */ 138 | RZKEY_OEM_7 = 0x030B, /*!< ; semi-colon. */ 139 | RZKEY_OEM_8 = 0x030C, /*!< ' apostrophe. */ 140 | RZKEY_OEM_9 = 0x040A, /*!< , comma. */ 141 | RZKEY_OEM_10 = 0x040B, /*!< . period. */ 142 | RZKEY_OEM_11 = 0x040C, /*!< / backslash. */ 143 | RZKEY_EUR_1 = 0x030D, /*!< # */ 144 | RZKEY_EUR_2 = 0x0402, /*!< \ */ 145 | RZKEY_JPN_1 = 0x0015, /*!< ¥ */ 146 | RZKEY_JPN_2 = 0x040D, /*!< / */ 147 | RZKEY_JPN_3 = 0x0504, /*!< 無変換 */ 148 | RZKEY_JPN_4 = 0x0509, /*!< 変換 */ 149 | RZKEY_JPN_5 = 0x050A, /*!< ひらがな/カタカナ */ 150 | RZKEY_KOR_1 = 0x0015, /*!< | */ 151 | RZKEY_KOR_2 = 0x030D, 152 | RZKEY_KOR_3 = 0x0402, 153 | RZKEY_KOR_4 = 0x040D, 154 | RZKEY_KOR_5 = 0x0504, /*!< 한자 */ 155 | RZKEY_KOR_6 = 0x0509, /*!< 한/영 */ 156 | RZKEY_KOR_7 = 0x050A, 157 | RZKEY_INVALID = 0xFFFF /*!< Invalid keys. */ 158 | } RZKEY; 159 | 160 | //! Maximum number of rows in a keyboard. 161 | const RZSIZE MAX_ROW = 6; 162 | 163 | //! Maximum number of columns in a keyboard. 164 | const RZSIZE MAX_COLUMN = 22; 165 | 166 | //! Maximum number of keys. 167 | const RZSIZE MAX_KEYS = MAX_ROW * MAX_COLUMN; 168 | 169 | //! Maximum number of custom effects. 170 | const RZSIZE MAX_CUSTOM_EFFECTS = MAX_KEYS; 171 | 172 | //! Keyboard grid layout. 173 | const COLORREF RZKEY_GRID_LAYOUT[MAX_ROW][MAX_COLUMN] = {}; 174 | 175 | //! Chroma keyboard effect types 176 | typedef enum EFFECT_TYPE 177 | { 178 | CHROMA_NONE = 0, //!< No effect. 179 | CHROMA_WAVE, //!< Wave effect. 180 | CHROMA_SPECTRUMCYCLING, //!< Spectrum cycling effect. 181 | CHROMA_BREATHING, //!< Breathing effect. 182 | CHROMA_REACTIVE, //!< Reactive effect. 183 | CHROMA_STATIC, //!< Static effect. 184 | CHROMA_INVALID //!< Invalid effect. 185 | } EFFECT_TYPE; 186 | 187 | // Chroma keyboard effects 188 | //! Wave effect type 189 | typedef struct WAVE_EFFECT_TYPE 190 | { 191 | //! Direction of the wave effect. 192 | enum Direction 193 | { 194 | DIRECTION_NONE=0, //!< No direction. 195 | DIRECTION_LEFT_TO_RIGHT, //!< Left to right. 196 | DIRECTION_RIGHT_TO_LEFT, //!< Right to left. 197 | DIRECTION_INVALID //!< Invalid direction. 198 | } Direction; //!< Direction of the wave. 199 | } WAVE_EFFECT_TYPE; 200 | 201 | //! Breathing effect type 202 | typedef struct BREATHING_EFFECT_TYPE 203 | { 204 | COLORREF Color1; //!< First color. 205 | COLORREF Color2; //!< Second color. 206 | } BREATHING_EFFECT_TYPE; 207 | 208 | //! Reactive effect type 209 | typedef struct REACTIVE_EFFECT_TYPE 210 | { 211 | COLORREF Color; //!< Color of the effect 212 | 213 | //! Duration of the effect. 214 | enum Duration 215 | { 216 | DURATION_NONE=0, //!< No duration. 217 | DURATION_SHORT, //!< Short duration. 218 | DURATION_MEDIUM, //!< Medium duration. 219 | DURATION_LONG, //!< Long duration. 220 | DURATION_INVALID //!< Invalid duraiont 221 | } Duration; //!< The time taken for the effect to fade away. 222 | } REACTIVE_EFFECT_TYPE; 223 | 224 | //! Static effect type 225 | typedef struct STATIC_EFFECT_TYPE 226 | { 227 | COLORREF Color; //!< Color of the effect 228 | } STATIC_EFFECT_TYPE; 229 | 230 | //! Custom effect using RZKEY type 231 | typedef struct CUSTOM_EFFECT_TYPE 232 | { 233 | RZKEY Key; //!< The key to apply customized effect. 234 | COLORREF Color; //!< Color of the key. 235 | } CUSTOM_EFFECT_TYPE; 236 | 237 | //! Custom effect using a grid type 238 | typedef struct CUSTOM_GRID_EFFECT_TYPE 239 | { 240 | COLORREF Key[MAX_ROW][MAX_COLUMN]; //!< Grid layout. 6 rows by 22 columns. 241 | } CUSTOM_GRID_EFFECT_TYPE; 242 | } 243 | 244 | namespace Mouse 245 | { 246 | //! Mouse LED Id defintion. 247 | typedef enum RZLED 248 | { 249 | RZLED_NONE = 0, //!< No LED. 250 | RZLED_SCROLLWHEEL = 0x0001, //!< Scroll Wheel LED. 251 | RZLED_LOGO = 0x0002, //!< Logo LED. 252 | RZLED_INVALID = 0xFFFF //!< Invalid LED. 253 | } RZLED; 254 | 255 | //! Chroma mouse effect types 256 | typedef enum EFFECT_TYPE 257 | { 258 | CHROMA_NONE = 0, //!< No effect. 259 | CHROMA_SPECTRUMCYCLING, //!< Spectrum cycling effect. 260 | CHROMA_BREATHING, //!< Breathing effect. 261 | CHROMA_STATIC, //!< Static effect. 262 | CHROMA_CUSTOM, 263 | CHROMA_INVALID //!< Invalid effect. 264 | } EFFECT_TYPE; 265 | 266 | //! Breathing effect type. 267 | typedef struct BREATHING_EFFECT_TYPE 268 | { 269 | COLORREF Color; //!< Color. 270 | } BREATHING_EFFECT_TYPE; 271 | 272 | //! Static effect type 273 | typedef struct STATIC_EFFECT_TYPE 274 | { 275 | COLORREF Color; //!< Color of the effect. 276 | } STATIC_EFFECT_TYPE; 277 | 278 | //! Custome effect type. 279 | typedef struct CUSTOM_EFFECT_TYPE 280 | { 281 | DWORD LED; //!< LED identifier. 282 | COLORREF Color; //!< Color of the LED(s). 283 | } CUSTOM_EFFECT_TYPE; 284 | } 285 | } 286 | 287 | #endif 288 | -------------------------------------------------------------------------------- /RzErrors.h: -------------------------------------------------------------------------------- 1 | 2 | //! \file RzErrors.h 3 | //! \brief Error codes. 4 | 5 | #ifndef _RZERRORS_H_ 6 | #define _RZERRORS_H_ 7 | 8 | #pragma once 9 | 10 | // Error codes 11 | //! Invalid 12 | #define RZRESULT_INVALID -1L 13 | //! Success 14 | #define RZRESULT_SUCCESS 0L 15 | //! Access denied 16 | #define RZRESULT_ACCESS_DENIED 5L 17 | //! Not supported 18 | #define RZRESULT_NOT_SUPPORTED 50L 19 | //! Invalid parameter. 20 | #define RZRESULT_INVALID_PARAMETER 87L 21 | //! Cannot start more than one instance of the specified program. 22 | #define RZRESULT_SINGLE_INSTANCE_APP 1152L 23 | //! Request aborted. 24 | #define RZRESULT_REQUEST_ABORTED 1235L 25 | //! Resource not available or disabled 26 | #define RZRESULT_RESOURCE_DISABLED 4309L 27 | //! General failure. 28 | #define RZRESULT_FAILED 2147500037L 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /chuck_fft.c: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // name: chuck_fft.c 3 | // desc: fft impl - based on CARL distribution 4 | // 5 | // authors: code from San Diego CARL package 6 | // Ge Wang (gewang@cs.princeton.edu) 7 | // Perry R. Cook (prc@cs.princeton.edu) 8 | // date: 11.27.2003 9 | //----------------------------------------------------------------------------- 10 | #include "chuck_fft.h" 11 | #include 12 | #include 13 | 14 | 15 | 16 | 17 | //----------------------------------------------------------------------------- 18 | // name: hanning() 19 | // desc: make window 20 | //----------------------------------------------------------------------------- 21 | void hanning( float * window, unsigned long length ) 22 | { 23 | unsigned long i; 24 | double pi, phase = 0, delta; 25 | 26 | pi = 4.*atan(1.0); 27 | delta = 2 * pi / (double) length; 28 | 29 | for( i = 0; i < length; i++ ) 30 | { 31 | window[i] = (float)(0.5 * (1.0 - cos(phase))); 32 | phase += delta; 33 | } 34 | } 35 | 36 | 37 | 38 | 39 | //----------------------------------------------------------------------------- 40 | // name: hamming() 41 | // desc: make window 42 | //----------------------------------------------------------------------------- 43 | void hamming( float * window, unsigned long length ) 44 | { 45 | unsigned long i; 46 | double pi, phase = 0, delta; 47 | 48 | pi = 4.*atan(1.0); 49 | delta = 2 * pi / (double) length; 50 | 51 | for( i = 0; i < length; i++ ) 52 | { 53 | window[i] = (float)(0.54 - .46*cos(phase)); 54 | phase += delta; 55 | } 56 | } 57 | 58 | 59 | 60 | //----------------------------------------------------------------------------- 61 | // name: blackman() 62 | // desc: make window 63 | //----------------------------------------------------------------------------- 64 | void blackman( float * window, unsigned long length ) 65 | { 66 | unsigned long i; 67 | double pi, phase = 0, delta; 68 | 69 | pi = 4.*atan(1.0); 70 | delta = 2 * pi / (double) length; 71 | 72 | for( i = 0; i < length; i++ ) 73 | { 74 | window[i] = (float)(0.42 - .5*cos(phase) + .08*cos(2*phase)); 75 | phase += delta; 76 | } 77 | } 78 | 79 | 80 | 81 | 82 | //----------------------------------------------------------------------------- 83 | // name: apply_window() 84 | // desc: apply a window to data 85 | //----------------------------------------------------------------------------- 86 | void apply_window( float * data, float * window, unsigned long length ) 87 | { 88 | unsigned long i; 89 | 90 | for( i = 0; i < length; i++ ) 91 | data[i] *= window[i]; 92 | } 93 | 94 | static float PI ; 95 | static float TWOPI ; 96 | void bit_reverse( float * x, long N ); 97 | 98 | //----------------------------------------------------------------------------- 99 | // name: rfft() 100 | // desc: real value fft 101 | // 102 | // these routines from the CARL software, spect.c 103 | // check out the CARL CMusic distribution for more source code 104 | // 105 | // if forward is true, rfft replaces 2*N real data points in x with N complex 106 | // values representing the positive frequency half of their Fourier spectrum, 107 | // with x[1] replaced with the real part of the Nyquist frequency value. 108 | // 109 | // if forward is false, rfft expects x to contain a positive frequency 110 | // spectrum arranged as before, and replaces it with 2*N real values. 111 | // 112 | // N MUST be a power of 2. 113 | // 114 | //----------------------------------------------------------------------------- 115 | void rfft( float * x, long N, unsigned int forward ) 116 | { 117 | static int first = 1 ; 118 | float c1, c2, h1r, h1i, h2r, h2i, wr, wi, wpr, wpi, temp, theta ; 119 | float xr, xi ; 120 | long i, i1, i2, i3, i4, N2p1 ; 121 | 122 | if( first ) 123 | { 124 | PI = (float) (4.*atan( 1. )) ; 125 | TWOPI = (float) (8.*atan( 1. )) ; 126 | first = 0 ; 127 | } 128 | 129 | theta = PI/N ; 130 | wr = 1. ; 131 | wi = 0. ; 132 | c1 = 0.5 ; 133 | 134 | if( forward ) 135 | { 136 | c2 = -0.5 ; 137 | cfft( x, N, forward ) ; 138 | xr = x[0] ; 139 | xi = x[1] ; 140 | } 141 | else 142 | { 143 | c2 = 0.5 ; 144 | theta = -theta ; 145 | xr = x[1] ; 146 | xi = 0. ; 147 | x[1] = 0. ; 148 | } 149 | 150 | wpr = (float) (-2.*pow( sin( 0.5*theta ), 2. )) ; 151 | wpi = (float) sin( theta ) ; 152 | N2p1 = (N<<1) + 1 ; 153 | 154 | for( i = 0 ; i <= N>>1 ; i++ ) 155 | { 156 | i1 = i<<1 ; 157 | i2 = i1 + 1 ; 158 | i3 = N2p1 - i2 ; 159 | i4 = i3 + 1 ; 160 | if( i == 0 ) 161 | { 162 | h1r = c1*(x[i1] + xr ) ; 163 | h1i = c1*(x[i2] - xi ) ; 164 | h2r = -c2*(x[i2] + xi ) ; 165 | h2i = c2*(x[i1] - xr ) ; 166 | x[i1] = h1r + wr*h2r - wi*h2i ; 167 | x[i2] = h1i + wr*h2i + wi*h2r ; 168 | xr = h1r - wr*h2r + wi*h2i ; 169 | xi = -h1i + wr*h2i + wi*h2r ; 170 | } 171 | else 172 | { 173 | h1r = c1*(x[i1] + x[i3] ) ; 174 | h1i = c1*(x[i2] - x[i4] ) ; 175 | h2r = -c2*(x[i2] + x[i4] ) ; 176 | h2i = c2*(x[i1] - x[i3] ) ; 177 | x[i1] = h1r + wr*h2r - wi*h2i ; 178 | x[i2] = h1i + wr*h2i + wi*h2r ; 179 | x[i3] = h1r - wr*h2r + wi*h2i ; 180 | x[i4] = -h1i + wr*h2i + wi*h2r ; 181 | } 182 | 183 | wr = (temp = wr)*wpr - wi*wpi + wr ; 184 | wi = wi*wpr + temp*wpi + wi ; 185 | } 186 | 187 | if( forward ) 188 | x[1] = xr ; 189 | else 190 | cfft( x, N, forward ) ; 191 | } 192 | 193 | 194 | 195 | 196 | //----------------------------------------------------------------------------- 197 | // name: cfft() 198 | // desc: complex value fft 199 | // 200 | // these routines from CARL software, spect.c 201 | // check out the CARL CMusic distribution for more software 202 | // 203 | // cfft replaces float array x containing NC complex values (2*NC float 204 | // values alternating real, imagininary, etc.) by its Fourier transform 205 | // if forward is true, or by its inverse Fourier transform ifforward is 206 | // false, using a recursive Fast Fourier transform method due to 207 | // Danielson and Lanczos. 208 | // 209 | // NC MUST be a power of 2. 210 | // 211 | //----------------------------------------------------------------------------- 212 | void cfft( float * x, long NC, unsigned int forward ) 213 | { 214 | float wr, wi, wpr, wpi, theta, scale ; 215 | long mmax, ND, m, i, j, delta ; 216 | ND = NC<<1 ; 217 | bit_reverse( x, ND ) ; 218 | 219 | for( mmax = 2 ; mmax < ND ; mmax = delta ) 220 | { 221 | delta = mmax<<1 ; 222 | theta = TWOPI/( forward? mmax : -mmax ) ; 223 | wpr = (float) (-2.*pow( sin( 0.5*theta ), 2. )) ; 224 | wpi = (float) sin( theta ) ; 225 | wr = 1. ; 226 | wi = 0. ; 227 | 228 | for( m = 0 ; m < mmax ; m += 2 ) 229 | { 230 | register float rtemp, itemp ; 231 | for( i = m ; i < ND ; i += delta ) 232 | { 233 | j = i + mmax ; 234 | rtemp = wr*x[j] - wi*x[j+1] ; 235 | itemp = wr*x[j+1] + wi*x[j] ; 236 | x[j] = x[i] - rtemp ; 237 | x[j+1] = x[i+1] - itemp ; 238 | x[i] += rtemp ; 239 | x[i+1] += itemp ; 240 | } 241 | 242 | wr = (rtemp = wr)*wpr - wi*wpi + wr ; 243 | wi = wi*wpr + rtemp*wpi + wi ; 244 | } 245 | } 246 | 247 | // scale output 248 | scale = (float)(forward ? 1./ND : 2.) ; 249 | { 250 | register float *xi=x, *xe=x+ND ; 251 | while( xi < xe ) 252 | *xi++ *= scale ; 253 | } 254 | } 255 | 256 | 257 | 258 | 259 | //----------------------------------------------------------------------------- 260 | // name: bit_reverse() 261 | // desc: bitreverse places float array x containing N/2 complex values 262 | // into bit-reversed order 263 | //----------------------------------------------------------------------------- 264 | void bit_reverse( float * x, long N ) 265 | { 266 | float rtemp, itemp ; 267 | long i, j, m ; 268 | for( i = j = 0 ; i < N ; i += 2, j += m ) 269 | { 270 | if( j > i ) 271 | { 272 | rtemp = x[j] ; itemp = x[j+1] ; /* complex exchange */ 273 | x[j] = x[i] ; x[j+1] = x[i+1] ; 274 | x[i] = rtemp ; x[i+1] = itemp ; 275 | } 276 | 277 | for( m = N>>1 ; m >= 2 && j >= m ; m >>= 1 ) 278 | j -= m ; 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /chuck_fft.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // name: chuck_fft.h 3 | // desc: fft impl - based on CARL distribution 4 | // 5 | // authors: code from San Diego CARL package 6 | // Ge Wang (gewang@cs.princeton.edu) 7 | // Perry R. Cook (prc@cs.princeton.edu) 8 | // date: 11.27.2003 9 | //----------------------------------------------------------------------------- 10 | #ifndef __CHUCK_FFT_H__ 11 | #define __CHUCK_FFT_H__ 12 | 13 | 14 | // complex type 15 | typedef struct { float re ; float im ; } complex; 16 | 17 | // complex absolute value 18 | #define cmp_abs(x) ( sqrt( (x).re * (x).re + (x).im * (x).im ) ) 19 | 20 | #define FFT_FORWARD 1 21 | #define FFT_INVERSE 0 22 | 23 | // c linkage 24 | #if ( defined( __cplusplus ) || defined( _cplusplus ) ) 25 | extern "C" { 26 | #endif 27 | 28 | // make the window 29 | void hanning( float * window, unsigned long length ); 30 | void hamming( float * window, unsigned long length ); 31 | void blackman( float * window, unsigned long length ); 32 | // apply the window 33 | void apply_window( float * data, float * window, unsigned long length ); 34 | 35 | // real fft, N must be power of 2 36 | void rfft( float * x, long N, unsigned int forward ); 37 | // complex fft, NC must be power of 2 38 | void cfft( float * x, long NC, unsigned int forward ); 39 | 40 | // c linkage 41 | #if ( defined( __cplusplus ) || defined( _cplusplus ) ) 42 | } 43 | #endif 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /fanbus.cpp: -------------------------------------------------------------------------------- 1 | //FanBus Protocol Files 2 | #include "fanbus.h" 3 | fanbus::fanbus() 4 | { 5 | FanBusPort = (serial_port*)NULL; 6 | } 7 | 8 | fanbus::fanbus(serial_port* fanPort) 9 | { 10 | FanBusPort = fanPort; 11 | } 12 | 13 | void fanbus::fanbus_write(unsigned char int_addr, unsigned char dev_addr, unsigned char val) 14 | { 15 | char fan_cmd[] = {0x00, int_addr, dev_addr, val, 0xFF}; 16 | FanBusPort->serial_write(fan_cmd, 5); 17 | //Flush the serial read buffer 18 | FanBusPort->serial_flush_rx(); 19 | } 20 | 21 | int fanbus::fanbus_read(unsigned char int_addr, unsigned char dev_addr) 22 | { 23 | char fan_cmd[6] = {0x01, int_addr, dev_addr, 0x00, 0xFF}; 24 | FanBusPort->serial_write(fan_cmd, 5); 25 | if(FanBusPort->serial_read(fan_cmd, 6) > 5) 26 | { 27 | return fan_cmd[5]; 28 | } 29 | else 30 | { 31 | return -1; 32 | } 33 | } 34 | 35 | void fanbus::fanbus_set_port(serial_port* newPort) 36 | { 37 | FanBusPort = newPort; 38 | } 39 | 40 | unsigned char fanbus::fanbus_scan_devices() 41 | { 42 | device_count = 0; 43 | for(unsigned char i = 0x03; i < 0xFF; i++) 44 | { 45 | printf("FanBusScan: Looking for FanBus device at Address %02X.\n", i); 46 | if(fanbus_read(0x00, i) >= 0) 47 | { 48 | printf("FanBusScan: Found device at Address %02X\n", i); 49 | device_list[device_count] = i; 50 | device_count++; 51 | } 52 | } 53 | //printf("Val: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",read_buf[0],read_buf[1],read_buf[2],read_buf[3],read_buf[4],read_buf[5]); 54 | printf("FanBusScan: Complete, found %d devices.\n", device_count); 55 | return device_count; 56 | } 57 | -------------------------------------------------------------------------------- /fanbus.h: -------------------------------------------------------------------------------- 1 | #ifndef FANBUS_H 2 | #define FANBUS_H 3 | 4 | #include "serial_port.h" 5 | 6 | class fanbus 7 | { 8 | public: 9 | //FanBus Constructor 10 | fanbus(); 11 | fanbus(serial_port* fanPort); 12 | 13 | //FanBus Register Read and Write 14 | void fanbus_write(unsigned char int_addr, unsigned char dev_addr, unsigned char val); 15 | int fanbus_read (unsigned char int_addr, unsigned char dev_addr); 16 | 17 | //FanBus Set Serial Port 18 | void fanbus_set_port(serial_port* newPort); 19 | 20 | //FanBus Device Scan 21 | unsigned char fanbus_scan_devices(); 22 | 23 | unsigned char device_list[252]; 24 | unsigned char device_count; 25 | 26 | private: 27 | serial_port* FanBusPort; 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /serial_port.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------*\ 2 | | Cross Platform Serial COM Library for Windows and Linux | 3 | | This library provides access to serial ports with a | 4 | | common API for both Windows and Linux systems. It | 5 | | features read and write as well as tx/rx buffer flush. | 6 | | | 7 | | Adam Honse (calcprogrammer1@gmail.com), 1/21/2013 | 8 | \*---------------------------------------------------------*/ 9 | 10 | #include "serial_port.h" 11 | 12 | //serial_port (constructor) 13 | // The default constructor does not initialize the serial port 14 | serial_port::serial_port() 15 | { 16 | //Set a default baud rate 17 | baud_rate = 9600; 18 | } 19 | 20 | //serial_port (constructor) 21 | // When created with port information, the constructor 22 | // will automatically open port at baud rate 23 | serial_port::serial_port(const char * name, unsigned int baud) 24 | { 25 | serial_open(name, baud); 26 | } 27 | 28 | //~serial_port (destructor) 29 | // Closes the port before destroying the object 30 | serial_port::~serial_port() 31 | { 32 | serial_close(); 33 | } 34 | 35 | //open 36 | // Opens the serial port using stored information 37 | // Sets the baud rate to the stored baud rate 38 | // 8 data bits, no parity, one stop bit 39 | bool serial_port::serial_open() 40 | { 41 | printf("SerialPort: Opening serial port %s at baud rate %d.\n", port_name, baud_rate); 42 | 43 | #ifdef WIN32 44 | file_descriptor = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); 45 | 46 | if((int)file_descriptor < 0) 47 | { 48 | printf("SerialPort: Port %s could not be opened: %d.\n", port_name, file_descriptor); 49 | return false; 50 | } 51 | 52 | SetupComm(file_descriptor, 1, 128); 53 | GetCommState(file_descriptor, &dcb); 54 | 55 | dcb.BaudRate = baud_rate; //Set baud rate 56 | dcb.ByteSize = 8; //8 data bits 57 | dcb.Parity = NOPARITY; //Parity = none 58 | dcb.StopBits = ONESTOPBIT; //One stop bit 59 | dcb.fAbortOnError = TRUE; //Abort on error 60 | dcb.fOutX = FALSE; //XON/XOFF off for transmit 61 | dcb.fInX = FALSE; //XON/XOFF off for receive 62 | dcb.fOutxCtsFlow = FALSE; //Turn off CTS flow control 63 | dcb.fRtsControl = RTS_CONTROL_DISABLE; //Options DISABLE, ENABLE, HANDSHAKE 64 | dcb.fOutxDsrFlow = FALSE; //Turn off DSR flow control 65 | dcb.fDtrControl = DTR_CONTROL_DISABLE; //Disable DTR control 66 | 67 | SetCommState(file_descriptor, &dcb); 68 | 69 | COMMTIMEOUTS timeouts = {0}; 70 | timeouts.ReadIntervalTimeout = 50; 71 | timeouts.ReadTotalTimeoutConstant=50; 72 | timeouts.ReadTotalTimeoutMultiplier=10; 73 | timeouts.WriteTotalTimeoutConstant=50; 74 | timeouts.WriteTotalTimeoutMultiplier=10; 75 | SetCommTimeouts(file_descriptor, &timeouts); 76 | 77 | #else 78 | 79 | file_descriptor = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY); 80 | 81 | if(file_descriptor < 0) 82 | { 83 | printf("SerialPort: Port %s could not be opened: %d.\n", port_name, file_descriptor); 84 | return false; 85 | } 86 | 87 | serial_struct ss; 88 | ioctl(file_descriptor, TIOCGSERIAL, &ss); 89 | ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST | ASYNCB_LOW_LATENCY; 90 | ss.custom_divisor = (ss.baud_base + (baud_rate / 2)) / baud_rate; 91 | int closestSpeed = ss.baud_base / ss.custom_divisor; 92 | 93 | if((float)closestSpeed < ((float)baud_rate * (98.0f/100.0f)) || (float)closestSpeed > ((float)baud_rate * (102.0f/100.0f))) 94 | { 95 | printf("SerialPort: Cannot set %s to %d. Closest possible speed is %d.\n", port_name, baud_rate, closestSpeed); 96 | } 97 | else 98 | { 99 | printf("SerialPort: %s speed set to %d.\n", port_name, baud_rate); 100 | } 101 | 102 | fcntl(file_descriptor, F_SETFL, 0); 103 | #endif 104 | 105 | printf("SerialPort: Serial port %s opened successfully.\n", port_name); 106 | return true; 107 | } 108 | 109 | //open 110 | // Opens the serial port without changing stored baud rate 111 | bool serial_port::serial_open(const char * name) 112 | { 113 | return serial_open(name, baud_rate); 114 | } 115 | 116 | //open 117 | // Opens the serial port at baud rate 118 | bool serial_port::serial_open(const char* name, unsigned int baud) 119 | { 120 | strcpy(port_name, name); 121 | baud_rate = baud; 122 | return serial_open(); 123 | } 124 | 125 | //close 126 | // Closes the serial port 127 | void serial_port::serial_close() 128 | { 129 | printf("SerialPort: Closing port %s.\n", port_name); 130 | #ifdef WIN32 131 | 132 | #else 133 | 134 | close(file_descriptor); 135 | #endif 136 | } 137 | 138 | // read 139 | // Reads bytes from the serial port into 140 | // Returns the number of bytes actually read 141 | // If less than bytes are available, it will read all 142 | // available bytes 143 | int serial_port::serial_read(char * buffer, int length) 144 | { 145 | #ifdef WIN32 146 | DWORD bytesread; 147 | ReadFile(file_descriptor, buffer, length, &bytesread, NULL); 148 | 149 | #else 150 | 151 | int bytesread; 152 | bytesread = read(file_descriptor, buffer, length); 153 | #endif 154 | 155 | //printf("SerialPort: Read %d bytes on port %s.\n", bytesread, port_name); 156 | return bytesread; 157 | } 158 | 159 | //write 160 | // Writes bytes to the serial port from 161 | // Returns the number of bytes actually written 162 | // Does not check for null-termination, so if is 163 | // greater than the number of bytes in , it will read 164 | // past and may cause a segfault 165 | int serial_port::serial_write(char * buffer, int length) 166 | { 167 | #ifdef WIN32 168 | DWORD byteswritten; 169 | WriteFile(file_descriptor, buffer, length, &byteswritten, NULL); 170 | 171 | #else 172 | 173 | int byteswritten; 174 | byteswritten = write(file_descriptor, buffer, length); 175 | #endif 176 | 177 | //printf("SerialPort: Wrote %d bytes on port %s.\n", byteswritten, port_name); 178 | return byteswritten; 179 | } 180 | 181 | //flush 182 | void serial_port::serial_flush_rx() 183 | { 184 | #ifdef WIN32 185 | PurgeComm(file_descriptor, PURGE_RXABORT | PURGE_RXCLEAR); 186 | 187 | #else 188 | 189 | tcflush(file_descriptor, TCIFLUSH); 190 | #endif 191 | } 192 | 193 | void serial_port::serial_flush_tx() 194 | { 195 | 196 | #ifdef WIN32 197 | PurgeComm(file_descriptor, PURGE_TXABORT | PURGE_TXCLEAR); 198 | 199 | #else 200 | 201 | tcflush(file_descriptor, TCOFLUSH); 202 | #endif 203 | } 204 | -------------------------------------------------------------------------------- /serial_port.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------*\ 2 | | Cross Platform Serial COM Library for Windows and Linux | 3 | | This library provides access to serial ports with a | 4 | | common API for both Windows and Linux systems. It | 5 | | features read and write as well as tx/rx buffer flush. | 6 | | | 7 | | Adam Honse (calcprogrammer1@gmail.com), 1/21/2013 | 8 | \*---------------------------------------------------------*/ 9 | 10 | #ifndef SERIAL_PORT_H 11 | #define SERIAL_PORT_H 12 | 13 | #include 14 | #include 15 | 16 | #ifdef WIN32 17 | #include 18 | 19 | #else 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #endif 27 | 28 | //Serial Port Class 29 | //The reason for this class is that serial ports are treated differently 30 | //on Windows and Linux. By creating a class, those differences can be 31 | //made invisible to the program and make cross-platform usage easy 32 | 33 | class serial_port 34 | { 35 | public: 36 | serial_port(); 37 | serial_port(const char * name, unsigned int baud); 38 | 39 | ~serial_port(); 40 | 41 | //Function to open the port 42 | bool serial_open(); 43 | bool serial_open(const char* name); 44 | bool serial_open(const char* name, unsigned int baud); 45 | 46 | //Function to close the port 47 | void serial_close(); 48 | 49 | //Functions for controlling baud rate 50 | void serial_set_baud(unsigned int baud); 51 | int serial_get_baud(); 52 | 53 | //Function to read data from the port buffer 54 | int serial_read(char * buffer, int length); 55 | 56 | //Function to write data to the serial port 57 | int serial_write(char * buffer, int length); 58 | 59 | //Functions to flush the serial port rx and tx buffers 60 | void serial_flush_rx(); 61 | void serial_flush_tx(); 62 | 63 | //Function to list the number of available bytes 64 | int serial_available(); 65 | 66 | private: 67 | char port_name[1024]; 68 | unsigned int baud_rate; 69 | 70 | #ifdef WIN32 71 | HANDLE file_descriptor; 72 | DCB dcb; 73 | 74 | #else 75 | 76 | int file_descriptor; 77 | #endif 78 | }; 79 | 80 | #endif 81 | --------------------------------------------------------------------------------