GRBL-Advanced/Libraries/Printf/Print.c

155 wiersze
2.7 KiB
C
Czysty Zwykły widok Historia

2017-05-30 22:12:10 +00:00
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
2017-05-30 22:12:10 +00:00
#include "Print.h"
#include "Config.h"
#include "Usart.h"
2017-06-01 17:44:37 +00:00
#include "FIFO_USART.h"
#include "GrIP.h"
#include "Platform.h"
2017-05-30 22:12:10 +00:00
#define MAX_BUFFER_SIZE 128
2020-04-15 18:26:40 +00:00
static char buf[512] = {0};
static uint16_t buf_idx = 0;
void Printf_Init(void)
2017-05-30 22:12:10 +00:00
{
2021-01-19 08:45:27 +00:00
Usart_Init(STDOUT, BAUD_RATE);
2017-05-30 22:12:10 +00:00
}
2018-11-07 18:00:32 +00:00
2017-05-30 22:12:10 +00:00
int Printf(const char *str, ...)
{
2021-01-19 08:45:27 +00:00
char buffer[MAX_BUFFER_SIZE];
uint8_t idx = 0;
2017-05-30 22:12:10 +00:00
2021-01-19 08:45:27 +00:00
va_list vl;
va_start(vl, str);
int i = vsnprintf(buffer, MAX_BUFFER_SIZE, str, vl);
2021-01-19 08:45:27 +00:00
if(i > MAX_BUFFER_SIZE)
{
i = MAX_BUFFER_SIZE;
}
2017-05-30 22:12:10 +00:00
2018-11-07 18:00:32 +00:00
for(uint8_t j = 0; j < i; j++)
2018-11-07 18:00:32 +00:00
{
buf[buf_idx++] = buffer[j];
}
//Usart_Write(STDOUT, false, buffer, i);
2017-05-30 22:12:10 +00:00
va_end(vl);
2017-05-30 22:12:10 +00:00
2018-11-07 18:00:32 +00:00
// Return number of sent bytes
2021-01-19 08:45:27 +00:00
return idx;
2017-05-30 22:12:10 +00:00
}
int8_t Getc(char *c)
{
2021-01-19 08:45:27 +00:00
if(FifoUsart_Get(STDOUT_NUM, USART_DIR_RX, c) == 0)
{
return 0;
}
2021-01-19 08:45:27 +00:00
return -1;
}
int Putc(const char c)
{
buf[buf_idx++] = c;
//Usart_Put(STDOUT, false, c);
2021-01-19 08:45:27 +00:00
return 0;
}
void Printf_Flush(void)
{
if(buf_idx == 0)
{
// No data to send
return;
}
#ifdef ETH_IF
Pdu_t data;
data.Data = (uint8_t*)buf;
data.Length = buf_idx;
uint8_t ret = GrIP_Transmit(MSG_DATA_NO_RESPONSE, 0, &data);
(void)ret; // TODO: Handle transmit error
#else
Usart_Write(STDOUT, false, buf, buf_idx);
#endif
memset(buf, 0, 512);
buf_idx = 0;
}
2017-05-30 22:12:10 +00:00
// Convert float to string by immediately converting to a long integer, which contains
// more digits than a float. Number of decimal places, which are tracked by a counter,
// may be set by the user. The integer is then efficiently converted to a string.
void Printf_Float(float n, uint8_t decimal_places)
2017-05-30 22:12:10 +00:00
{
2021-01-19 08:45:27 +00:00
if(n < 0)
{
Putc('-');
n = -n;
}
uint8_t decimals = decimal_places;
while(decimals >= 2) // Quickly convert values expected to be E0 to E-4.
{
n *= 100;
decimals -= 2;
}
if(decimals)
{
n *= 10;
}
n += 0.5; // Add rounding factor. Ensures carryover through entire value.
// Generate digits backwards and store in string.
unsigned char buf[13];
uint8_t i = 0;
uint32_t a = (long)n;
while(a > 0)
{
buf[i++] = (a % 10) + '0'; // Get digit
a /= 10;
}
while(i < decimal_places)
{
buf[i++] = '0'; // Fill in zeros to decimal point for (n < 1)
}
if(i == decimal_places) // Fill in leading zero, if needed.
{
buf[i++] = '0';
}
// Print the generated string.
for(; i > 0; i--)
{
if(i == decimal_places)
{
Putc('.');
} // Insert decimal point in right place.
Putc(buf[i-1]);
}
2017-05-30 22:12:10 +00:00
}