void FCDevice::writeDevicePixels(Document &msg) { /* * Write pixels without mapping, from a JSON integer * array in msg["pixels"]. The pixel array is removed from * the reply to save network bandwidth. * * Pixel values are clamped to [0, 255], for convenience. */ const Value &pixels = msg["pixels"]; if (!pixels.IsArray()) { msg.AddMember("error", "Pixel array is missing", msg.GetAllocator()); } else { // Truncate to the framebuffer size, and only deal in whole pixels. int numPixels = pixels.Size() / 3; if (numPixels > NUM_PIXELS) numPixels = NUM_PIXELS; for (int i = 0; i < numPixels; i++) { uint8_t *out = fbPixel(i); const Value &r = pixels[i*3 + 0]; const Value &g = pixels[i*3 + 1]; const Value &b = pixels[i*3 + 2]; out[0] = std::max(0, std::min(255, r.IsInt() ? r.GetInt() : 0)); out[1] = std::max(0, std::min(255, g.IsInt() ? g.GetInt() : 0)); out[2] = std::max(0, std::min(255, b.IsInt() ? b.GetInt() : 0)); } writeFramebuffer(); } }
void FCDevice::opcMapPixelColors(const OPCSink::Message &msg, const Value &inst) { /* * Parse one JSON mapping instruction, and copy any relevant parts of 'msg' * into our framebuffer. This looks for any mapping instructions that we * recognize: * * [ OPC Channel, First OPC Pixel, First output pixel, pixel count ] */ unsigned msgPixelCount = msg.length() / 3; if (inst.IsArray() && inst.Size() == 4) { // Map a range from an OPC channel to our framebuffer const Value &vChannel = inst[0u]; const Value &vFirstOPC = inst[1]; const Value &vFirstOut = inst[2]; const Value &vCount = inst[3]; if (vChannel.IsUint() && vFirstOPC.IsUint() && vFirstOut.IsUint() && vCount.IsUint()) { unsigned channel = vChannel.GetUint(); unsigned firstOPC = vFirstOPC.GetUint(); unsigned firstOut = vFirstOut.GetUint(); unsigned count = vCount.GetUint(); if (channel != msg.channel) { return; } // Clamping, overflow-safe firstOPC = std::min<unsigned>(firstOPC, msgPixelCount); firstOut = std::min<unsigned>(firstOut, unsigned(NUM_PIXELS)); count = std::min<unsigned>(count, msgPixelCount - firstOPC); count = std::min<unsigned>(count, NUM_PIXELS - firstOut); // Copy pixels const uint8_t *inPtr = msg.data + (firstOPC * 3); unsigned outIndex = firstOut; while (count--) { uint8_t *outPtr = fbPixel(outIndex++); outPtr[0] = inPtr[0]; outPtr[1] = inPtr[1]; outPtr[2] = inPtr[2]; inPtr += 3; } return; } } // Still haven't found a match? if (mVerbose) { std::clog << "Unsupported JSON mapping instruction\n"; } }
void BitWizardWSDevice::opcMapPixelColors(const OPC::Message &msg, const Value &inst) { /* * Parse one JSON mapping instruction, and copy any relevant parts of 'msg' * into our framebuffer. This looks for any mapping instructions that we * recognize: * * [ OPC Channel, OPC Pixel, Pixel Color, DMX Channel ] */ unsigned msgPixelCount = msg.length() / 3; if (inst.IsArray() && inst.Size() == 4) { // Map a range from an OPC channel to our framebuffer const Value &vChannel = inst[0u]; const Value &vFirstOPC = inst[1]; const Value &vFirstOut = inst[2]; const Value &vCount = inst[3]; if (vChannel.IsUint() && vFirstOPC.IsUint() && vFirstOut.IsUint() && vCount.IsUint()) { unsigned channel = vChannel.GetUint(); unsigned firstOPC = vFirstOPC.GetUint(); unsigned firstOut = vFirstOut.GetUint(); unsigned count = vCount.GetUint(); if (channel != msg.channel) { return; } // Clamping, overflow-safe firstOPC = std::min<unsigned>(firstOPC, msgPixelCount); firstOut = std::min<unsigned>(firstOut, unsigned(NUM_PIXELS)); count = std::min<unsigned>(count, msgPixelCount - firstOPC); count = std::min<unsigned>(count, NUM_PIXELS - firstOut); // Copy pixels const uint8_t *inPtr = msg.data + (firstOPC * 3); unsigned outIndex = firstOut; //std::cout << "mapping " << count << " pixels at " << firstOut << "...\n"; while (count--) { uint8_t *outPtr = fbPixel(outIndex++); outPtr[0] = inPtr[0]; outPtr[1] = inPtr[1]; outPtr[2] = inPtr[2]; inPtr += 3; if (outIndex >= NUM_PIXELS) return; } return; } } if (inst.IsArray() && inst.Size() == 2) { // Constant value const Value &vValue = inst[0u]; const Value &vDMXChannel = inst[1]; if (vValue.IsUint() && vDMXChannel.IsUint()) { unsigned value = vValue.GetUint(); unsigned dmxChannel = vDMXChannel.GetUint(); //setChannel(dmxChannel, value); return; } } // Still haven't found a match? if (mVerbose) { rapidjson::GenericStringBuffer<rapidjson::UTF8<> > buffer; rapidjson::Writer<rapidjson::GenericStringBuffer<rapidjson::UTF8<> > > writer(buffer); inst.Accept(writer); std::clog << "Unsupported JSON mapping instruction: " << buffer.GetString() << "\n"; } }