Color sensor reset at the end of calibration added. sensor id auto assignment was changed to be a fixed value assignment to avoid sensor id shift when some sensor is absent.

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 23 07:51:55 2018 +0000
Revision:
1:8818b793d147
Parent:
0:ce97f6d34336
Wrong behavior when one of color sensor is missing has been fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:ce97f6d34336 1 /**
Rhyme 0:ce97f6d34336 2 * Copyright 2015 Afero, Inc.
Rhyme 0:ce97f6d34336 3 *
Rhyme 0:ce97f6d34336 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rhyme 0:ce97f6d34336 5 * you may not use this file except in compliance with the License.
Rhyme 0:ce97f6d34336 6 * You may obtain a copy of the License at
Rhyme 0:ce97f6d34336 7 *
Rhyme 0:ce97f6d34336 8 * http://www.apache.org/licenses/LICENSE-2.0
Rhyme 0:ce97f6d34336 9 *
Rhyme 0:ce97f6d34336 10 * Unless required by applicable law or agreed to in writing, software
Rhyme 0:ce97f6d34336 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rhyme 0:ce97f6d34336 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rhyme 0:ce97f6d34336 13 * See the License for the specific language governing permissions and
Rhyme 0:ce97f6d34336 14 * limitations under the License.
Rhyme 0:ce97f6d34336 15 */
Rhyme 0:ce97f6d34336 16
Rhyme 0:ce97f6d34336 17 #include "StatusCommand.h"
Rhyme 0:ce97f6d34336 18
Rhyme 0:ce97f6d34336 19 StatusCommand::StatusCommand(uint16_t bytesToSend) {
Rhyme 0:ce97f6d34336 20 _cmd = 0x30;
Rhyme 0:ce97f6d34336 21 _bytesToSend = bytesToSend;
Rhyme 0:ce97f6d34336 22 _bytesToRecv = 0;
Rhyme 0:ce97f6d34336 23 }
Rhyme 0:ce97f6d34336 24
Rhyme 0:ce97f6d34336 25 StatusCommand::StatusCommand() {
Rhyme 0:ce97f6d34336 26 _cmd = 0x30;
Rhyme 0:ce97f6d34336 27 _bytesToSend = 0;
Rhyme 0:ce97f6d34336 28 _bytesToRecv = 0;
Rhyme 0:ce97f6d34336 29 }
Rhyme 0:ce97f6d34336 30
Rhyme 0:ce97f6d34336 31 StatusCommand::~StatusCommand() {
Rhyme 0:ce97f6d34336 32 }
Rhyme 0:ce97f6d34336 33
Rhyme 0:ce97f6d34336 34 uint16_t StatusCommand::getSize() {
Rhyme 0:ce97f6d34336 35 return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv);
Rhyme 0:ce97f6d34336 36 }
Rhyme 0:ce97f6d34336 37
Rhyme 0:ce97f6d34336 38 uint16_t StatusCommand::getBytes(int *bytes) {
Rhyme 0:ce97f6d34336 39 int index = 0;
Rhyme 0:ce97f6d34336 40
Rhyme 0:ce97f6d34336 41 bytes[index++] = (_cmd);
Rhyme 0:ce97f6d34336 42 bytes[index++] = (_bytesToSend & 0xff);
Rhyme 0:ce97f6d34336 43 bytes[index++] = ((_bytesToSend >> 8) & 0xff);
Rhyme 0:ce97f6d34336 44 bytes[index++] = (_bytesToRecv & 0xff);
Rhyme 0:ce97f6d34336 45 bytes[index++] = ((_bytesToRecv >> 8) & 0xff);
Rhyme 0:ce97f6d34336 46
Rhyme 0:ce97f6d34336 47 return index;
Rhyme 0:ce97f6d34336 48 }
Rhyme 0:ce97f6d34336 49
Rhyme 0:ce97f6d34336 50 uint8_t StatusCommand::calcChecksum() {
Rhyme 0:ce97f6d34336 51 uint8_t result = 0;
Rhyme 0:ce97f6d34336 52
Rhyme 0:ce97f6d34336 53 result += (_cmd);
Rhyme 0:ce97f6d34336 54 result += (_bytesToSend & 0xff);
Rhyme 0:ce97f6d34336 55 result += ((_bytesToSend >> 8) & 0xff);
Rhyme 0:ce97f6d34336 56 result += (_bytesToRecv & 0xff);
Rhyme 0:ce97f6d34336 57 result += ((_bytesToRecv >> 8) & 0xff);
Rhyme 0:ce97f6d34336 58
Rhyme 0:ce97f6d34336 59 return result;
Rhyme 0:ce97f6d34336 60 }
Rhyme 0:ce97f6d34336 61
Rhyme 0:ce97f6d34336 62 void StatusCommand::setChecksum(uint8_t checksum) {
Rhyme 0:ce97f6d34336 63 _checksum = checksum;
Rhyme 0:ce97f6d34336 64 }
Rhyme 0:ce97f6d34336 65
Rhyme 0:ce97f6d34336 66 uint8_t StatusCommand::getChecksum() {
Rhyme 0:ce97f6d34336 67 uint8_t result = 0;
Rhyme 0:ce97f6d34336 68
Rhyme 0:ce97f6d34336 69 result += (_cmd);
Rhyme 0:ce97f6d34336 70 result += (_bytesToSend & 0xff);
Rhyme 0:ce97f6d34336 71 result += ((_bytesToSend >> 8) & 0xff);
Rhyme 0:ce97f6d34336 72 result += (_bytesToRecv & 0xff);
Rhyme 0:ce97f6d34336 73 result += ((_bytesToRecv >> 8) & 0xff);
Rhyme 0:ce97f6d34336 74
Rhyme 0:ce97f6d34336 75 return result;
Rhyme 0:ce97f6d34336 76 }
Rhyme 0:ce97f6d34336 77
Rhyme 0:ce97f6d34336 78 void StatusCommand::setAck(bool ack) {
Rhyme 0:ce97f6d34336 79 _cmd = ack ? 0x31 : 0x30;
Rhyme 0:ce97f6d34336 80 }
Rhyme 0:ce97f6d34336 81
Rhyme 0:ce97f6d34336 82 void StatusCommand::setBytesToSend(uint16_t bytesToSend) {
Rhyme 0:ce97f6d34336 83 _bytesToSend = bytesToSend;
Rhyme 0:ce97f6d34336 84 }
Rhyme 0:ce97f6d34336 85
Rhyme 0:ce97f6d34336 86 uint16_t StatusCommand::getBytesToSend() {
Rhyme 0:ce97f6d34336 87 return _bytesToSend;
Rhyme 0:ce97f6d34336 88 }
Rhyme 0:ce97f6d34336 89
Rhyme 0:ce97f6d34336 90 void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) {
Rhyme 0:ce97f6d34336 91 _bytesToRecv = bytesToRecv;
Rhyme 0:ce97f6d34336 92 }
Rhyme 0:ce97f6d34336 93
Rhyme 0:ce97f6d34336 94 uint16_t StatusCommand::getBytesToRecv() {
Rhyme 0:ce97f6d34336 95 return _bytesToRecv;
Rhyme 0:ce97f6d34336 96 }
Rhyme 0:ce97f6d34336 97
Rhyme 0:ce97f6d34336 98 bool StatusCommand::equals(StatusCommand *statusCommand) {
Rhyme 0:ce97f6d34336 99 return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend &&
Rhyme 0:ce97f6d34336 100 _bytesToRecv == statusCommand->_bytesToRecv);
Rhyme 0:ce97f6d34336 101 }
Rhyme 0:ce97f6d34336 102
Rhyme 0:ce97f6d34336 103 bool StatusCommand::isValid() {
Rhyme 0:ce97f6d34336 104 return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31);
Rhyme 0:ce97f6d34336 105 }
Rhyme 0:ce97f6d34336 106
Rhyme 0:ce97f6d34336 107 void StatusCommand::dumpBytes() {
Rhyme 0:ce97f6d34336 108 int len = getSize();
Rhyme 0:ce97f6d34336 109 int bytes[len];
Rhyme 0:ce97f6d34336 110 getBytes(bytes);
Rhyme 0:ce97f6d34336 111
Rhyme 0:ce97f6d34336 112 SERIAL_PRINT_DBG_ASR("len : %d\n",len);
Rhyme 0:ce97f6d34336 113 SERIAL_PRINT_DBG_ASR("data : ");
Rhyme 0:ce97f6d34336 114 for (int i = 0; i < len; i++) {
Rhyme 0:ce97f6d34336 115 if (i > 0) {
Rhyme 0:ce97f6d34336 116 SERIAL_PRINT_DBG_ASR(", ");
Rhyme 0:ce97f6d34336 117 }
Rhyme 0:ce97f6d34336 118 int b = bytes[i] & 0xff;
Rhyme 0:ce97f6d34336 119 if (b < 0x10) {
Rhyme 0:ce97f6d34336 120 SERIAL_PRINT_DBG_ASR("0x%08x",b);
Rhyme 0:ce97f6d34336 121 } else {
Rhyme 0:ce97f6d34336 122 SERIAL_PRINT_DBG_ASR("0x%08x",b);
Rhyme 0:ce97f6d34336 123 }
Rhyme 0:ce97f6d34336 124 }
Rhyme 0:ce97f6d34336 125 SERIAL_PRINT_DBG_ASR("\n");
Rhyme 0:ce97f6d34336 126 }
Rhyme 0:ce97f6d34336 127
Rhyme 0:ce97f6d34336 128 void StatusCommand::dump() {
Rhyme 0:ce97f6d34336 129 SERIAL_PRINT_DBG_ASR("cmd : %s\n",_cmd == 0x30 ? "STATUS" : "STATUS_ACK");
Rhyme 0:ce97f6d34336 130 SERIAL_PRINT_DBG_ASR("bytes to send : %d\n",_bytesToSend);
Rhyme 0:ce97f6d34336 131 SERIAL_PRINT_DBG_ASR("bytes to receive : %d\n",_bytesToRecv);
Rhyme 0:ce97f6d34336 132 }
Rhyme 0:ce97f6d34336 133