fprintf Library
fprintf
.fprintf
Library
This is a simple implementation of fprintf
.
{{< hint color=“info” >}}
This library is provided for source code compatibility. For new implementations, it is recommended to use TWENETmcu/printf
.
{{< /hint >}}
Reference
TWENETmcu/printf
-printf
library (open source)TWENETstgs
-TWE_fprintf()
, etc.
tsFILE
A structure that defines the output destination specified by vfPrintf()
and vPutChar()
.
Members
Type | Name | Description |
---|---|---|
uint8 | u8Device | Specifies the serial port (E_AHI_UART_0 or E_AHI_UART_1 ). |
bool_t (*) (uint8 u8Device, uint8 u8Char) | bPutChar | A function pointer for output. For the SERIAL library, SERIAL_bTxChar() is prepared, so specify that. |
{% hint style=“info” %}
SERIAL_bTxChar()
puts the byte passed as u8Char
into the FIFO queue within the SERIAL
library.
By preparing your own output function, you can use this for outputting strings to destinations other than UART. {% endhint %}
Sample code
#include "serial.h"
#include "fprintf.h"
tsFILE sSerStream;
tsSerialPortSetup sSerPort;
void vSerialInit(uint32 u32Baud, tsUartOpt *pUartOpt) {
// initialize sSerPort
...
SERIAL_vInit(&sSerPort);
// for vfPrintf()
sSerStream.bPutChar = SERIAL_bTxChar;
sSerStream.u8Device = E_AHI_UART_0;
}
void vSerOut() {
vfPrintf(&sSerStream, "HELLO!");
}
Here is an example of the character LCD output code.
#include "serial.h"
#include "fprintf.h"
tsFILE sLcdStream;
// handle LCD display
PUBLIC bool_t LCD_bTxChar(uint8 u8Device, uint8 u8Data) {
int i;
switch (u8Data) {
case '\n':
...
}
void vInitHardware() {
/* Initialise the LCD */
vLcdReset(3, 0);
/* register for vfPrintf() */
sLcdStream.bPutChar = LCD_bTxChar;
sLcdStream.u8Device = 0xFF;
}
void vSomeOutput() {
vfPrintf(&sLcdStream, "Hello World!\n");
}
vfPrintf()
Explanation
This function outputs to the destination specified by the tsFILE
structure (UART) using printf
format.
Arguments
Type | Name | Description |
---|---|---|
tsFILE* | psStream | Output destination |
const char * | pcFormat | Output format |
… | Variable arguments |
Supported Formats
s | String |
---|---|
d | Integer (up to 32 bits) |
u | Unsigned integer (up to 32 bits) |
x | Hexadecimal. a-f are lowercase. |
X | Hexadecimal. A-F are uppercase. |
b | Bit sequence |
Return Value
None.
Sample
void cbToCoNet_vMain(void) {
while (!SERIAL_bRxQueueEmpty(sSerPort.u8SerialPort)) {
int16 i16Char;
i16Char = SERIAL_i16RxChar(sSerPort.u8SerialPort);
vfPrintf(&sSerStream, "\n\r## [%c] --> ", i16Char);
SERIAL_vFlush(sSerStream.u8Device);
...
}
}
vPutChar()
Explanation
This function outputs one byte to the destination specified by the tsFILE
structure (UART).
Arguments
Type | Name | Description |
---|---|---|
tsFILE* | psStream | Output destination |
uint8 | u8Char | Output byte |
Return Value
None
Sample
#define IS_ASC(c) ((c) >= 0x20 && (c) <= 0x7e)
void cbToCoNet_vRxEvent(tsRxDataApp *pRx) {
uint8 u8i;
vfPrintf(&sSerStream, LB"RX(len=%d):[", pRx->u8Len);
for (i = 0; i < pRx->u8Len; i++) {
uint8 c = pRx->auData[i];
vPutChar(&sSerStream, IS_ASC(c) ? c : '.');
}
}