Nespresso coffee demo working on the Arch Pro

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Committer:
bridadan
Date:
Fri Mar 20 16:11:15 2015 +0000
Revision:
12:ad05fe84b4ff
Parent:
6:8729a0db0e25
Working on Arch Pro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjblazkowicz 6:8729a0db0e25 1 /*
bjblazkowicz 6:8729a0db0e25 2 * Builds the payload to send to the server.
bjblazkowicz 6:8729a0db0e25 3 *
bjblazkowicz 6:8729a0db0e25 4 * Copyright (c) 2014 ARM Limited
bjblazkowicz 6:8729a0db0e25 5 *
bjblazkowicz 6:8729a0db0e25 6 * Licensed under the Apache License, Version 2.0 (the "License");
bjblazkowicz 6:8729a0db0e25 7 * you may not use this file except in compliance with the License.
bjblazkowicz 6:8729a0db0e25 8 * You may obtain a copy of the License at
bjblazkowicz 6:8729a0db0e25 9 *
bjblazkowicz 6:8729a0db0e25 10 * http://www.apache.org/licenses/LICENSE-2.0
bjblazkowicz 6:8729a0db0e25 11 *
bjblazkowicz 6:8729a0db0e25 12 * Unless required by applicable law or agreed to in writing, software
bjblazkowicz 6:8729a0db0e25 13 * distributed under the License is distributed on an "AS IS" BASIS,
bjblazkowicz 6:8729a0db0e25 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bjblazkowicz 6:8729a0db0e25 15 * See the License for the specific language governing permissions and
bjblazkowicz 6:8729a0db0e25 16 * limitations under the License.
bjblazkowicz 6:8729a0db0e25 17 */
bjblazkowicz 6:8729a0db0e25 18 #include <mbed.h>
bjblazkowicz 6:8729a0db0e25 19 #include "payload.h"
bjblazkowicz 6:8729a0db0e25 20
bjblazkowicz 6:8729a0db0e25 21 // Maximum number of samples to include in the payload.
bjblazkowicz 6:8729a0db0e25 22 // Defined such that the payload fits within one UDP packet.
bjblazkowicz 6:8729a0db0e25 23 #define MAX_SAMPLES 300
bjblazkowicz 6:8729a0db0e25 24
bjblazkowicz 6:8729a0db0e25 25 Payload::Payload() :
bjblazkowicz 6:8729a0db0e25 26 m_mdb_bytes(0)
bjblazkowicz 6:8729a0db0e25 27 {
bjblazkowicz 6:8729a0db0e25 28 }
bjblazkowicz 6:8729a0db0e25 29
bjblazkowicz 6:8729a0db0e25 30 //
bjblazkowicz 6:8729a0db0e25 31 // Build the payload. The payload is defined by the Payload::MeanDeltaBuffer struct
bjblazkowicz 6:8729a0db0e25 32 // and consists of:
bjblazkowicz 6:8729a0db0e25 33 // * number of samples (32 bits)
bjblazkowicz 6:8729a0db0e25 34 // * mean of red, green, blue channels (32 bits each)
bjblazkowicz 6:8729a0db0e25 35 // * difference of sample to mean for red, green, blue channels (1 byte each)
bjblazkowicz 6:8729a0db0e25 36 //
bjblazkowicz 6:8729a0db0e25 37 // This encoding can store ~300 samples in a one UDP packet.
bjblazkowicz 6:8729a0db0e25 38 //
bjblazkowicz 6:8729a0db0e25 39 void Payload::build(const TRGB* samples, int sample_count)
bjblazkowicz 6:8729a0db0e25 40 {
bjblazkowicz 6:8729a0db0e25 41 if (sample_count > MAX_SAMPLES)
bjblazkowicz 6:8729a0db0e25 42 sample_count = MAX_SAMPLES;
bjblazkowicz 6:8729a0db0e25 43
bjblazkowicz 6:8729a0db0e25 44 for (int i = 0; i < sample_count; i++)
bjblazkowicz 6:8729a0db0e25 45 {
bjblazkowicz 6:8729a0db0e25 46 m_mdb.mean[0] += (samples[i].data[0] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 47 m_mdb.mean[1] += (samples[i].data[1] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 48 m_mdb.mean[2] += (samples[i].data[2] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 49 }
bjblazkowicz 6:8729a0db0e25 50
bjblazkowicz 6:8729a0db0e25 51 m_mdb.sample_count = sample_count;
bjblazkowicz 6:8729a0db0e25 52 m_mdb.mean[0] /= sample_count;
bjblazkowicz 6:8729a0db0e25 53 m_mdb.mean[1] /= sample_count;
bjblazkowicz 6:8729a0db0e25 54 m_mdb.mean[2] /= sample_count;
bjblazkowicz 6:8729a0db0e25 55
bjblazkowicz 6:8729a0db0e25 56 for (int i = 0; i < sample_count; i++)
bjblazkowicz 6:8729a0db0e25 57 {
bjblazkowicz 6:8729a0db0e25 58 // TODO: clamp to signed char range [-128, 127]
bjblazkowicz 6:8729a0db0e25 59 m_mdb.deltas[i][0] = m_mdb.mean[0] - (samples[i].data[0] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 60 m_mdb.deltas[i][1] = m_mdb.mean[1] - (samples[i].data[1] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 61 m_mdb.deltas[i][2] = m_mdb.mean[2] - (samples[i].data[2] / RGB_OVERSAMPLING);
bjblazkowicz 6:8729a0db0e25 62 }
bjblazkowicz 6:8729a0db0e25 63
bjblazkowicz 6:8729a0db0e25 64 // calculate number of bytes in payload
bjblazkowicz 6:8729a0db0e25 65 m_mdb_bytes = sizeof(int) + // number of samples
bjblazkowicz 6:8729a0db0e25 66 (3 * sizeof(int)) + // mean of red, green & blue channels
bjblazkowicz 6:8729a0db0e25 67 (sample_count * sizeof(signed char) * 3); // one byte per channel per sample
bjblazkowicz 6:8729a0db0e25 68 }
bjblazkowicz 6:8729a0db0e25 69
bjblazkowicz 6:8729a0db0e25 70 int Payload::raw_bytes_size()
bjblazkowicz 6:8729a0db0e25 71 {
bjblazkowicz 6:8729a0db0e25 72 return m_mdb_bytes;
bjblazkowicz 6:8729a0db0e25 73 }
bjblazkowicz 6:8729a0db0e25 74
bjblazkowicz 6:8729a0db0e25 75 uint8_t* Payload::raw_bytes() const
bjblazkowicz 6:8729a0db0e25 76 {
bjblazkowicz 6:8729a0db0e25 77 return (uint8_t *)&m_mdb;
bjblazkowicz 6:8729a0db0e25 78 }