|
| 1 | +/* |
| 2 | + * I2CConsole.c |
| 3 | + * |
| 4 | + * Created on: May 29, 2017 |
| 5 | + * Author: dat |
| 6 | + */ |
| 7 | + |
| 8 | +#include "I2CConsole.h" |
| 9 | + |
| 10 | +#include <util/delay.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +#include "SerialDebug.h" |
| 15 | +#include "I2C.h" |
| 16 | + |
| 17 | +static uint8_t _i2c_address; |
| 18 | +static uint8_t _i2c_slowTx; |
| 19 | + |
| 20 | +static void consoleInit() |
| 21 | +{ |
| 22 | + static uint8_t isInited = 0; |
| 23 | + if (!isInited) |
| 24 | + { |
| 25 | + I2CInit(); |
| 26 | + isInited = 1; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command) |
| 31 | +{ |
| 32 | + consoleInit(); |
| 33 | + |
| 34 | + uint8_t retVal = (command->isValid) ? 0 : 2; |
| 35 | + |
| 36 | + if (!retVal) |
| 37 | + { |
| 38 | + uint8_t data[I2CMESSAGE_MAXLEN]; // for converting to uint8_t array |
| 39 | + |
| 40 | + if (command->command == SET_ADDRESS) |
| 41 | + { |
| 42 | + _i2c_address = (uint8_t) command->address; |
| 43 | + } |
| 44 | + else if (command->command == PING) |
| 45 | + { |
| 46 | + // don't change command address if it's PING |
| 47 | + ; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + command->address = _i2c_address; |
| 52 | + int i; |
| 53 | + |
| 54 | + if (command->tx_len) |
| 55 | + { |
| 56 | + for (i = 0; i < command->tx_len; ++i) |
| 57 | + { |
| 58 | + data[i] = command->tx[i]; |
| 59 | + } |
| 60 | + } |
| 61 | + else if (command->message) |
| 62 | + { |
| 63 | + for (i = 0; i < (int) strlen(command->message); ++i) |
| 64 | + { |
| 65 | + data[i] = command->message[i]; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (command->command == SET_SLOW) |
| 71 | + { |
| 72 | + _i2c_slowTx = (command->isDelayBetweenBytes) ? 1 : 0; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + command->isDelayBetweenBytes = _i2c_slowTx; |
| 77 | + } |
| 78 | + |
| 79 | + if (command->address != 0x00) |
| 80 | + { |
| 81 | + if (command->command == SEND) |
| 82 | + { |
| 83 | + if (command->tx_len) |
| 84 | + { |
| 85 | + retVal += I2CSendData(_i2c_address, data, command->tx_len, |
| 86 | + _i2c_slowTx); |
| 87 | + } |
| 88 | + |
| 89 | + else if (command->message) |
| 90 | + { |
| 91 | + retVal += I2CSendData(_i2c_address, data, |
| 92 | + strlen(command->message), _i2c_slowTx); |
| 93 | + } |
| 94 | + } |
| 95 | + else if (command->command == SENDNRECV) |
| 96 | + { |
| 97 | + |
| 98 | + if (command->tx_len) |
| 99 | + { |
| 100 | + retVal += I2CSendnRecvData(_i2c_address, data, |
| 101 | + command->tx_len, command->rx, command->rx_len, |
| 102 | + _i2c_slowTx); |
| 103 | + } |
| 104 | + |
| 105 | + else if (command->message) |
| 106 | + { |
| 107 | + retVal += I2CSendnRecvData(_i2c_address, data, |
| 108 | + strlen(command->message), command->rx, |
| 109 | + command->rx_len, _i2c_slowTx); |
| 110 | + } |
| 111 | + } |
| 112 | + else if (command->command == PING) |
| 113 | + { |
| 114 | + retVal = (I2CCheckAlive(command->address) == 0) ? retVal : 4; |
| 115 | + TRACE_INT(retVal); |
| 116 | + LOG("address %x", command->address); |
| 117 | + } |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + retVal = 3; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!retVal) |
| 126 | + _delay_ms(150); |
| 127 | + |
| 128 | + return retVal; |
| 129 | +} |
| 130 | + |
| 131 | +uint8_t I2CConsoleGetCurrentAddress() |
| 132 | +{ |
| 133 | + consoleInit(); |
| 134 | + return _i2c_address; |
| 135 | +} |
| 136 | + |
| 137 | +uint8_t I2CConsoleGetSlowSendingStatus() |
| 138 | +{ |
| 139 | + consoleInit(); |
| 140 | + return _i2c_slowTx; |
| 141 | +} |
0 commit comments