Skip to content

Commit 8cb0e4f

Browse files
committed
- moved i2c-console parser to callback management
1 parent 1c1a0bf commit 8cb0e4f

File tree

5 files changed

+252
-164
lines changed

5 files changed

+252
-164
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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
45+
{
46+
command->address = _i2c_address;
47+
int i;
48+
49+
if (command->tx_len)
50+
{
51+
for (i = 0; i < command->tx_len; ++i)
52+
{
53+
data[i] = command->tx[i];
54+
}
55+
}
56+
else if (command->message)
57+
{
58+
for (i = 0; i < (int) strlen(command->message); ++i)
59+
{
60+
data[i] = command->message[i];
61+
}
62+
}
63+
}
64+
65+
if (command->command == SET_SLOW)
66+
{
67+
_i2c_slowTx = (command->isDelayBetweenBytes) ? 1 : 0;
68+
}
69+
else
70+
{
71+
command->isDelayBetweenBytes = _i2c_slowTx;
72+
}
73+
74+
if (_i2c_address != 0x00)
75+
{
76+
if (command->command == SEND)
77+
{
78+
if (command->tx_len)
79+
{
80+
retVal += I2CSendData(_i2c_address, data, command->tx_len,
81+
_i2c_slowTx);
82+
}
83+
84+
else if (command->message)
85+
{
86+
retVal += I2CSendData(_i2c_address, data,
87+
strlen(command->message), _i2c_slowTx);
88+
}
89+
}
90+
else if (command->command == SENDNRECV)
91+
{
92+
93+
if (command->tx_len)
94+
{
95+
retVal += I2CSendnRecvData(_i2c_address, data,
96+
command->tx_len, command->rx, command->rx_len,
97+
_i2c_slowTx);
98+
}
99+
100+
else if (command->message)
101+
{
102+
retVal += I2CSendnRecvData(_i2c_address, data,
103+
strlen(command->message), command->rx,
104+
command->rx_len, _i2c_slowTx);
105+
}
106+
}
107+
}
108+
else
109+
{
110+
retVal = 3;
111+
}
112+
}
113+
114+
if (!retVal)
115+
_delay_ms(150);
116+
117+
return retVal;
118+
}
119+
120+
uint8_t I2CConsoleGetCurrentAddress()
121+
{
122+
consoleInit();
123+
return _i2c_address;
124+
}
125+
126+
uint8_t I2CConsoleGetSlowSendingStatus()
127+
{
128+
consoleInit();
129+
return _i2c_slowTx;
130+
}

0 commit comments

Comments
 (0)