|
| 1 | +/* |
| 2 | + Copyright (c) 2012 Arduino LLC. All right reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <Arduino.h> |
| 20 | +#include <TwoWayIntegerEasyTransfer.h> |
| 21 | +#include "../LottieLemon.h" |
| 22 | + |
| 23 | +#ifdef ARDUINO_AVR_ROBOT_CONTROL |
| 24 | + |
| 25 | +using namespace LottieLemon; |
| 26 | + |
| 27 | +static void _requestData(uint8_t command) { |
| 28 | + TwoWayIntegerEasyTransfer.writeByte(command); |
| 29 | + TwoWayIntegerEasyTransfer.sendData(); |
| 30 | + if (TwoWayIntegerEasyTransfer.hasReceivedData()) { |
| 31 | + TwoWayIntegerEasyTransfer.processInput(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +ControlBoard::ControlBoard() |
| 36 | + : _multiplexer{ NOT_A_PIN, MUXA, MUXB, MUXC, MUXD } { |
| 37 | +} |
| 38 | + |
| 39 | +void ControlBoard::setMode(uint8_t mode) { |
| 40 | + TwoWayIntegerEasyTransfer.writeByte(COMMAND_SWITCH_MODE); |
| 41 | + TwoWayIntegerEasyTransfer.writeByte(mode); |
| 42 | + TwoWayIntegerEasyTransfer.sendData(); |
| 43 | +} |
| 44 | + |
| 45 | +void ControlBoard::pauseMode(bool isPaused) { |
| 46 | + TwoWayIntegerEasyTransfer.writeByte(COMMAND_PAUSE_MODE); |
| 47 | + TwoWayIntegerEasyTransfer.writeByte(isPaused); |
| 48 | + TwoWayIntegerEasyTransfer.sendData(); |
| 49 | +} |
| 50 | + |
| 51 | +bool ControlBoard::isActionDone(void) { |
| 52 | + static bool actionValue; |
| 53 | + TwoWayIntegerEasyTransfer.attach( |
| 54 | + [](uint8_t command, IntegerEasyTransfer &) { |
| 55 | + if (command == COMMAND_ACTION_DONE) { |
| 56 | + actionValue = true; |
| 57 | + } |
| 58 | + else { |
| 59 | + actionValue = false; |
| 60 | + } |
| 61 | + }); |
| 62 | + if (TwoWayIntegerEasyTransfer.hasReceivedData()) { |
| 63 | + TwoWayIntegerEasyTransfer.processInput(); |
| 64 | + } |
| 65 | + return actionValue; |
| 66 | +} |
| 67 | + |
| 68 | +void ControlBoard::lineFollowConfig( |
| 69 | + uint8_t kP, uint8_t kD, |
| 70 | + uint8_t robotSpeedPercentage, uint8_t intergrationTimeMillis) { |
| 71 | + TwoWayIntegerEasyTransfer.writeByte(COMMAND_LINE_FOLLOW_CONFIG); |
| 72 | + TwoWayIntegerEasyTransfer.writeByte(kP); |
| 73 | + TwoWayIntegerEasyTransfer.writeByte(kD); |
| 74 | + TwoWayIntegerEasyTransfer.writeByte(robotSpeedPercentage); |
| 75 | + TwoWayIntegerEasyTransfer.writeByte(intergrationTimeMillis); |
| 76 | + TwoWayIntegerEasyTransfer.sendData(); |
| 77 | +} |
| 78 | + |
| 79 | +void ControlBoard::motorsWrite(int speedLeft, int speedRight) { |
| 80 | + TwoWayIntegerEasyTransfer.writeByte(COMMAND_RUN); |
| 81 | + TwoWayIntegerEasyTransfer.writeInt(speedLeft); |
| 82 | + TwoWayIntegerEasyTransfer.writeInt(speedRight); |
| 83 | + TwoWayIntegerEasyTransfer.sendData(); |
| 84 | +} |
| 85 | + |
| 86 | +void ControlBoard::motorsStop(void) { |
| 87 | + TwoWayIntegerEasyTransfer.writeByte(COMMAND_MOTORS_STOP); |
| 88 | + TwoWayIntegerEasyTransfer.sendData(); |
| 89 | +} |
| 90 | + |
| 91 | +bool ControlBoard::digitalRead(uint8_t port) { |
| 92 | + return false; |
| 93 | +} |
| 94 | + |
| 95 | +void ControlBoard::digitalWrite(uint8_t port, bool value) { |
| 96 | + /* |
| 97 | + //bottom TKs, just for communication purpose |
| 98 | + enum { |
| 99 | + B_TK1 = 201, |
| 100 | + B_TK2 = 202, |
| 101 | + B_TK3 = 203, |
| 102 | + B_TK4 = 204 |
| 103 | + }; |
| 104 | + #define D10 B_TK1 |
| 105 | + #define D9 B_TK2 |
| 106 | + #define D8 B_TK4 |
| 107 | + #define D7 B_TK3 |
| 108 | + */ |
| 109 | + /* |
| 110 | + Type Code Label |
| 111 | + D12 [TKD5] (D5) MUXC Not safe to use! |
| 112 | + D6 [TKD4] (D4) MUXA Not safe to use! |
| 113 | + A4 [TKD3] (D3) |
| 114 | + A3 [TKD2] (D2) |
| 115 | + A2 [TKD1] (D1) |
| 116 | + A1 [TKD0] (D0) |
| 117 | + D17 [LED1] (LED) |
| 118 | + Type Code Label |
| 119 | + A11 [B_TK4] (D8) |
| 120 | + A6 [B_TK3] (D7) |
| 121 | + A1 [B_TK2] (D9) |
| 122 | + A0 [B_TK1] (D10) |
| 123 | + X0..X7 @ mux |
| 124 | + */ |
| 125 | +} |
| 126 | + |
| 127 | +int ControlBoard::analogRead(uint8_t port) { |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +void ControlBoard::analogWrite(uint8_t port, uint8_t value) { |
| 132 | +} |
| 133 | + |
| 134 | +uint8_t ControlBoard::updateIR(uint16_t * ir, uint8_t size) { |
| 135 | + static const uint8_t MAX_IR_DATA = 5; |
| 136 | + static uint16_t irValues[MAX_IR_DATA]; |
| 137 | + static uint8_t maxItems; |
| 138 | + maxItems = min(MAX_IR_DATA, size); |
| 139 | + TwoWayIntegerEasyTransfer.attach( |
| 140 | + [](uint8_t command, IntegerEasyTransfer & request) { |
| 141 | + for (uint16_t i = 0; i < MAX_IR_DATA; i++) { |
| 142 | + if (command == COMMAND_READ_IR_RE) { |
| 143 | + irValues[i] = request.readInt(); |
| 144 | + } |
| 145 | + else { |
| 146 | + irValues[i] = 0; |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + _requestData(COMMAND_READ_IR); |
| 151 | + memcpy(ir, irValues, maxItems * sizeof(uint16_t)); |
| 152 | + return maxItems; |
| 153 | +} |
| 154 | + |
| 155 | +int LottieLemon::ControlBoard::trimRead() { |
| 156 | + static int trimmerValue; |
| 157 | + TwoWayIntegerEasyTransfer.attach( |
| 158 | + [](uint8_t command, IntegerEasyTransfer & request) { |
| 159 | + if (command == COMMAND_READ_TRIM_RE) { |
| 160 | + trimmerValue = request.readInt(); |
| 161 | + } |
| 162 | + else { |
| 163 | + trimmerValue = 0; |
| 164 | + } |
| 165 | + }); |
| 166 | + _requestData(COMMAND_READ_TRIM); |
| 167 | + return trimmerValue; |
| 168 | +} |
| 169 | + |
| 170 | +int ControlBoard::knobRead() { |
| 171 | + return /*Arduino*/::analogRead(POT); |
| 172 | +} |
| 173 | + |
| 174 | +#endif // ARDUINO_AVR_ROBOT_CONTROL |
0 commit comments