cphidgettextled.c
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include "cphidgettextled.h"
00003 #include "cusb.h"
00004 #include "csocket.h"
00005 #include "cthread.h"
00006 
00007 // === Internal Functions === //
00008 
00009 //clearVars - sets all device variables to unknown state
00010 CPHIDGETCLEARVARS(TextLED)
00011 
00012         phid->brightness = PUNK_INT;
00013 
00014         return EPHIDGET_OK;
00015 }
00016 
00017 //initAfterOpen - sets up the initial state of an object, reading in packets from the device if needed
00018 //                                used during attach initialization - on every attach
00019 CPHIDGETINIT(TextLED)
00020         TESTPTR(phid);
00021 
00022         //Make sure no old writes are still pending
00023         phid->outputPacketLen = 0;
00024 
00025         //set variables to unknown
00026         phid->brightness = PUNK_INT;
00027 
00028         return EPHIDGET_OK;
00029 }
00030 
00031 //dataInput - parses device packets - not used
00032 CPHIDGETDATA(TextLED)
00033         phid = 0;
00034         return EPHIDGET_OK;
00035 }
00036 
00037 //eventsAfterOpen - sends out an event for all valid data, used during attach initialization - not used
00038 CPHIDGETINITEVENTS(TextLED)
00039         phid = 0;
00040         return EPHIDGET_OK;
00041 }
00042 
00043 //getPacket - used by write thread to get the next packet to send to device
00044 CGETPACKET_BUF(TextLED)
00045 
00046 //sendpacket - sends a packet to the device asynchronously, blocking if the 1-packet queue is full
00047 CSENDPACKET_BUF(TextLED)
00048 
00049 const unsigned long LED_Brightness[33] = {
00050         0x00000000,
00051         0x00000001,
00052         0x00010001,
00053         0x01010001,
00054 
00055         0x01010101,
00056         0x01110101,
00057         0x01110111,
00058         0x11110111,
00059 
00060         0x11111111,
00061         0x11111115,
00062         0x11151115,
00063         0x15151115,
00064 
00065         0x15151515,
00066         0x55151515,
00067         0x55155515,
00068         0x55555515,
00069 
00070         0x55555555,
00071         0x55555557,
00072         0x55575557,
00073         0x57575557,
00074 
00075         0x57575757,
00076         0x77575757,
00077         0x77775757,
00078         0x77777757,
00079 
00080         0x77777777,
00081         0x7777777F,
00082         0x777F777F,
00083         0x7F7F777F,
00084         0x7F7F7F7F,
00085 
00086         0x7FFF7F7F,
00087         0x7FFF7FFF,
00088         0x7FFFFFFF,
00089         0xFFFFFFFF};
00090 
00091 //BL: Is this the same mapping as the LCD custom chars?
00092 const unsigned char LED_Translate[256] = {
00093         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00094         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00095         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00096         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00097 
00098         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00099         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
00100         0x7E, 0x42, 0x3B, 0x6B, 0x47, 0x6D, 0x7D, 0x4A,
00101         0x7F, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00102 
00103         0x00, 0x5F, 0x7F, 0x3C, 0x73, 0x3D, 0x1D, 0x7C, /* A,B,C,D,E,F,G */
00104         0x57, 0x42, 0x72, 0x57, 0x34, 0x5E, 0x5E, 0x7E, /* H, I, J, K, L, M, N, O */
00105         0x1F, 0x7E, 0x5F, 0x6D, 0x1C, 0x76, 0x76, 0x76, /* P, Q, R, S, T, U, V, W */
00106         0x57, 0x67, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, /* X, Y, Z */
00107 
00108         0x00, 0x5F, 0x7F, 0x3C, 0x73, 0x3D, 0x1D, 0x7C, /* A,B,C,D,E,F,G */
00109         0x57, 0x42, 0x72, 0x57, 0x34, 0x5E, 0x5E, 0x7E, /* H, I, J, K, L, M, N, O */
00110         0x1F, 0x7E, 0x5F, 0x6D, 0x1C, 0x76, 0x76, 0x76, /* P, Q, R, S, T, U, V, W */
00111         0x57, 0x67, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, /* X, Y, Z */
00112 
00113         0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 
00114         0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 
00115         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
00116         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 
00117 
00118         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 
00119         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 
00120         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 
00121         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 
00122 
00123         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 
00124         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 
00125         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 
00126         0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 
00127 
00128         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 
00129         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 
00130         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 
00131         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f};
00132 
00133 
00134 const unsigned char     LEDV2_Table[512] = {
00135         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00136         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00137         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00138         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0 - 0x0F
00139 
00140         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00141         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00142         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00143         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x1F
00144 
00145         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00146         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00147         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00148         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x2F
00149 
00150         46, 177, 18, 48, 98, 131, 98, 178, 
00151         70, 34, 108, 178, 108, 179, 48, 8, 
00152         110, 179, 110, 178, 1, 4, 1, 8, 
00153         16, 64, 0, 0, 128, 8, 110, 50,                             // 0x30 - 0x3F 
00154 
00155         62, 147, 110, 35, 110, 179, 44, 129,
00156         46, 177, 108, 131, 108, 3, 44,179,
00157         78, 51, 33, 132, 34, 177, 20, 73,
00158         12, 129, 47, 53, 142, 113, 46, 177,                       // 0x40 - 0x4F 
00159 
00160         110, 3, 110, 82, 110, 67, 108, 178,
00161         41,4, 70, 179, 20, 9, 6, 181,
00162         144, 72, 144, 4, 48, 136, 36, 129, 
00163         128, 64, 34, 160, 0, 72, 0, 128,                        // 0x50 - 0x5F 
00164 
00165         128, 0, 64, 179, 68, 179, 64, 131,
00166         2, 186, 64, 139, 108, 1, 18, 178,
00167         68, 35, 0, 4, 2, 112, 17, 68,
00168         4, 1, 64,55, 64,5, 64, 163, 204, 1,                     // 0x60 - 0x6F 
00169 
00170         204, 8, 64, 1, 0, 0, 65, 6, 
00171         0, 161, 0, 9, 0, 165, 144, 72,
00172         144, 8, 64, 136, 0, 0, 0, 0,                            // 0x70 - 0x7F 
00173 
00174         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00175         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00176         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00177         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 - 0x8F 
00178 
00179         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00180         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00181         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00182         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 - 0x9F 
00183 
00184         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00185         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00186         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00187         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 - 0xAF 
00188 
00189         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00190         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00191         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00192         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 - 0xBF 
00193 
00194         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00195         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00196         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00197         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 - 0xCF 
00198 
00199         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00200         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00201         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00202         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 - 0xDF 
00203 
00204         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00205         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00206         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00207         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 - 0xEF 
00208 
00209         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00210         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00211         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00212         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 0xF0 - 0xFF 
00213 
00214 
00215 /*
00216 A = 102, 35
00217 B = 110, 79
00218 C = 44, 129
00219 D = 46, 177
00220 E = 108,131
00221 F = 108, 3
00222 G = 44,179
00223 H = 78, 51
00224 I = 33, 132
00225 J = 34, 177
00226 K = 20, 73
00227 L = 12, 129
00228 M = 47, 53
00229 N = 142, 113
00230 O = 46, 177
00231 P = 110, 3
00232 Q = 110, 82
00233 R = 110, 67
00234 S = 108,178
00235 T = 41,4
00236 U = 66, 177
00237 V = 20, 9
00238 W = 6, 165
00239 X = 144, 72
00240 Y = 144, 4
00241 Z = 48, 136
00242 
00243 0 = 46,177
00244 1 = 18,48
00245 2 = 98, 131
00246 3 = 98,178
00247 4 = 70, 34
00248 5 = 108, 178
00249 6 = 108, 179
00250 7 = 48,136
00251 8 = 110, 179
00252 9 = 110,178
00253 
00254 a = 64, 179
00255 b = 68, 179
00256 c = 64, 131
00257 d = 2, 186
00258 e = 64, 139
00259 f = 108, 129
00260 g = 18, 178
00261 h = 68, 35
00262 i = 0, 04
00263 j = 2, 112
00264 k = 17, 68
00265 l = 4, 1
00266 m = 64,55
00267 n = 64,5
00268 o = 64, 163
00269 p = 204, 1
00270 q = 204, 8
00271 r = 64, 1
00272 s = 
00273 t = 65, 6
00274 u = 0, 161
00275 v = 0, 9
00276 w = 0, 165
00277 x = 144, 72
00278 y = 144, 8
00279 z = 64, 136             
00280                 
00281 " = 3, 0 
00282 ] = 34, 160
00283 [ = 36, 129
00284 ` = 128, 0
00285 ' = 16, 0
00286 % = 24, 24
00287 
00288 */
00289 
00290 //makePacket - constructs a packet using current device state
00291 CMAKEPACKETINDEXED(TextLED)
00292         int brightness = 0;
00293         char displayString[TEXTLED_MAXROWS][TEXTLED_MAXCOLS];
00294         size_t length = 0;
00295         int j, k, correction, result;
00296         unsigned int ui;
00297 
00298         correction = -1;
00299 
00300         TESTPTRS(phid, buffer)
00301 
00302         switch(phid->phid.deviceIDSpec)
00303         {
00304                 case PHIDID_TEXTLED_1x8:
00305                         if ((phid->phid.deviceVersion >= 111) && (phid->phid.deviceVersion < 114))
00306                         {
00307                                 switch(Index)
00308                                 {
00309                                         case TEXTLED_BRIGHTNESS_PACKET:
00310                                                 brightness = round((phid->brightness * 31) / 100.0);
00311 
00312                                                 buffer[0] = 0x1C | (0x04 << 5);
00313                                                 buffer[1] = (unsigned char)(LED_Brightness[brightness] & 0xff);
00314                                                 buffer[2] = (unsigned char)((LED_Brightness[brightness] >> 8) & 0xff);
00315                                                 buffer[3] = (unsigned char)((LED_Brightness[brightness] >> 16) & 0xff);
00316                                                 buffer[4] = (unsigned char)((LED_Brightness[brightness] >> 24) & 0xff);
00317 
00318                                                 break;
00319                                         case TEXTLED_DISPLAYSTRING_PACKET + 0:
00320                                                 length = strlen(phid->displayStringPtr[0]);
00321                                                 correction = 0;
00322 
00323                                                 for (ui = 0; ui<=length; ui++)
00324                                                 {
00325                                                         displayString[0][ui] = LED_Translate[phid->displayStringPtr[0][ui + correction] & 0xff];
00326                                                         if (((phid->displayStringPtr[0][ui + correction] & 0xff) == 0x2E) && (ui > 0)) 
00327                                                         {
00328                                                                 displayString[0][ui-1] |= 0x80;
00329                                                                 correction++;
00330                                                                 displayString[0][ui] = LED_Translate[phid->displayStringPtr[0][ui + correction] & 0xff];
00331                                                         }
00332                                                 }
00333 
00334                                                 //Takes 2 packets to set string, so we have to send the first one here manually
00335 
00336                                                 //3-bit character count, 5-bit position
00337                                                 buffer[0] = ((0x07 << 5) | 0x00); //7 characters at posn 0
00338                                                 memcpy(buffer+1, displayString[0], 7); //the first 7 characters
00339                                                 //send the first 7 characters
00340                                                 if ((result = CPhidgetTextLED_sendpacket(phid, buffer)) != EPHIDGET_OK)
00341                                                 {
00342                                                         return result;
00343                                                 }
00344 
00345                                                 //3-bit character count, 5-bit position
00346                                                 ZEROMEM(buffer, phid->phid.outputReportByteLength);
00347                                                 buffer[0] = ((0x01 << 5) | 0x07); //1 character at posn 7
00348                                                 memcpy(buffer+1, displayString[0]+7, 1); //the 8th character
00349 
00350                                                 break;
00351                                         default:
00352                                                 return EPHIDGET_UNEXPECTED;
00353                                 }
00354                         }
00355                         else
00356                                 return EPHIDGET_UNEXPECTED;
00357                         break;
00358                 case PHIDID_TEXTLED_4x8:
00359                         if ((phid->phid.deviceVersion >= 200) && (phid->phid.deviceVersion < 300))
00360                         {
00361                                 switch(Index)
00362                                 {
00363                                         case TEXTLED_BRIGHTNESS_PACKET:
00364                                                 brightness = round((phid->brightness * 63) / 100.0);
00365 
00366                                                 buffer[0] = 0x80;
00367                                                 buffer[1] = brightness;
00368 
00369                                                 break;
00370                                         case TEXTLED_DISPLAYSTRING_PACKET + 0:
00371                                         case TEXTLED_DISPLAYSTRING_PACKET + 1:
00372                                         case TEXTLED_DISPLAYSTRING_PACKET + 2:
00373                                         case TEXTLED_DISPLAYSTRING_PACKET + 3:
00374                                                 length = strlen(phid->displayStringPtr[Index]);
00375                                                 buffer[0] = (unsigned char)Index;
00376 
00377                                                 for (ui = 0, j = 1, k=8; ui<length; ui++)
00378                                                 {
00379                                                         /* Collapse decimal point into previous digit if possible */
00380                                                         if (phid->displayStringPtr[Index][ui] == '.')
00381                                                         {
00382                                                                 if(buffer[7] & j)
00383                                                                 {
00384                                                                         k+=2;
00385                                                                         j=j<<1;
00386                                                                 }
00387                                                                 buffer[7] |= j;
00388                                                                 if (k == 8) k+=2;
00389                                                         } 
00390                                                         else
00391                                                         {
00392                                                                 buffer[k] = LEDV2_Table[((unsigned short)phid->displayStringPtr[Index][ui] << 1)];
00393                                                                 buffer[k+1] = LEDV2_Table[(((unsigned short)phid->displayStringPtr[Index][ui] << 1) + 1)];
00394                                                                 if (k != 8) j=j<<1;
00395                                                                 k+=2;
00396                                                         }
00397                                                 }
00398                                                 break;
00399                                         default:
00400                                                 return EPHIDGET_UNEXPECTED;
00401                                 }
00402                         }
00403                         else
00404                                 return EPHIDGET_UNEXPECTED;
00405                         break;
00406                 default:
00407                         return EPHIDGET_UNEXPECTED;
00408         }
00409 
00410         return EPHIDGET_OK;
00411 }
00412 
00413 // === Exported Functions === //
00414 
00415 //create and initialize a device structure
00416 CCREATE(TextLED, PHIDCLASS_TEXTLED)
00417 
00418 CGET(TextLED,RowCount,int)
00419         TESTPTRS(phid,pVal) 
00420         TESTDEVICETYPE(PHIDCLASS_TEXTLED)
00421         TESTATTACHED
00422 
00423         MASGN(phid.attr.textled.numRows)
00424 }
00425 CGET(TextLED,ColumnCount,int)
00426         TESTPTRS(phid,pVal) 
00427         TESTDEVICETYPE(PHIDCLASS_TEXTLED)
00428         TESTATTACHED
00429 
00430         MASGN(phid.attr.textled.numColumns)
00431 }
00432 
00433 CGET(TextLED,Brightness,int)
00434         TESTPTRS(phid,pVal) 
00435         TESTDEVICETYPE(PHIDCLASS_TEXTLED)
00436         TESTATTACHED
00437         TESTMASGN(brightness, PUNK_INT)
00438 
00439         MASGN(brightness)
00440 }
00441 CSET(TextLED,Brightness,int)
00442         TESTPTR(phid) 
00443         TESTDEVICETYPE(PHIDCLASS_TEXTLED)
00444         TESTATTACHED
00445         TESTRANGE(0, 100)       
00446 
00447         if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
00448                 ADDNETWORKKEY(Brightness, "%d", brightness);
00449         else
00450                 SENDPACKETINDEXED(TextLED, brightness, TEXTLED_BRIGHTNESS_PACKET);
00451 
00452         return EPHIDGET_OK;
00453 }
00454 
00455 CSETINDEX(TextLED, DisplayString, char *)
00456         size_t length = strlen(newVal);
00457         int i = 0, length_temp = 0;
00458         TESTPTR(phid)
00459         TESTDEVICETYPE(PHIDCLASS_TEXTLED)
00460         TESTATTACHED
00461         TESTINDEX(phid.attr.textled.numRows)
00462 
00463         //for the 4 row textled, periods don't count in the length 
00464         // unless the first char is a period, or there are two in a row
00465         switch(phid->phid.deviceIDSpec)
00466         {
00467                 case PHIDID_TEXTLED_4x8:
00468                         length_temp = (int)length;
00469                         for(i=1;i<length_temp;i++)
00470                                 if(newVal[i] == '.' && newVal[i-1] != '.') length--;
00471                         break;
00472                 default:
00473                         break;
00474         }
00475         if (length > (size_t)phid->phid.attr.textled.numColumns) return EPHIDGET_INVALIDARG;
00476 
00477         if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
00478                 ADDNETWORKKEYINDEXED(DisplayString, "%s", strings);
00479         else
00480                 SENDPACKETINDEXED(TextLED, displayStringPtr[Index], TEXTLED_DISPLAYSTRING_PACKET + Index);
00481 
00482         return EPHIDGET_OK;
00483 }
00484 
00485 // === Deprecated Functions === //
00486 
00487 CGET(TextLED,NumRows,int)
00488         return CPhidgetTextLED_getRowCount(phid, pVal);
00489 }
00490 CGET(TextLED,NumColumns,int)
00491         return CPhidgetTextLED_getColumnCount(phid, pVal);
00492 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Fri Jun 6 2014 18:37:21