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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
// ColorCal.c
//
// Author: Christopher Broussard
// Date: 2/16/09
#include "ColorCal.h"
// Globals
IOUSBDeviceInterface **ccDevice = NULL;
bool exitFunctionRegistered = false;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
IOUSBDevRequest request;
char command[256], buffer[64];
int i;
// Verify the number of input arguments.
if (nrhs == 0) {
mexErrMsgTxt("Usage: ouputData = ColorCal('command', [inputData])");
}
// Check to see if the first argument is a string.
if (mxIsChar(prhs[0]) == false) {
mexErrMsgTxt("First parameter must be a command string.");
}
// If the device isn't open, go ahead and open it.
if (ccDevice == NULL) {
mexPrintf("- Opening ColorCal device...");
if (OpenDevice() == true) {
mexPrintf("Done\n");
}
else {
mexErrMsgTxt("(ColorCal) ColorCal2 device not attached to the computer.");
}
}
// Make sure the exit function is registered.
if (exitFunctionRegistered == false) {
mexAtExit(CloseDevice);
exitFunctionRegistered = true;
}
// Extract the command.
if (mxGetString(prhs[0], command, 256)) {
mexErrMsgTxt("(ColorCal) Failed to extract command string.");
}
if (strcasecmp("LEDOn", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 2;
request.wLength = 0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to turn on LED.");
}
}
else if (strcasecmp("LEDOff", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 3;
request.wLength = 0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to turn off LED.");
}
}
else if (strcasecmp("GetRawData", command) == 0) {
UInt32 fatBuffer[7];
// Create the fieldnames for the structure we'll use to return the data.
char **fieldNames = (char**)mxMalloc(7*sizeof(char*));
for (i = 0; i < 7; i++) {
fieldNames[i] = (char*)mxMalloc(16*sizeof(char));
switch (i) {
case 0:
strcpy(fieldNames[i], "Xdata");
break;
case 1:
strcpy(fieldNames[i], "Xzero");
break;
case 2:
strcpy(fieldNames[i], "Ydata");
break;
case 3:
strcpy(fieldNames[i], "Yzero");
break;
case 4:
strcpy(fieldNames[i], "Zdata");
break;
case 5:
strcpy(fieldNames[i], "Zzero");
break;
case 6:
strcpy(fieldNames[i], "Trigger");
break;
}
}
plhs[0] = mxCreateStructMatrix(1, 1, 7, (const char**)fieldNames);
request.bmRequestType = 0xC0;
request.wValue = 4;
request.wLength = 28;
request.pData = fatBuffer;
// Send read request.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to read raw data.");
}
#ifdef __ppc__
// Swap the byte order of the results on PPC machines.
for (i = 0; i < 7; i++) {
fatBuffer[i] = Endian32_Swap(fatBuffer[i]);
}
#endif
//mexPrintf("%d\n", fatBuffer[0]);
// Add the results to the struct we'll return to Matlab.
mxSetField(plhs[0], 0, "Xdata", mxCreateDoubleScalar((double)fatBuffer[0]));
mxSetField(plhs[0], 0, "Xzero", mxCreateDoubleScalar((double)fatBuffer[1]));
mxSetField(plhs[0], 0, "Ydata", mxCreateDoubleScalar((double)fatBuffer[2]));
mxSetField(plhs[0], 0, "Yzero", mxCreateDoubleScalar((double)fatBuffer[3]));
mxSetField(plhs[0], 0, "Zdata", mxCreateDoubleScalar((double)fatBuffer[4]));
mxSetField(plhs[0], 0, "Zzero", mxCreateDoubleScalar((double)fatBuffer[5]));
mxSetField(plhs[0], 0, "Trigger", mxCreateDoubleScalar((double)fatBuffer[6]));
}
else if (strcasecmp("MeasureXYZ", command) == 0) {
float xxx, yyy, zzz;
request.bmRequestType = 0x40;
request.wValue = 1;
request.wLength = 32;
buffer[0] = 'M'; buffer[1] = 'E'; buffer[2] = 'S';
request.pData = buffer;
// Send the request to make a measurement.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send measurement request.");
}
// Grab the result.
request.bmRequestType = 0xC0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to get measurement.");
}
// Parse the results.
sscanf(buffer, "OK00,%6f,%6f,%6f", &xxx, &yyy, &zzz);
// Setup the returned variables.
plhs[0] = mxCreateDoubleScalar((double)xxx);
plhs[1] = mxCreateDoubleScalar((double)yyy);
plhs[2] = mxCreateDoubleScalar((double)zzz);
}
else if (strcasecmp("ZeroCalibration", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 1;
request.wLength = 32;
buffer[0] = 'U'; buffer[1] = 'Z'; buffer[2] = 'C';
request.pData = buffer;
// Send the request to zero the calibration.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send zero calibration request.");
}
// Grab the result.
request.bmRequestType = 0xC0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to get zero calibration result.");
}
if (strcmp("OK00\n", buffer) == 0) {
plhs[0] = mxCreateDoubleScalar(1.0);
}
else if (strcmp("ER11\n", buffer) == 0) {
plhs[0] = mxCreateDoubleScalar(0.0);
}
else {
mexPrintf("* Buffer Data: %s\n", buffer);
mexErrMsgTxt("(ColorCal) Failed to parse zero calibration return code.");
}
}
else if (strcasecmp("ReadColorMatrix", command) == 0 || strcasecmp("ReadColourMatrix", command) == 0) {
int xxx, yyy, zzz;
int row;
// Create the mxArray to hold the data.
plhs[0] = mxCreateDoubleMatrix(9, 3, mxREAL);
plhs[1] = mxCreateDoubleMatrix(9, 3, mxREAL);
double *pr = mxGetPr(plhs[0]); // Pointer to converted data.
double *rpr = mxGetPr(plhs[1]); // Pointer to raw data.
for (row = 0; row < 9; row++) {
request.bmRequestType = 0x40;
request.wValue = 1;
request.wLength = 32;
buffer[0] = 'r'; buffer[1] = '0'; buffer[2] = row +'1';
request.pData = buffer;
// Send the request to read the matrix.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send read matrix request.");
}
// Grab the result.
request.bmRequestType = 0xC0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to read matrix.");
}
// Parse the results.
sscanf(buffer, "OK00,%5d,%5d,%5d", &xxx, &yyy, &zzz);
// Converted data.
pr[row] = (double)DefunnyMatrixValue(xxx);
pr[row+9] = (double)DefunnyMatrixValue(yyy);
pr[row+18] = (double)DefunnyMatrixValue(zzz);
// Raw data.
rpr[row] = (double)xxx;
rpr[row+9] = (double)yyy;
rpr[row+18] = (double)zzz;
}
}
else if (strcasecmp("DeviceInfo", command) == 0) {
int rom_version, build_number, serial_number;
request.bmRequestType = 0x40;
request.wValue = 1;
request.wLength = 32;
buffer[0] = 'I'; buffer[1] = 'D'; buffer[2] = 'R';
request.pData = buffer;
// Send the request to get the device info.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send device info request.");
}
// Grab the result.
request.bmRequestType = 0xC0;
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to get device info.");
}
// Parse the results.
sscanf(buffer, "OK00,1,%3d,100.10,%8d,%3d", &rom_version, &serial_number, &build_number);
// Add the results to Matlab.
plhs[0] = mxCreateDoubleScalar((double)rom_version);
plhs[1] = mxCreateDoubleScalar((double)serial_number);
plhs[2] = mxCreateDoubleScalar((double)build_number);
}
else if (strcasecmp("ResetEEProm", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 7;
request.wLength = 0;
// Send the reset command.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send reset command.");
}
}
else if (strcasecmp("StartBootloader", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 99;
request.wLength = 0;
// Send the bootloader command.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to send bootloader command.");
}
}
else if (strcasecmp("SetTriggerThreshold", command) == 0) {
request.bmRequestType = 0x40;
request.wValue = 8;
request.wLength = 0;
int triggerValue;
// Make sure that the user passed a threshold value.
if (nrhs != 2) {
mexErrMsgTxt("(ColorCal) Trigger value required.");
}
// Grab the trigger value.
triggerValue = (int)mxGetScalar(prhs[1]);
request.wIndex = (UInt16)triggerValue;
// Send the set trigger threshold command.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to set the trigger threshold.");
}
}
else if (strcasecmp("SetLEDFunction", command) == 0) {
int tValue;
request.bmRequestType = 0x40;
request.wValue = 9;
request.wLength = 0;
if (nrhs != 2) {
mexErrMsgTxt("(ColorCal) LED function value required.");
}
// Get the LED trigger value and make sure it's valid.
tValue = (int)mxGetScalar(prhs[1]);
if (tValue < 0 || tValue > 1) {
mexErrMsgTxt("(ColorCal) LED function value must be 0 or 1.");
}
request.wIndex = (UInt16)tValue;
// Send the new LED function value.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to set the LED function value.");
}
}
else if (strcasecmp("SetColorMatrix", command) == 0 || strcasecmp("SetColourMatrix", command) == 0) {
UInt16 cBuffer[9];
size_t dims[2];
double *pr;
int matrixIndex;
request.bmRequestType = 0x40;
request.wValue = 6;
request.wLength = 18;
request.pData = cBuffer;
// Make sure that the user passed a matrix index and matrix data.
if (nrhs != 3) {
mexErrMsgTxt("(ColorCal) Color matrix and index required.");
}
// Make sure that the matrix index is in the range [0,2].
matrixIndex = (int)mxGetScalar(prhs[1]);
if (matrixIndex < 0 || matrixIndex > 2) {
mexErrMsgTxt("(ColorCal) Matrix index must be in the range [0,2].");
}
request.wIndex = (UInt16)matrixIndex;
// Check the matrix dimensions.
if (mxGetNumberOfDimensions(prhs[2]) != 2) {
mexErrMsgTxt("(ColorCal) Matrix must only have 2 dimensions.");
}
dims[0] = mxGetM(prhs[2]); dims[1] = mxGetN(prhs[2]);
if (dims[0] != 3 || dims[1] != 3) {
mexErrMsgTxt("(ColorCal) Matrix must be 3x3.");
}
// Populate the data phase by iterating over each row.
pr = mxGetPr(prhs[2]);
for (i = 0; i < 3; i++) {
cBuffer[i*3] = RefunnyMatrixValue(pr[i]);
cBuffer[i*3+1] = RefunnyMatrixValue(pr[i+3]);
cBuffer[i*3+2] = RefunnyMatrixValue(pr[i+6]);
}
// Send the new color matrix.
if ((*ccDevice)->DeviceRequest(ccDevice, &request) != kIOReturnSuccess) {
mexErrMsgTxt("(ColorCal) Failed to set the color matrix.");
}
}
else {
mexErrMsgTxt("(ColorCal) Invalid command.");
}
}
// Converts from floating point to Minolta format. Performs a byte swap if
// run on a PPC machine.
UInt16 RefunnyMatrixValue(double value)
{
UInt16 minoltaValue;
value *= 10000.0;
if (value < 0) {
value *= -1;
value += 50000.0;
}
minoltaValue = (UInt16)value;
#ifdef __ppc__
// On PPC machines we need to change the by order from big endian to little.
minoltaValue = Endian16_Swap(minoltaValue);
#endif
return minoltaValue;
}
// Converts a raw matrix value from Minolta format to floating point.
double DefunnyMatrixValue(int value)
{
mexPrintf("%d\n", value);
if (value >= 50000) {
return -((double)value - 50000.0) / 10000.0;
}
else {
return (double)value / 10000.0;
}
}
static void CloseDevice(void)
{
if (ccDevice != NULL) {
mexPrintf("- Closing ColorCal Device\n");
(void)(*ccDevice)->USBDeviceClose(ccDevice);
(void)(*ccDevice)->Release(ccDevice);
ccDevice = NULL;
}
}
bool OpenDevice(void)
{
mach_port_t masterPort;
kern_return_t kr;
CFMutableDictionaryRef matchingDict;
SInt32 usbVendor = kColorCal2VendorID;
SInt32 usbProduct = kColorCal2ProductID;
IOUSBDeviceInterface **dev = NULL;
io_iterator_t iterator;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT result;
io_service_t usbDevice;
SInt32 score;
UInt16 vendor;
UInt16 product;
UInt16 release;
bool deviceFound = false;
// Create a master port for communication with the I/O Kit
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (kr || !masterPort) {
mexErrMsgTxt("(ColorCal) Couldn?t create a master I/O Kit port.");
}
// Set up matching dictionary for class IOUSBDevice and its subclasses
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict) {
mach_port_deallocate(mach_task_self(), masterPort);
mexErrMsgTxt("(ColorCal) Couldn?t create a USB matching dictionary\n");
}
//Add the vendor and product IDs to the matching dictionary.
//This is the second key in the table of device-matching keys of the
//USB Common Class Specification
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
kr = IOServiceGetMatchingServices(masterPort, matchingDict, &iterator);
if (kr) {
mexErrMsgTxt("(ColorCal) Couldn't get matching services\n");
}
// Attempt to find the correct device.
while (usbDevice = IOIteratorNext(iterator)) {
// Create an intermediate plug-in
kr = IOCreatePlugInInterfaceForService(usbDevice,
kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
&plugInInterface, &score);
// Don?t need the device object after intermediate plug-in is created
kr = IOObjectRelease(usbDevice);
if ((kIOReturnSuccess != kr) || !plugInInterface) {
printf("Unable to create a plug-in (%08x)\n", kr);
continue;
}
// Now create the device interface
result = (*plugInInterface)->QueryInterface(plugInInterface,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(LPVOID *)&dev);
// Don?t need the intermediate plug-in after device interface is created.
(*plugInInterface)->Release(plugInInterface);
if (result || !dev) {
printf("Couldn?t create a device interface (%08x)\n", (int) result);
continue;
}
// Check these values for confirmation.
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
kr = (*dev)->GetDeviceReleaseNumber(dev, &release);
if ((vendor != kColorCal2VendorID) || (product != kColorCal2ProductID)) {
mexPrintf("Found unwanted device (vendor = %d, product = %d)\n", vendor, product);
(void) (*dev)->Release(dev);
continue;
}
else {
deviceFound = true;
//mexPrintf("Vendor: 0x%x\nProduct: 0x%x\nRelease: 0x%x\n", vendor, product, release);
break;
}
}
if (deviceFound) {
// Open the device to change its state
kr = (*dev)->USBDeviceOpen(dev);
if (kr != kIOReturnSuccess) {
(void) (*dev)->Release(dev);
mexErrMsgTxt("(ColorCal) Unable to open device.");
}
// Configure device
kr = ConfigureDevice(dev);
if (kr != kIOReturnSuccess) {
(void) (*dev)->USBDeviceClose(dev);
(void) (*dev)->Release(dev);
mexErrMsgTxt("Unable to configure device");
}
// Store a reference to the device.
ccDevice = dev;
}
// Finished with master port
mach_port_deallocate(mach_task_self(), masterPort);
masterPort = 0;
return deviceFound;
}
IOReturn ConfigureDevice(IOUSBDeviceInterface **dev)
{
UInt8 numConfig;
IOReturn kr;
IOUSBConfigurationDescriptorPtr configDesc;
// Get the number of configurations. The sample code always chooses
// the first configuration (at index 0) but your code may need a
// different one
kr = (*dev)->GetNumberOfConfigurations(dev, &numConfig);
if (!numConfig) {
return -1;
}
// Get the configuration descriptor for index 0
kr = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &configDesc);
if (kr) {
printf("Couldn?t get configuration descriptor for index %d (err = %08x)\n", 0, kr);
return -1;
}
// Set the device?s configuration. The configuration value is found in
// the bConfigurationValue field of the configuration descriptor
kr = (*dev)->SetConfiguration(dev, configDesc->bConfigurationValue);
if (kr) {
printf("Couldn?t set configuration to value %d (err = %08x)\n", 0, kr);
return -1;
}
return kIOReturnSuccess;
}
|