void benchmark() { struct timeval starttime, endtime, difftime; uint8_t buffer[16000]; int i; gettimeofday(&starttime, NULL); printf("async gruuu..\n"); setupTransfer(buffer, sizeof(buffer)); while(totalBytes < 49152*50) { submitTransfer(); libusb_handle_events(NULL); } printf("\n"); gettimeofday(&endtime, NULL); timersub(&endtime,&starttime,&difftime); printf("Received %d bytes in %d.%06d seconds\n", totalBytes, difftime.tv_sec, difftime.tv_usec); printf("Transfer speed: %f kbytes/sec\n", totalBytes/1024.0/(difftime.tv_sec+difftime.tv_usec/1e6)); }
void FCDevice::writeFramebuffer() { /* * Asynchronously write the current framebuffer. * Note that the OS will copy our framebuffer at submit-time. * * XXX: To-do, flow control. If more than one frame is pending, we need to be able to * tell clients that we're going too fast, *or* we need to drop frames. */ submitTransfer(new Transfer(this, &mFramebuffer, sizeof mFramebuffer)); }
void EnttecDMXDevice::writeDMXPacket() { /* * Asynchronously write an FTDI packet containing an Enttec packet containing * our set of DMX channels. * * XXX: We should probably throttle this so that we don't send DMX messages * faster than the Enttec device can keep up! */ submitTransfer(new Transfer(this, &mChannelBuffer, mChannelBuffer.length + 5)); }
void FCDevice::writeFirmwareConfiguration() { /* * Write mFirmwareConfig to the device, and log it. */ submitTransfer(new Transfer(this, &mFirmwareConfig, sizeof mFirmwareConfig)); if (mVerbose) { std::clog << "New Fadecandy firmware configuration:"; for (unsigned i = 0; i < sizeof mFirmwareConfig.data; i++) { if (!(i & 31)) { std::clog << "\n"; } char hex[4]; sprintf(hex, " %02x", mFirmwareConfig.data[i]); std::clog << hex; } std::clog << "\n"; } }
void FCDevice::writeFramebuffer() { /* * Asynchronously write the current framebuffer. * Note that the OS will copy our framebuffer at submit-time. * * TODO: Currently if this gets ahead of what the USB device is capable of, * we always drop frames. Alternatively, it would be nice to have end-to-end * flow control so that the client can produce frames slower. */ if (mNumFramesPending >= MAX_FRAMES_PENDING) { // Too many outstanding frames. Wait to submit until a previous frame completes. mFrameWaitingForSubmit = true; return; } if (submitTransfer(new Transfer(this, &mFramebuffer, sizeof mFramebuffer, FRAME))) { mFrameWaitingForSubmit = false; mNumFramesPending++; } }
void FCDevice::opcSetFirmwareConfiguration(const OPCSink::Message &msg) { /* * Raw firmware configuration packet */ memcpy(mFirmwareConfig.data, msg.data + 4, std::min<size_t>(sizeof mFirmwareConfig.data, msg.length() - 4)); submitTransfer(new Transfer(this, &mFirmwareConfig, sizeof mFirmwareConfig)); if (mVerbose) { std::clog << "New Fadecandy firmware configuration:"; for (unsigned i = 0; i < sizeof mFirmwareConfig.data; i++) { if (!(i & 31)) { std::clog << "\n"; } char hex[4]; sprintf(hex, " %02x", mFirmwareConfig.data[i]); std::clog << hex; } std::clog << "\n"; } }
void FCDevice::writeColorCorrection(const Value &color) { /* * Populate the color correction table based on a JSON configuration object, * and send the new color LUT out over USB. * * 'color' may be 'null' to load an identity-mapped LUT, or it may be * a dictionary of options including 'gamma' and 'whitepoint'. */ // Default color LUT parameters double gamma = 1.0; double whitepoint[3] = {1.0, 1.0, 1.0}; /* * Parse the JSON object */ if (color.IsObject()) { const Value &vGamma = color["gamma"]; const Value &vWhitepoint = color["whitepoint"]; if (vGamma.IsNumber()) { gamma = vGamma.GetDouble(); } else if (!vGamma.IsNull() && mVerbose) { std::clog << "Gamma value must be a number.\n"; } if (vWhitepoint.IsArray() && vWhitepoint.Size() == 3 && vWhitepoint[0u].IsNumber() && vWhitepoint[1].IsNumber() && vWhitepoint[2].IsNumber()) { whitepoint[0] = vWhitepoint[0u].GetDouble(); whitepoint[1] = vWhitepoint[1].GetDouble(); whitepoint[2] = vWhitepoint[2].GetDouble(); } else if (!vWhitepoint.IsNull() && mVerbose) { std::clog << "Whitepoint value must be a list of 3 numbers.\n"; } } else if (!color.IsNull() && mVerbose) { std::clog << "Color correction value must be a JSON dictionary object.\n"; } /* * Calculate the color LUT, stowing the result in an array of USB packets. */ Packet *packet = mColorLUT; const unsigned firstByteOffset = 1; // Skip padding byte unsigned byteOffset = firstByteOffset; for (unsigned channel = 0; channel < 3; channel++) { for (unsigned entry = 0; entry < LUT_ENTRIES; entry++) { /* * Normalized input value corresponding to this LUT entry. * Ranges from 0 to slightly higher than 1. (The last LUT entry * can't quite be reached.) */ double input = (entry << 8) / 65535.0; // Color conversion double output = pow(input * whitepoint[channel], gamma); // Round to the nearest integer, and clamp. Overflow-safe. int64_t longValue = (output * 0xFFFF) + 0.5; int intValue = std::max<int64_t>(0, std::min<int64_t>(0xFFFF, longValue)); // Store LUT entry, little-endian order. packet->data[byteOffset++] = uint8_t(intValue); packet->data[byteOffset++] = uint8_t(intValue >> 8); if (byteOffset >= sizeof packet->data) { byteOffset = firstByteOffset; packet++; } } } // Start asynchronously sending the LUT. submitTransfer(new Transfer(this, &mColorLUT, sizeof mColorLUT)); }
void FCDevice::writeFirmwareConfiguration() { // Write mFirmwareConfig to the device submitTransfer(new Transfer(this, &mFirmwareConfig, sizeof mFirmwareConfig)); }
void FCDevice::writeColorCorrection(const Value &color) { /* * Populate the color correction table based on a JSON configuration object, * and send the new color LUT out over USB. * * 'color' may be 'null' to load an identity-mapped LUT, or it may be * a dictionary of options including 'gamma' and 'whitepoint'. * * This calculates a compound curve with a linear section and a nonlinear * section. The linear section, near zero, avoids creating very low output * values that will cause distracting flicker when dithered. This isn't a problem * when the LEDs are viewed indirectly such that the flicker is below the threshold * of perception, but in cases where the flicker is a problem this linear section can * eliminate it entierly at the cost of some dynamic range. * * By default, the linear section is disabled (linearCutoff is zero). To enable the * linear section, set linearCutoff to some nonzero value. A good starting point is * 1/256.0, correspnding to the lowest 8-bit PWM level. */ // Default color LUT parameters double gamma = 1.0; // Power for nonlinear portion of curve double whitepoint[3] = {1.0, 1.0, 1.0}; // White-point RGB value (also, global brightness) double linearSlope = 1.0; // Slope (output / input) of linear section of the curve, near zero double linearCutoff = 0.0; // Y (output) coordinate of intersection of linear and nonlinear curves /* * Parse the JSON object */ if (color.IsObject()) { const Value &vGamma = color["gamma"]; const Value &vWhitepoint = color["whitepoint"]; const Value &vLinearSlope = color["linearSlope"]; const Value &vLinearCutoff = color["linearCutoff"]; if (vGamma.IsNumber()) { gamma = vGamma.GetDouble(); } else if (!vGamma.IsNull() && mVerbose) { std::clog << "Gamma value must be a number.\n"; } if (vLinearSlope.IsNumber()) { linearSlope = vLinearSlope.GetDouble(); } else if (!vLinearSlope.IsNull() && mVerbose) { std::clog << "Linear slope value must be a number.\n"; } if (vLinearCutoff.IsNumber()) { linearCutoff = vLinearCutoff.GetDouble(); } else if (!vLinearCutoff.IsNull() && mVerbose) { std::clog << "Linear slope value must be a number.\n"; } if (vWhitepoint.IsArray() && vWhitepoint.Size() == 3 && vWhitepoint[0u].IsNumber() && vWhitepoint[1].IsNumber() && vWhitepoint[2].IsNumber()) { whitepoint[0] = vWhitepoint[0u].GetDouble(); whitepoint[1] = vWhitepoint[1].GetDouble(); whitepoint[2] = vWhitepoint[2].GetDouble(); } else if (!vWhitepoint.IsNull() && mVerbose) { std::clog << "Whitepoint value must be a list of 3 numbers.\n"; } } else if (!color.IsNull() && mVerbose) { std::clog << "Color correction value must be a JSON dictionary object.\n"; } /* * Calculate the color LUT, stowing the result in an array of USB packets. */ Packet *packet = mColorLUT; const unsigned firstByteOffset = 1; // Skip padding byte unsigned byteOffset = firstByteOffset; for (unsigned channel = 0; channel < 3; channel++) { for (unsigned entry = 0; entry < LUT_ENTRIES; entry++) { double output; /* * Normalized input value corresponding to this LUT entry. * Ranges from 0 to slightly higher than 1. (The last LUT entry * can't quite be reached.) */ double input = (entry << 8) / 65535.0; // Scale by whitepoint before anything else input *= whitepoint[channel]; // Is this entry part of the linear section still? if (input * linearSlope <= linearCutoff) { // Output value is below linearCutoff. We're still in the linear portion of the curve output = input * linearSlope; } else { // Nonlinear portion of the curve. This starts right where the linear portion leaves // off. We need to avoid any discontinuity. double nonlinearInput = input - (linearSlope * linearCutoff); double scale = 1.0 - linearCutoff; output = linearCutoff + pow(nonlinearInput / scale, gamma) * scale; } // Round to the nearest integer, and clamp. Overflow-safe. int64_t longValue = (output * 0xFFFF) + 0.5; int intValue = std::max<int64_t>(0, std::min<int64_t>(0xFFFF, longValue)); // Store LUT entry, little-endian order. packet->data[byteOffset++] = uint8_t(intValue); packet->data[byteOffset++] = uint8_t(intValue >> 8); if (byteOffset >= sizeof packet->data) { byteOffset = firstByteOffset; packet++; } } } // Start asynchronously sending the LUT. submitTransfer(new Transfer(this, &mColorLUT, sizeof mColorLUT)); }