From a5902f72e7f1cdaed73357499c785501904f9dec Mon Sep 17 00:00:00 2001 From: Temporary User Date: Wed, 20 Nov 2019 16:15:01 -0600 Subject: [PATCH 1/5] remove prints --- BSP/bsp_error_handler.c | 8 +++---- BSP/bsp_error_handler.h | 4 ++-- BSP/bsp_gpio.h | 1 - BSP/bsp_imu.h | 1 - BSP/bsp_key.c | 4 +++- BSP/bsp_key.h | 1 - BSP/bsp_oled.h | 1 - BSP/bsp_print.c | 24 +++++++------------ BSP/bsp_print.h | 52 ---------------------------------------- Libraries/data_process.c | 29 ++++++++++++---------- Libraries/imu_onboard.c | 26 ++++++++++---------- Libraries/motor.c | 41 +++++++++++++++---------------- Libraries/motor.h | 1 - Libraries/oled_module.h | 4 +++- Libraries/referee.c | 4 +++- Libraries/tx2.h | 2 ++ Tests/rm_test.h | 4 ++-- Tests/test_bsp_adc.c | 2 +- Tests/test_bsp_can.c | 12 +++++----- Tests/test_bsp_can.h | 1 - Tests/test_bsp_imu.h | 1 - Tests/test_bsp_power.c | 2 +- Tests/test_bsp_tof.c | 5 ++-- Tests/test_chassis.c | 1 - Tests/test_dbus.c | 46 ++++++++++++++++++----------------- Tests/test_dbus.h | 1 - Tests/test_key.c | 6 +++-- Tests/test_key.h | 1 - Tests/test_motor.c | 2 -- Tests/test_oled_module.c | 14 +++++------ Tests/test_pid.c | 1 - Tests/test_sdio.c | 10 ++++---- 32 files changed, 129 insertions(+), 183 deletions(-) delete mode 100644 BSP/bsp_print.h diff --git a/BSP/bsp_error_handler.c b/BSP/bsp_error_handler.c index 3962f5c..46cb45a 100644 --- a/BSP/bsp_error_handler.c +++ b/BSP/bsp_error_handler.c @@ -27,9 +27,9 @@ #include "bsp_error_handler.h" void bsp_error_handler(const char* func, int line, char* msg) { - print("[ERROR at "); - print("%s:", func); - print("%d] ", line); - print("%s\r\n", msg); + printf("[ERROR at "); + printf("%s:", func); + printf("%d] ", line); + printf("%s\r\n", msg); return; } diff --git a/BSP/bsp_error_handler.h b/BSP/bsp_error_handler.h index 56ab258..ec38494 100644 --- a/BSP/bsp_error_handler.h +++ b/BSP/bsp_error_handler.h @@ -27,7 +27,7 @@ #ifndef _BSP_ERROR_HANDLER_H_ #define _BSP_ERROR_HANDLER_H_ -#include "bsp_print.h" +#include /** * @ingroup bsp @@ -35,7 +35,7 @@ * @{ */ -#define BSP_DEBUG print("[DEBUG] %s:%d ", __FUNCTION__, __LINE__) +#define BSP_DEBUG printf("[DEBUG] %s:%d ", __FUNCTION__, __LINE__) /** * Handle error condition printf etc. diff --git a/BSP/bsp_gpio.h b/BSP/bsp_gpio.h index b9aaa2f..727437d 100644 --- a/BSP/bsp_gpio.h +++ b/BSP/bsp_gpio.h @@ -29,7 +29,6 @@ #include "stm32f4xx_hal.h" #include "bsp_error_handler.h" -#include "bsp_print.h" #include "bsp_key.h" #include "main.h" diff --git a/BSP/bsp_imu.h b/BSP/bsp_imu.h index c32eff0..39f6f17 100644 --- a/BSP/bsp_imu.h +++ b/BSP/bsp_imu.h @@ -30,7 +30,6 @@ #include "bsp_mpu6500_reg.h" #include "bsp_ist8310_reg.h" #include "bsp_error_handler.h" -#include "bsp_print.h" #include "spi.h" #include "main.h" diff --git a/BSP/bsp_key.c b/BSP/bsp_key.c index cb4ef62..a9c46b4 100644 --- a/BSP/bsp_key.c +++ b/BSP/bsp_key.c @@ -24,6 +24,8 @@ * @log 2018-05-27 nickelliang */ +#include + #include "bsp_key.h" static uint32_t key_previous_tick; @@ -91,6 +93,6 @@ uint16_t key_pressed_time(void) { } void wait_until_key_pressed(void) { - print("Please press the key to continue...\r\n"); + printf("Please press the key to continue...\r\n"); while (!key_pressed_once()); } diff --git a/BSP/bsp_key.h b/BSP/bsp_key.h index 10baf5c..cb10777 100644 --- a/BSP/bsp_key.h +++ b/BSP/bsp_key.h @@ -29,7 +29,6 @@ #include "stm32f4xx_hal.h" #include "main.h" -#include "bsp_print.h" /** * @ingroup bsp diff --git a/BSP/bsp_oled.h b/BSP/bsp_oled.h index 5f2ab41..7092e54 100644 --- a/BSP/bsp_oled.h +++ b/BSP/bsp_oled.h @@ -29,7 +29,6 @@ #include "stm32f4xx_hal.h" #include "bsp_error_handler.h" -#include "bsp_print.h" #include "bsp_config.h" #include "bsp_adc.h" #include "spi.h" diff --git a/BSP/bsp_print.c b/BSP/bsp_print.c index 03f0fc5..00c3698 100644 --- a/BSP/bsp_print.c +++ b/BSP/bsp_print.c @@ -16,21 +16,15 @@ * along with this program. If not, see . *************************************************************************/ -#include "bsp_print.h" +#include "bsp_config.h" + #include "cmsis_os.h" -#include -#include +#include "stm32f4xx_hal.h" -void print(const char *fmt, ...) { - if (PRINT_TO_UART == 1) { - char *printf_temp = pvPortMalloc(512*sizeof(char)); - va_list args; - va_start(args, fmt); - vsprintf(printf_temp, fmt, args); - va_end(args); - HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)printf_temp, strlen(printf_temp), 200); - vPortFree(printf_temp); - } - if (PRINT_TO_SD == 1) - return; +int __io_putchar(int ch) { + if (HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)&ch, 1, 0xffff) == HAL_OK) + return ch; + else + return 0; } + diff --git a/BSP/bsp_print.h b/BSP/bsp_print.h deleted file mode 100644 index 29f462e..0000000 --- a/BSP/bsp_print.h +++ /dev/null @@ -1,52 +0,0 @@ -/************************************************************************** - * Copyright (C) 2018 - * Illini RoboMaster @ University of Illinois at Urbana-Champaign. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *************************************************************************/ - -/** - * @author Yixiao Sun - * @date 2017-12-20 - * @file bsp_print.h - * @brief - * @log 2018-04-16 nickelliang - */ - -#ifndef _BSP_PRINT_H_ -#define _BSP_PRINT_H_ - -#include "stm32f4xx_hal.h" -#include "bsp_config.h" -#include - -/** - * @ingroup bsp - * @defgroup bsp_print BSP Print - * @{ - */ - -/** - * Implementation of printf equivalent in embedded system - * - * @param fmt formatted string - * @param ... variable length arguments - * @author Yixiao Sun - * @date 2017-12-20 - */ -void print(const char *fmt, ...); - -/** @} */ - -#endif // _BSP_PRINT_ diff --git a/Libraries/data_process.c b/Libraries/data_process.c index 54f45e4..d8284d2 100644 --- a/Libraries/data_process.c +++ b/Libraries/data_process.c @@ -24,6 +24,9 @@ * @log 2018-05-26 nickelliang */ +#include +#include + #include "data_process.h" data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, uint32_t fifo_size, uint16_t buffer_size, uint8_t sof, dispatcher_func_t dispatcher_func, void *source_struct, osMutexId tx_mutex, packer_func_t packer_func) { @@ -64,8 +67,8 @@ data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, ui source->buff[1] = source->buff[0] + source->buff_size; #ifdef DEBUG BSP_DEBUG; - print("source->buff[0] 0x%08x ", source->buff[0]); - print("source->buff[1] 0x%08x \r\n", source->buff[1]); + printf("source->buff[0] 0x%08x ", source->buff[0]); + printf("source->buff[1] 0x%08x \r\n", source->buff[1]); #endif source->frame_packet = (uint8_t*)pvPortMalloc(fifo_size); @@ -158,8 +161,8 @@ static uint8_t buffer_to_fifo(data_process_t *source) { return 0; } if (write_len != fifo_s_puts(source->data_fifo, &buff[source->read_index], write_len)) { - print("write index: %d\r\n", write_index); - print("memory target: %d\r\n", dma_memory_target); + printf("write index: %d\r\n", write_index); + printf("memory target: %d\r\n", dma_memory_target); bsp_error_handler(__FUNCTION__, __LINE__, "FIFO overflow, need to resize."); return 0; } @@ -174,11 +177,11 @@ static uint8_t buffer_to_fifo(data_process_t *source) { #ifdef DEBUG BSP_DEBUG; - print("Total write len: %d\r\n", total_write_len); - print("Mem target: %u\r\n", dma_memory_target); - print("Mem remain: %d\r\n", dma_remain_space); - print("Write index: %d\r\n", write_index); - print("\r\n"); + printf("Total write len: %d\r\n", total_write_len); + printf("Mem target: %u\r\n", dma_memory_target); + printf("Mem remain: %d\r\n", dma_remain_space); + printf("Write index: %d\r\n", write_index); + printf("\r\n"); #endif /* General write condition */ @@ -199,9 +202,9 @@ static uint8_t fifo_to_struct(data_process_t *source) { while (!fifo_is_empty(source->data_fifo)) { byte = fifo_s_peek(source->data_fifo, 0); // Peek head #ifdef DEBUG - print("Byte: %02x\r\n", byte); - print("Used: %d\r\n", source->data_fifo->used); - print("Free: %d\r\n", source->data_fifo->free); + printf("Byte: %02x\r\n", byte); + printf("Used: %d\r\n", source->data_fifo->used); + printf("Free: %d\r\n", source->data_fifo->free); #endif if (byte != source->sof) { fifo_s_get(source->data_fifo); @@ -215,7 +218,7 @@ static uint8_t fifo_to_struct(data_process_t *source) { } #ifdef DEBUG BSP_DEBUG; - print("End of loop.\r\n"); + printf("End of loop.\r\n"); #endif return flag; } diff --git a/Libraries/imu_onboard.c b/Libraries/imu_onboard.c index 0d81e5c..93e3980 100644 --- a/Libraries/imu_onboard.c +++ b/Libraries/imu_onboard.c @@ -27,25 +27,25 @@ void print_mpu_data(imu_t* imu) { return; } mpu6500_get_data(imu); - print("[DECODED MPU] "); - print("Acce X %f \tY %f \t", imu->acce.x, imu->acce.y); - print("Z %f \t| ", imu->acce.z); - print("Gyro X %f \tY %f \t", imu->gyro.x, imu->gyro.y); - print("Z %f \t| ", imu->gyro.z); - print("Temp %f\r\n", imu->temp); + printf("[DECODED MPU] "); + printf("Acce X %f \tY %f \t", imu->acce.x, imu->acce.y); + printf("Z %f \t| ", imu->acce.z); + printf("Gyro X %f \tY %f \t", imu->gyro.x, imu->gyro.y); + printf("Z %f \t| ", imu->gyro.z); + printf("Temp %f\r\n", imu->temp); } void print_imu_data(void){ - print("Angle X %.2f \tY %.2f", imuBoard.angle[0], imuBoard.angle[1]); - print(" \tZ %.2f \t| ", imuBoard.angle[2]); - print("\r\n"); - print("ZBias X %.2f \tY %.2f", imuBoard.angle_zero_bias[0], imuBoard.angle_zero_bias[1]); - print(" \tZ %.2f \t| ", imuBoard.angle_zero_bias[2]); - print("\r\n"); + printf("Angle X %.2f \tY %.2f", imuBoard.angle[0], imuBoard.angle[1]); + printf(" \tZ %.2f \t| ", imuBoard.angle[2]); + printf("\r\n"); + printf("ZBias X %.2f \tY %.2f", imuBoard.angle_zero_bias[0], imuBoard.angle_zero_bias[1]); + printf(" \tZ %.2f \t| ", imuBoard.angle_zero_bias[2]); + printf("\r\n"); } void onboard_imu_lib_init(void){ - print("Initializing and calibrating onboard imu\r\n"); + printf("Initializing and calibrating onboard imu\r\n"); for(int i = 0; i < 3; ++i){ imuBoard.angle[i] = 0; for(int j = 0; j < 2; ++j){ diff --git a/Libraries/motor.c b/Libraries/motor.c index 6330f31..98f80b1 100644 --- a/Libraries/motor.c +++ b/Libraries/motor.c @@ -19,6 +19,7 @@ #include "motor.h" #include "utils.h" #include +#include /* private function starts from here */ @@ -34,12 +35,12 @@ static void get_3508_data(motor_t* motor, uint8_t buf[CAN_DATA_SIZE]) { } static void print_3508_data(motor_t* motor) { - print("== 3508 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); - print("Angle %d\n", motor->as.m3508.angle); - print("Current %d\n", motor->as.m3508.current_get); - print("Speed %d\n", motor->as.m3508.speed_rpm); - print("Temperature %u\n", motor->as.m3508.temperature); - print("================================\r\n"); + printf("== 3508 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); + printf("Angle %d\n", motor->as.m3508.angle); + printf("Current %d\n", motor->as.m3508.current_get); + printf("Speed %d\n", motor->as.m3508.speed_rpm); + printf("Temperature %u\n", motor->as.m3508.temperature); + printf("================================\r\n"); } static void get_6623_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { @@ -52,11 +53,11 @@ static void get_6623_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { } static void print_6623_data(motor_t* motor) { - print("== 6623 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); - print("Angle %d\r\n", motor->as.m6623.angle); - print("Current %d\r\n", motor->as.m6623.current_get); - print("Set Current %d\r\n", motor->as.m6623.current_set); - print("================================\r\n"); + printf("== 6623 at CAN bus %u node %x ==\r\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); + printf("Angle %d\r\n", motor->as.m6623.angle); + printf("Current %d\r\n", motor->as.m6623.current_get); + printf("Set Current %d\r\n", motor->as.m6623.current_set); + printf("================================\r\n"); } static void get_3510_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { @@ -67,10 +68,10 @@ static void get_3510_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { } static void print_3510_data(motor_t* motor) { - print("== 3510 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); - print("Angle %d\n", motor->as.m3510.angle); - print("Current %d\n", motor->as.m3510.current_get); - print("================================\n"); + printf("== 3510 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); + printf("Angle %d\n", motor->as.m3510.angle); + printf("Current %d\n", motor->as.m3510.current_get); + printf("================================\n"); } static void get_2006_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { @@ -83,11 +84,11 @@ static void get_2006_data(motor_t *motor, uint8_t buf[CAN_DATA_SIZE]) { } static void print_2006_data(motor_t* motor) { - print("== 2006 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); - print("Angle %d\n", motor->as.m2006.angle); - print("Current %d\n", motor->as.m2006.current_get); - print("Speed %d\n", motor->as.m2006.speed_rpm); - print("================================\n"); + printf("== 2006 at CAN bus %u node %x ==\n", motor->as.mdjican.can_id, motor->as.mdjican.rx_id); + printf("Angle %d\n", motor->as.m2006.angle); + printf("Current %d\n", motor->as.m2006.current_get); + printf("Speed %d\n", motor->as.m2006.speed_rpm); + printf("================================\n"); } static uint8_t match_id(uint16_t *old_id, uint16_t new_id) { diff --git a/Libraries/motor.h b/Libraries/motor.h index b8ce320..e4e9f50 100644 --- a/Libraries/motor.h +++ b/Libraries/motor.h @@ -37,7 +37,6 @@ #include "bsp_can.h" #include "bsp_pwm.h" #include "bsp_error_handler.h" -#include "bsp_print.h" #define CAN1_ID 1 #define CAN2_ID 2 diff --git a/Libraries/oled_module.h b/Libraries/oled_module.h index 38b4be1..6774105 100644 --- a/Libraries/oled_module.h +++ b/Libraries/oled_module.h @@ -27,8 +27,10 @@ #ifndef _OLED_MODULE_H_ #define _OLED_MODULE_H_ -#include "bsp_oled.h" #include +#include + +#include "bsp_oled.h" /* TODO: Function documentation incomplete */ diff --git a/Libraries/referee.c b/Libraries/referee.c index dc52caa..b4d65b5 100644 --- a/Libraries/referee.c +++ b/Libraries/referee.c @@ -24,6 +24,8 @@ * @log 2018-05-26 nickelliang */ +#include + #include "referee.h" data_process_t *referee_process; @@ -42,7 +44,7 @@ uint8_t referee_dispatcher(void* target_struct, data_process_t* process_struct) /* @todo Need to consider racing condition here */ #ifdef DEBUG BSP_DEBUG; - print("Enter referee dispatcher.\r\n"); + printf("Enter referee dispatcher.\r\n"); #endif referee_t* referee = target_struct; data_process_t* source = process_struct; diff --git a/Libraries/tx2.h b/Libraries/tx2.h index e79c8cb..456027d 100644 --- a/Libraries/tx2.h +++ b/Libraries/tx2.h @@ -27,6 +27,8 @@ #ifndef _TX2_H_ #define _TX2_H_ +#include + #include "bsp_error_handler.h" #include "bsp_uart.h" #include "bsp_config.h" diff --git a/Tests/rm_test.h b/Tests/rm_test.h index aaf121c..cd86ef0 100644 --- a/Tests/rm_test.h +++ b/Tests/rm_test.h @@ -46,9 +46,9 @@ #define RM_MAIN 1 #define TEST_HEADER \ - print("[TEST] Running %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__) + printf("[TEST] Running %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__) #define TEST_OUTPUT(name, result) \ - print("[TEST] %s : %s\n", name, (result) ? "PASS" : "FAIL"); + printf("[TEST] %s : %s\n", name, (result) ? "PASS" : "FAIL"); /* Test options */ #define RUN_IN RM_MAIN // RM_RTOS : Run test in RTOS default task @todo Not yet implemented diff --git a/Tests/test_bsp_adc.c b/Tests/test_bsp_adc.c index 540749c..8a0d617 100644 --- a/Tests/test_bsp_adc.c +++ b/Tests/test_bsp_adc.c @@ -25,7 +25,7 @@ void test_bsp_adc(void) { while (1) { uint16_t chan0 = adc_get_val(adc1); uint16_t chan1 = adc_get_val(adc2); - print("channel 0: %d channel 1: %d\r\n", chan0, chan1); + printf("channel 0: %d channel 1: %d\r\n", chan0, chan1); osDelay(300); } } diff --git a/Tests/test_bsp_can.c b/Tests/test_bsp_can.c index 9b03d1a..29ce5d9 100644 --- a/Tests/test_bsp_can.c +++ b/Tests/test_bsp_can.c @@ -36,18 +36,18 @@ uint8_t test_bsp_can(void) { for (i = 0; i < CAN_TEST_COUNT; i++) { size_t device; if (PRINT_CAN_1) { - print("===CAN1===\r\n"); + printf("===CAN1===\r\n"); for (device = 0; device < CAN1_DEVICE_NUM; device++) { - print("Node %d: %08x\r\n", device, can1_rx_buffer[device]); + printf("Node %d: %08x\r\n", device, can1_rx_buffer[device]); } - print("==========\r\n"); + printf("==========\r\n"); } if (PRINT_CAN_2) { - print("===CAN2===\r\n"); + printf("===CAN2===\r\n"); for (device = 0; device < CAN2_DEVICE_NUM; device++) { - print("Node %d: %08x\r\n", device, can2_rx_buffer[device]); + printf("Node %d: %08x\r\n", device, can2_rx_buffer[device]); } - print("==========\r\n"); + printf("==========\r\n"); } osDelay(100); diff --git a/Tests/test_bsp_can.h b/Tests/test_bsp_can.h index 09b96d9..980af05 100644 --- a/Tests/test_bsp_can.h +++ b/Tests/test_bsp_can.h @@ -28,7 +28,6 @@ #define _TEST_BSP_CAN_H_ #include "bsp_can.h" -#include "bsp_print.h" #define CAN_TEST_COUNT 100000 #define PRINT_CAN_1 1 // Set to 1 to print can 1 data diff --git a/Tests/test_bsp_imu.h b/Tests/test_bsp_imu.h index de869f2..271b6b5 100644 --- a/Tests/test_bsp_imu.h +++ b/Tests/test_bsp_imu.h @@ -28,7 +28,6 @@ #define _TEST_BSP_IMU_H_ #include "bsp_imu.h" -#include "bsp_print.h" #include "imu_onboard.h" #define IMU_TEST_COUNT 100000 diff --git a/Tests/test_bsp_power.c b/Tests/test_bsp_power.c index aba765f..2f3b34d 100644 --- a/Tests/test_bsp_power.c +++ b/Tests/test_bsp_power.c @@ -26,7 +26,7 @@ void test_bsp_power(void) { power_module_calibrate(referee_info.power_heat_data.chassis_volt, referee_info.power_heat_data.chassis_current); while (1) { - print("voltage: %.3f current %.3f\r\n", + printf("voltage: %.3f current %.3f\r\n", get_volt(), get_current()); osDelay(50); } diff --git a/Tests/test_bsp_tof.c b/Tests/test_bsp_tof.c index b85d122..cd2eb52 100644 --- a/Tests/test_bsp_tof.c +++ b/Tests/test_bsp_tof.c @@ -1,5 +1,6 @@ +#include + #include "test_bsp_tof.h" -#include "bsp_print.h" #include "cmsis_os.h" void test_bsp_tof(void) { @@ -10,7 +11,7 @@ void test_bsp_tof(void) { while (1) { dist1 = get_vl53l0x_dist_milli(tof1); dist2 = get_vl53l0x_dist_milli(tof2); - print("dist1: %d dist2: %d\r\n", dist1, dist2); + printf("dist1: %d dist2: %d\r\n", dist1, dist2); osDelay(50); } } diff --git a/Tests/test_chassis.c b/Tests/test_chassis.c index f03c67b..3966b72 100644 --- a/Tests/test_chassis.c +++ b/Tests/test_chassis.c @@ -20,7 +20,6 @@ #include "test_pid.h" #include "motor.h" #include "pid.h" -#include "bsp_print.h" #include "stdlib.h" #include "dbus.h" #include "test_mouse.h" diff --git a/Tests/test_dbus.c b/Tests/test_dbus.c index bfb62f0..d7d9d38 100644 --- a/Tests/test_dbus.c +++ b/Tests/test_dbus.c @@ -24,6 +24,8 @@ * @log 2018-04-17 nickelliang */ +#include + #include "test_dbus.h" uint8_t test_dbus(void) { @@ -31,28 +33,28 @@ uint8_t test_dbus(void) { size_t i; dbus_t* rc = dbus_get_struct(); for (i = 0; i < DBUS_TEST_COUNT; i++) { - print("CH0 %03d CH1 %03d ", rc->ch0, rc->ch1); - print("CH2 %03d CH3 %03d ", rc->ch2, rc->ch3); - print("SL %u SR %u ", rc->swl, rc->swr); - print("MX %04d MY %04d ", rc->mouse.x, rc->mouse.y); - print("ML %u MR %u ", rc->mouse.l, rc->mouse.r); - if (rc->key.bit.SHIFT) print("SHIFT "); - if (rc->key.bit.CTRL) print("CTRL "); - if (rc->key.bit.W) print("W "); - if (rc->key.bit.A) print("A "); - if (rc->key.bit.S) print("S "); - if (rc->key.bit.D) print("D "); - if (rc->key.bit.Q) print("Q "); - if (rc->key.bit.E) print("E "); - if (rc->key.bit.R) print("R "); - if (rc->key.bit.F) print("F "); - if (rc->key.bit.G) print("G "); - if (rc->key.bit.Z) print("Z "); - if (rc->key.bit.X) print("X "); - if (rc->key.bit.C) print("C "); - if (rc->key.bit.V) print("V "); - if (rc->key.bit.B) print("B "); - print("\r\n"); + printf("CH0 %03d CH1 %03d ", rc->ch0, rc->ch1); + printf("CH2 %03d CH3 %03d ", rc->ch2, rc->ch3); + printf("SL %u SR %u ", rc->swl, rc->swr); + printf("MX %04d MY %04d ", rc->mouse.x, rc->mouse.y); + printf("ML %u MR %u ", rc->mouse.l, rc->mouse.r); + if (rc->key.bit.SHIFT) printf("SHIFT "); + if (rc->key.bit.CTRL) printf("CTRL "); + if (rc->key.bit.W) printf("W "); + if (rc->key.bit.A) printf("A "); + if (rc->key.bit.S) printf("S "); + if (rc->key.bit.D) printf("D "); + if (rc->key.bit.Q) printf("Q "); + if (rc->key.bit.E) printf("E "); + if (rc->key.bit.R) printf("R "); + if (rc->key.bit.F) printf("F "); + if (rc->key.bit.G) printf("G "); + if (rc->key.bit.Z) printf("Z "); + if (rc->key.bit.X) printf("X "); + if (rc->key.bit.C) printf("C "); + if (rc->key.bit.V) printf("V "); + if (rc->key.bit.B) printf("B "); + printf("\r\n"); } return ret; } diff --git a/Tests/test_dbus.h b/Tests/test_dbus.h index 28fd573..26d3370 100644 --- a/Tests/test_dbus.h +++ b/Tests/test_dbus.h @@ -28,7 +28,6 @@ #define _TEST_DBUS_H_ #include "dbus.h" -#include "bsp_print.h" #define DBUS_TEST_COUNT 100000 diff --git a/Tests/test_key.c b/Tests/test_key.c index 90bfb94..8b5d35f 100644 --- a/Tests/test_key.c +++ b/Tests/test_key.c @@ -24,11 +24,13 @@ * @log 2018-05-27 nickelliang */ +#include + #include "test_key.h" uint8_t test_key(void) { - print("Please press the key to continue...\r\n"); + printf("Please press the key to continue...\r\n"); while (!key_pressed_once()); - print("Key pressed!\r\n"); + printf("Key pressed!\r\n"); return 1; } diff --git a/Tests/test_key.h b/Tests/test_key.h index 28adacd..a519029 100644 --- a/Tests/test_key.h +++ b/Tests/test_key.h @@ -28,7 +28,6 @@ #define _TEST_KEY_H_ #include "bsp_key.h" -#include "bsp_print.h" /** * Wait until a key pressed. diff --git a/Tests/test_motor.c b/Tests/test_motor.c index ba15994..c6e3d02 100644 --- a/Tests/test_motor.c +++ b/Tests/test_motor.c @@ -17,7 +17,6 @@ #include "test_motor.h" #include "motor.h" -#include "bsp_print.h" void test_motor() { // motor_feedback(); @@ -53,7 +52,6 @@ void test_motor_3508(uint8_t rotate) { for (i = 0; i < 1000; i++) { get_motor_data(&motor); print_motor_data(&motor); - print("Target: %d Error: %d\n", 400, get_angle_err(&motor, 400)); osDelay(20); } } diff --git a/Tests/test_oled_module.c b/Tests/test_oled_module.c index 07a13e1..c927c6a 100644 --- a/Tests/test_oled_module.c +++ b/Tests/test_oled_module.c @@ -22,7 +22,7 @@ void test_oled_button_raw(void) { button = oled_button_get_val(); oled_clear(OLED_OFF); oled_printf("Button value %u\n", button); - // print("Button value %u.\r\n", button); + // printf("Button value %u.\r\n", button); } } @@ -31,32 +31,32 @@ void test_oled_button(void) { for (count = 0; count < 100000; count++) { oled_button_status_t button_state = oled_get_button_status(); if (button_state == PRESS_MIDDLE) { - // print("MIDDLE pressed.\r\n"); + // printf("MIDDLE pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("MIDDLE pressed.\n"); } else if (button_state == PRESS_LEFT) { - // print("LEFT pressed.\r\n"); + // printf("LEFT pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("LEFT pressed.\n"); } else if (button_state == PRESS_RIGHT) { - // print("RIGHT pressed.\r\n"); + // printf("RIGHT pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("RIGHT pressed.\n"); } else if (button_state == PRESS_UP) { - // print("UP pressed.\r\n"); + // printf("UP pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("UP pressed.\n"); } else if (button_state == PRESS_DOWN) { - // print("DOWN pressed.\r\n"); + // printf("DOWN pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("DOWN pressed.\n"); } else if (button_state == PRESS_STATIC) { - // print("STATIC pressed.\r\n"); + // printf("STATIC pressed.\r\n"); // oled_clear(OLED_OFF); oled_printf("STATIC pressed.\n"); } diff --git a/Tests/test_pid.c b/Tests/test_pid.c index a76bc06..451d2cf 100644 --- a/Tests/test_pid.c +++ b/Tests/test_pid.c @@ -19,7 +19,6 @@ #include "test_pid.h" #include "motor.h" #include "pid.h" -#include "bsp_print.h" #include "stdlib.h" #include "dbus.h" #include "utils.h" diff --git a/Tests/test_sdio.c b/Tests/test_sdio.c index 43238c2..ca32ece 100644 --- a/Tests/test_sdio.c +++ b/Tests/test_sdio.c @@ -40,19 +40,19 @@ uint8_t test_sdio(void) { // /* Register the file system object to the FatFs module and create a FAT file system (format) on the logical drive */ // ret = f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer)); // if (ret == FR_OK) { - print("SDIO initialize success.\r\n"); + printf("SDIO initialize success.\r\n"); if (f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK) { - print("SDIO mount success.\r\n"); + printf("SDIO mount success.\r\n"); /* Create and Open a new text file object with write access */ if (f_open(&SDFile, f_name, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { - print("SDIO file create success.\r\n"); + printf("SDIO file create success.\r\n"); /* Write data to the text file */ f_write(&SDFile, wtext, sizeof(wtext), (void *)&byteswritten); /* Close the open text file */ f_close(&SDFile); /* Open the text file object with read access */ if (f_open(&SDFile, f_name, FA_READ) == FR_OK) { - print("SDIO file open success.\r\n"); + printf("SDIO file open success.\r\n"); /* Read data from the text file */ f_read(&SDFile, rtext, sizeof(rtext), (UINT*)&bytesread); /* Close the open text file */ @@ -74,7 +74,7 @@ uint8_t test_sdio(void) { } // } // else { - // print("[ERROR Val] %x", ret); + // printf("[ERROR Val] %x", ret); // bsp_error_handler(__FUNCTION__, __LINE__, "SDIO initialize failed."); // return 0; // } From 69ce4534c14223fc83e38dd40869a255a655fb0b Mon Sep 17 00:00:00 2001 From: Temporary User Date: Thu, 21 Nov 2019 15:23:53 -0600 Subject: [PATCH 2/5] third party printf lib added --- BSP/bsp_error_handler.h | 2 +- BSP/bsp_key.c | 2 +- BSP/bsp_oled.h | 1 - BSP/bsp_print.c | 921 +++++++++++++++++++++++++++++- BSP/bsp_print.h | 117 ++++ Libraries/data_process.c | 16 +- Libraries/motor.c | 2 +- Libraries/oled_module.c | 4 +- Tests/rm_test.h | 2 +- Tests/test_bsp_tof.c | 2 +- Tests/test_dbus.c | 2 +- Tests/test_key.c | 2 +- Tests/test_motor.c | 8 +- Tests/test_pid.c | 8 +- Third_Party_Libraries/data_fifo.h | 1 - 15 files changed, 1058 insertions(+), 32 deletions(-) create mode 100644 BSP/bsp_print.h diff --git a/BSP/bsp_error_handler.h b/BSP/bsp_error_handler.h index ec38494..4d7bf03 100644 --- a/BSP/bsp_error_handler.h +++ b/BSP/bsp_error_handler.h @@ -27,7 +27,7 @@ #ifndef _BSP_ERROR_HANDLER_H_ #define _BSP_ERROR_HANDLER_H_ -#include +#include "bsp_print.h" /** * @ingroup bsp diff --git a/BSP/bsp_key.c b/BSP/bsp_key.c index a9c46b4..dbf085b 100644 --- a/BSP/bsp_key.c +++ b/BSP/bsp_key.c @@ -24,7 +24,7 @@ * @log 2018-05-27 nickelliang */ -#include +#include "bsp_print.h" #include "bsp_key.h" diff --git a/BSP/bsp_oled.h b/BSP/bsp_oled.h index 7092e54..d6d1883 100644 --- a/BSP/bsp_oled.h +++ b/BSP/bsp_oled.h @@ -33,7 +33,6 @@ #include "bsp_adc.h" #include "spi.h" #include "math.h" -#include #include #include diff --git a/BSP/bsp_print.c b/BSP/bsp_print.c index 00c3698..24ba3c5 100644 --- a/BSP/bsp_print.c +++ b/BSP/bsp_print.c @@ -17,14 +17,923 @@ *************************************************************************/ #include "bsp_config.h" +#include "bsp_print.h" -#include "cmsis_os.h" #include "stm32f4xx_hal.h" -int __io_putchar(int ch) { - if (HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)&ch, 1, 0xffff) == HAL_OK) - return ch; - else - return 0; +void _putchar(char ch) { + HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)&ch, 1, 0xffff); } +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2019, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on +// embedded systems with a very limited resources. These routines are thread +// safe and reentrant! +// Use this instead of the bloated standard/newlib printf cause these use +// malloc for printf (and may not be thread safe). +// +/////////////////////////////////////////////////////////////////////////////// + +#include +#include + + +// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the +// printf_config.h header file +// default: undefined +#ifdef PRINTF_INCLUDE_CONFIG_H +#include "printf_config.h" +#endif + + +// 'ntoa' conversion buffer size, this must be big enough to hold one converted +// numeric number including padded zeros (dynamically created on stack) +// default: 32 byte +#ifndef PRINTF_NTOA_BUFFER_SIZE +#define PRINTF_NTOA_BUFFER_SIZE 32U +#endif + +// 'ftoa' conversion buffer size, this must be big enough to hold one converted +// float number including padded zeros (dynamically created on stack) +// default: 32 byte +#ifndef PRINTF_FTOA_BUFFER_SIZE +#define PRINTF_FTOA_BUFFER_SIZE 32U +#endif + +// support for the floating point type (%f) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_FLOAT +#define PRINTF_SUPPORT_FLOAT +#endif + +// support for exponential floating point notation (%e/%g) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL +#define PRINTF_SUPPORT_EXPONENTIAL +#endif + +// define the default floating point precision +// default: 6 digits +#ifndef PRINTF_DEFAULT_FLOAT_PRECISION +#define PRINTF_DEFAULT_FLOAT_PRECISION 6U +#endif + +// define the largest float suitable to print with %f +// default: 1e9 +#ifndef PRINTF_MAX_FLOAT +#define PRINTF_MAX_FLOAT 1e9 +#endif + +// support for the long long types (%llu or %p) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG +#define PRINTF_SUPPORT_LONG_LONG +#endif + +// support for the ptrdiff_t type (%t) +// ptrdiff_t is normally defined in as long or long long type +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T +#define PRINTF_SUPPORT_PTRDIFF_T +#endif + +/////////////////////////////////////////////////////////////////////////////// + +// internal flag definitions +#define FLAGS_ZEROPAD (1U << 0U) +#define FLAGS_LEFT (1U << 1U) +#define FLAGS_PLUS (1U << 2U) +#define FLAGS_SPACE (1U << 3U) +#define FLAGS_HASH (1U << 4U) +#define FLAGS_UPPERCASE (1U << 5U) +#define FLAGS_CHAR (1U << 6U) +#define FLAGS_SHORT (1U << 7U) +#define FLAGS_LONG (1U << 8U) +#define FLAGS_LONG_LONG (1U << 9U) +#define FLAGS_PRECISION (1U << 10U) +#define FLAGS_ADAPT_EXP (1U << 11U) + + +// import float.h for DBL_MAX +#if defined(PRINTF_SUPPORT_FLOAT) +#include +#endif + + +// output function type +typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen); + + +// wrapper (used as buffer) for output function type +typedef struct { + void (*fct)(char character, void* arg); + void* arg; +} out_fct_wrap_type; + + +// internal buffer output +static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) +{ + if (idx < maxlen) { + ((char*)buffer)[idx] = character; + } +} + + +// internal null output +static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)character; (void)buffer; (void)idx; (void)maxlen; +} + + +// internal _putchar wrapper +static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)buffer; (void)idx; (void)maxlen; + if (character) { + _putchar(character); + } +} + + +// internal output function wrapper +static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)idx; (void)maxlen; + if (character) { + // buffer is the output fct pointer + ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); + } +} + + +// internal secure strlen +// \return The length of the string (excluding the terminating 0) limited by 'maxsize' +static inline unsigned int _strnlen_s(const char* str, size_t maxsize) +{ + const char* s; + for (s = str; *s && maxsize--; ++s); + return (unsigned int)(s - str); +} + + +// internal test if char is a digit (0-9) +// \return true if char is a digit +static inline bool _is_digit(char ch) +{ + return (ch >= '0') && (ch <= '9'); +} + + +// internal ASCII string to unsigned int conversion +static unsigned int _atoi(const char** str) +{ + unsigned int i = 0U; + while (_is_digit(**str)) { + i = i * 10U + (unsigned int)(*((*str)++) - '0'); + } + return i; +} + + +// output the specified string in reverse, taking care of any zero-padding +static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags) +{ + const size_t start_idx = idx; + + // pad spaces up to given width + if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { + for (size_t i = len; i < width; i++) { + out(' ', buffer, idx++, maxlen); + } + } + + // reverse string + while (len) { + out(buf[--len], buffer, idx++, maxlen); + } + + // append pad spaces up to given width + if (flags & FLAGS_LEFT) { + while (idx - start_idx < width) { + out(' ', buffer, idx++, maxlen); + } + } + + return idx; +} + + +// internal itoa format +static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) +{ + // pad leading zeros + if (!(flags & FLAGS_LEFT)) { + if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + width--; + } + while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + } + + // handle hash + if (flags & FLAGS_HASH) { + if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { + len--; + if (len && (base == 16U)) { + len--; + } + } + if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'x'; + } + else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'X'; + } + else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'b'; + } + if (len < PRINTF_NTOA_BUFFER_SIZE) { + buf[len++] = '0'; + } + } + + if (len < PRINTF_NTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); +} + + +// internal itoa for 'long' type +static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || value) { + do { + const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); + } + + return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); +} + + +// internal itoa for 'long long' type +#if defined(PRINTF_SUPPORT_LONG_LONG) +static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || value) { + do { + const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); + } + + return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); +} +#endif // PRINTF_SUPPORT_LONG_LONG + + +#if defined(PRINTF_SUPPORT_FLOAT) + +#if defined(PRINTF_SUPPORT_EXPONENTIAL) +// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT +static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags); +#endif + + +// internal ftoa for fixed decimal floating point +static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_FTOA_BUFFER_SIZE]; + size_t len = 0U; + double diff = 0.0; + + // powers of 10 + static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + + // test for special values + if (value != value) + return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags); + if (value < -DBL_MAX) + return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags); + if (value > DBL_MAX) + return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags); + + // test for very large values + // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad + if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) { +#if defined(PRINTF_SUPPORT_EXPONENTIAL) + return _etoa(out, buffer, idx, maxlen, value, prec, width, flags); +#else + return 0U; +#endif + } + + // test for negative + bool negative = false; + if (value < 0) { + negative = true; + value = 0 - value; + } + + // set default precision, if not set explicitly + if (!(flags & FLAGS_PRECISION)) { + prec = PRINTF_DEFAULT_FLOAT_PRECISION; + } + // limit precision to 9, cause a prec >= 10 can lead to overflow errors + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { + buf[len++] = '0'; + prec--; + } + + int whole = (int)value; + double tmp = (value - whole) * pow10[prec]; + unsigned long frac = (unsigned long)tmp; + diff = tmp - frac; + + if (diff > 0.5) { + ++frac; + // handle rollover, e.g. case 0.99 with prec 1 is 1.0 + if (frac >= pow10[prec]) { + frac = 0; + ++whole; + } + } + else if (diff < 0.5) { + } + else if ((frac == 0U) || (frac & 1U)) { + // if halfway, round up if odd OR if last digit is 0 + ++frac; + } + + if (prec == 0U) { + diff = value - (double)whole; + if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) { + // exactly 0.5 and ODD, then round up + // 1.5 -> 2, but 2.5 -> 2 + ++whole; + } + } + else { + unsigned int count = prec; + // now do fractional part, as an unsigned number + while (len < PRINTF_FTOA_BUFFER_SIZE) { + --count; + buf[len++] = (char)(48U + (frac % 10U)); + if (!(frac /= 10U)) { + break; + } + } + // add extra 0s + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { + buf[len++] = '0'; + } + if (len < PRINTF_FTOA_BUFFER_SIZE) { + // add decimal + buf[len++] = '.'; + } + } + + // do whole part, number is reversed + while (len < PRINTF_FTOA_BUFFER_SIZE) { + buf[len++] = (char)(48 + (whole % 10)); + if (!(whole /= 10)) { + break; + } + } + + // pad leading zeros + if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { + if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + width--; + } + while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + } + + if (len < PRINTF_FTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); +} + + +#if defined(PRINTF_SUPPORT_EXPONENTIAL) +// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse +static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) +{ + // check for NaN and special values + if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) { + return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags); + } + + // determine the sign + const bool negative = value < 0; + if (negative) { + value = -value; + } + + // default precision + if (!(flags & FLAGS_PRECISION)) { + prec = PRINTF_DEFAULT_FLOAT_PRECISION; + } + + // determine the decimal exponent + // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) + union { + uint64_t U; + double F; + } conv; + + conv.F = value; + int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2 + conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2) + // now approximate log10 from the log2 integer part and an expansion of ln around 1.5 + int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168); + // now we want to compute 10^expval but we want to be sure it won't overflow + exp2 = (int)(expval * 3.321928094887362 + 0.5); + const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453; + const double z2 = z * z; + conv.U = (uint64_t)(exp2 + 1023) << 52U; + // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex + conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); + // correct for rounding errors + if (value < conv.F) { + expval--; + conv.F /= 10; + } + + // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters + unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U; + + // in "%g" mode, "prec" is the number of *significant figures* not decimals + if (flags & FLAGS_ADAPT_EXP) { + // do we want to fall-back to "%f" mode? + if ((value >= 1e-4) && (value < 1e6)) { + if ((int)prec > expval) { + prec = (unsigned)((int)prec - expval - 1); + } + else { + prec = 0; + } + flags |= FLAGS_PRECISION; // make sure _ftoa respects precision + // no characters in exponent + minwidth = 0U; + expval = 0; + } + else { + // we use one sigfig for the whole part + if ((prec > 0) && (flags & FLAGS_PRECISION)) { + --prec; + } + } + } + + // will everything fit? + unsigned int fwidth = width; + if (width > minwidth) { + // we didn't fall-back so subtract the characters required for the exponent + fwidth -= minwidth; + } else { + // not enough characters, so go back to default sizing + fwidth = 0U; + } + if ((flags & FLAGS_LEFT) && minwidth) { + // if we're padding on the right, DON'T pad the floating part + fwidth = 0U; + } + + // rescale the float value + if (expval) { + value /= conv.F; + } + + // output the floating part + const size_t start_idx = idx; + idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); + + // output the exponent part + if (minwidth) { + // output the exponential symbol + out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); + // output the exponent value + idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); + // might need to right-pad spaces + if (flags & FLAGS_LEFT) { + while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); + } + } + return idx; +} +#endif // PRINTF_SUPPORT_EXPONENTIAL +#endif // PRINTF_SUPPORT_FLOAT + + +// internal vsnprintf +static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va) +{ + unsigned int flags, width, precision, n; + size_t idx = 0U; + + if (!buffer) { + // use null output function + out = _out_null; + } + + while (*format) + { + // format specifier? %[flags][width][.precision][length] + if (*format != '%') { + // no + out(*format, buffer, idx++, maxlen); + format++; + continue; + } + else { + // yes, evaluate it + format++; + } + + // evaluate flags + flags = 0U; + do { + switch (*format) { + case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; + case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; + case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; + case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; + case '#': flags |= FLAGS_HASH; format++; n = 1U; break; + default : n = 0U; break; + } + } while (n); + + // evaluate width field + width = 0U; + if (_is_digit(*format)) { + width = _atoi(&format); + } + else if (*format == '*') { + const int w = va_arg(va, int); + if (w < 0) { + flags |= FLAGS_LEFT; // reverse padding + width = (unsigned int)-w; + } + else { + width = (unsigned int)w; + } + format++; + } + + // evaluate precision field + precision = 0U; + if (*format == '.') { + flags |= FLAGS_PRECISION; + format++; + if (_is_digit(*format)) { + precision = _atoi(&format); + } + else if (*format == '*') { + const int prec = (int)va_arg(va, int); + precision = prec > 0 ? (unsigned int)prec : 0U; + format++; + } + } + + // evaluate length field + switch (*format) { + case 'l' : + flags |= FLAGS_LONG; + format++; + if (*format == 'l') { + flags |= FLAGS_LONG_LONG; + format++; + } + break; + case 'h' : + flags |= FLAGS_SHORT; + format++; + if (*format == 'h') { + flags |= FLAGS_CHAR; + format++; + } + break; +#if defined(PRINTF_SUPPORT_PTRDIFF_T) + case 't' : + flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; +#endif + case 'j' : + flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; + case 'z' : + flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; + default : + break; + } + + // evaluate specifier + switch (*format) { + case 'd' : + case 'i' : + case 'u' : + case 'x' : + case 'X' : + case 'o' : + case 'b' : { + // set the base + unsigned int base; + if (*format == 'x' || *format == 'X') { + base = 16U; + } + else if (*format == 'o') { + base = 8U; + } + else if (*format == 'b') { + base = 2U; + } + else { + base = 10U; + flags &= ~FLAGS_HASH; // no hash for dec format + } + // uppercase + if (*format == 'X') { + flags |= FLAGS_UPPERCASE; + } + + // no plus or space flag for u, x, X, o, b + if ((*format != 'i') && (*format != 'd')) { + flags &= ~(FLAGS_PLUS | FLAGS_SPACE); + } + + // ignore '0' flag when precision is given + if (flags & FLAGS_PRECISION) { + flags &= ~FLAGS_ZEROPAD; + } + + // convert the integer + if ((*format == 'i') || (*format == 'd')) { + // signed + if (flags & FLAGS_LONG_LONG) { +#if defined(PRINTF_SUPPORT_LONG_LONG) + const long long value = va_arg(va, long long); + idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); +#endif + } + else if (flags & FLAGS_LONG) { + const long value = va_arg(va, long); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); + } + else { + const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); + } + } + else { + // unsigned + if (flags & FLAGS_LONG_LONG) { +#if defined(PRINTF_SUPPORT_LONG_LONG) + idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags); +#endif + } + else if (flags & FLAGS_LONG) { + idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags); + } + else { + const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int); + idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); + } + } + format++; + break; + } +#if defined(PRINTF_SUPPORT_FLOAT) + case 'f' : + case 'F' : + if (*format == 'F') flags |= FLAGS_UPPERCASE; + idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); + format++; + break; +#if defined(PRINTF_SUPPORT_EXPONENTIAL) + case 'e': + case 'E': + case 'g': + case 'G': + if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP; + if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE; + idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); + format++; + break; +#endif // PRINTF_SUPPORT_EXPONENTIAL +#endif // PRINTF_SUPPORT_FLOAT + case 'c' : { + unsigned int l = 1U; + // pre padding + if (!(flags & FLAGS_LEFT)) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + // char output + out((char)va_arg(va, int), buffer, idx++, maxlen); + // post padding + if (flags & FLAGS_LEFT) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + format++; + break; + } + + case 's' : { + const char* p = va_arg(va, char*); + unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1); + // pre padding + if (flags & FLAGS_PRECISION) { + l = (l < precision ? l : precision); + } + if (!(flags & FLAGS_LEFT)) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + // string output + while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { + out(*(p++), buffer, idx++, maxlen); + } + // post padding + if (flags & FLAGS_LEFT) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + format++; + break; + } + + case 'p' : { + width = sizeof(void*) * 2U; + flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; +#if defined(PRINTF_SUPPORT_LONG_LONG) + const bool is_ll = sizeof(uintptr_t) == sizeof(long long); + if (is_ll) { + idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags); + } + else { +#endif + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags); +#if defined(PRINTF_SUPPORT_LONG_LONG) + } +#endif + format++; + break; + } + + case '%' : + out('%', buffer, idx++, maxlen); + format++; + break; + + default : + out(*format, buffer, idx++, maxlen); + format++; + break; + } + } + + // termination + out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen); + + // return written chars without terminating \0 + return (int)idx; +} + + +/////////////////////////////////////////////////////////////////////////////// + +int printf_(const char* format, ...) +{ + va_list va; + va_start(va, format); + char buffer[1]; + const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); + va_end(va); + return ret; +} + + +int sprintf_(char* buffer, const char* format, ...) +{ + va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va); + va_end(va); + return ret; +} + + +int snprintf_(char* buffer, size_t count, const char* format, ...) +{ + va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, count, format, va); + va_end(va); + return ret; +} + + +int vprintf_(const char* format, va_list va) +{ + char buffer[1]; + return _vsnprintf(_out_char, buffer, (size_t)-1, format, va); +} + + +int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) +{ + return _vsnprintf(_out_buffer, buffer, count, format, va); +} + + +int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) +{ + va_list va; + va_start(va, format); + const out_fct_wrap_type out_fct_wrap = { out, arg }; + const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); + va_end(va); + return ret; +} diff --git a/BSP/bsp_print.h b/BSP/bsp_print.h new file mode 100644 index 0000000..8f7a4c8 --- /dev/null +++ b/BSP/bsp_print.h @@ -0,0 +1,117 @@ +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2019, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on +// embedded systems with a very limited resources. +// Use this instead of bloated standard/newlib printf. +// These routines are thread safe and reentrant. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _PRINTF_H_ +#define _PRINTF_H_ + +#include +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * Output a character to a custom device like UART, used by the printf() function + * This function is declared here only. You have to write your custom implementation somewhere + * \param character Character to output + */ +void _putchar(char character); + + +/** + * Tiny printf implementation + * You have to implement _putchar if you use printf() + * To avoid conflicts with the regular printf() API it is overridden by macro defines + * and internal underscore-appended functions like printf_() are used + * \param format A string that specifies the format of the output + * \return The number of characters that are written into the array, not counting the terminating null character + */ +#define printf printf_ +int printf_(const char* format, ...); + + +/** + * Tiny sprintf implementation + * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD! + * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output! + * \param format A string that specifies the format of the output + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character + */ +#define sprintf sprintf_ +int sprintf_(char* buffer, const char* format, ...); + + +/** + * Tiny snprintf/vsnprintf implementation + * \param buffer A pointer to the buffer where to store the formatted string + * \param count The maximum number of characters to store in the buffer, including a terminating null character + * \param format A string that specifies the format of the output + * \param va A value identifying a variable arguments list + * \return The number of characters that COULD have been written into the buffer, not counting the terminating + * null character. A value equal or larger than count indicates truncation. Only when the returned value + * is non-negative and less than count, the string has been completely written. + */ +#define snprintf snprintf_ +#define vsnprintf vsnprintf_ +int snprintf_(char* buffer, size_t count, const char* format, ...); +int vsnprintf_(char* buffer, size_t count, const char* format, va_list va); + + +/** + * Tiny vprintf implementation + * \param format A string that specifies the format of the output + * \param va A value identifying a variable arguments list + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character + */ +#define vprintf vprintf_ +int vprintf_(const char* format, va_list va); + + +/** + * printf with output function + * You may use this as dynamic alternative to printf() with its fixed _putchar() output + * \param out An output function which takes one character and an argument pointer + * \param arg An argument pointer for user data passed to output function + * \param format A string that specifies the format of the output + * \return The number of characters that are sent to the output function, not counting the terminating null character + */ +int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...); + + +#ifdef __cplusplus +} +#endif + + +#endif // _PRINTF_H_ diff --git a/Libraries/data_process.c b/Libraries/data_process.c index d8284d2..d223a16 100644 --- a/Libraries/data_process.c +++ b/Libraries/data_process.c @@ -24,9 +24,9 @@ * @log 2018-05-26 nickelliang */ -#include #include +#include "bsp_print.h" #include "data_process.h" data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, uint32_t fifo_size, uint16_t buffer_size, uint8_t sof, dispatcher_func_t dispatcher_func, void *source_struct, osMutexId tx_mutex, packer_func_t packer_func) { @@ -53,7 +53,7 @@ data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, ui source->data_fifo = fifo_s_create(fifo_size, mutex); if (source->data_fifo == NULL) { bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate FIFO for data process object."); - free(source); + vPortFree(source); return NULL; } @@ -61,7 +61,7 @@ data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, ui if (source->buff[0] == NULL) { bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate DMA buffer for data process object."); fifo_s_destory(source->data_fifo); - free(source); + vPortFree(source); return NULL; } source->buff[1] = source->buff[0] + source->buff_size; @@ -74,19 +74,19 @@ data_process_t* data_process_init(UART_HandleTypeDef *huart, osMutexId mutex, ui source->frame_packet = (uint8_t*)pvPortMalloc(fifo_size); if (source->frame_packet == NULL) { bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate frame packet buffer for data process object."); - free(source->buff[0]); + vPortFree(source->buff[0]); fifo_s_destory(source->data_fifo); - free(source); + vPortFree(source); return NULL; } source->transmit_fifo = fifo_s_create(fifo_size, tx_mutex); if (source->transmit_fifo == NULL) { bsp_error_handler(__FUNCTION__, __LINE__, "Unable to allocate transmit FIFO for data process object."); - free(source->frame_packet); - free(source->buff[0]); + vPortFree(source->frame_packet); + vPortFree(source->buff[0]); fifo_s_destory(source->data_fifo); - free(source); + vPortFree(source); return NULL; } diff --git a/Libraries/motor.c b/Libraries/motor.c index 98f80b1..9281dd0 100644 --- a/Libraries/motor.c +++ b/Libraries/motor.c @@ -16,10 +16,10 @@ * along with this program. If not, see . *************************************************************************/ +#include "bsp_print.h" #include "motor.h" #include "utils.h" #include -#include /* private function starts from here */ diff --git a/Libraries/oled_module.c b/Libraries/oled_module.c index 3bc7fd3..1124d12 100644 --- a/Libraries/oled_module.c +++ b/Libraries/oled_module.c @@ -24,6 +24,8 @@ * @log 2018-05-30 nickelliang */ +#include "bsp_print.h" + #include "oled_module.h" #include "oled_module_resource.h" @@ -244,7 +246,7 @@ void oled_printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); - vsprintf((char*)OLED_PRINT_BUF, fmt, ap); + vsnprintf((char*)OLED_PRINT_BUF, CHAR_MAX_PRINT, fmt, ap); va_end(ap); oled_puts(&cursor_row, &cursor_col, OLED_PRINT_BUF, OLED_ON); diff --git a/Tests/rm_test.h b/Tests/rm_test.h index cd86ef0..dcd709c 100644 --- a/Tests/rm_test.h +++ b/Tests/rm_test.h @@ -62,7 +62,7 @@ #define TEST_SERVO OFF #define TEST_BSP_PWM OFF #define TEST_BSP_IMU OFF -#define TEST_IMU OFF +#define TEST_IMU ON #define TEST_MOUSE OFF #define TEST_CHASSIS OFF #define TEST_TX2 OFF diff --git a/Tests/test_bsp_tof.c b/Tests/test_bsp_tof.c index cd2eb52..0dbe6c4 100644 --- a/Tests/test_bsp_tof.c +++ b/Tests/test_bsp_tof.c @@ -1,4 +1,4 @@ -#include +#include "bsp_print.h" #include "test_bsp_tof.h" #include "cmsis_os.h" diff --git a/Tests/test_dbus.c b/Tests/test_dbus.c index d7d9d38..ceb4be7 100644 --- a/Tests/test_dbus.c +++ b/Tests/test_dbus.c @@ -24,7 +24,7 @@ * @log 2018-04-17 nickelliang */ -#include +#include "bsp_print.h" #include "test_dbus.h" diff --git a/Tests/test_key.c b/Tests/test_key.c index 8b5d35f..fa5fd3d 100644 --- a/Tests/test_key.c +++ b/Tests/test_key.c @@ -24,7 +24,7 @@ * @log 2018-05-27 nickelliang */ -#include +#include "bsp_print.h" #include "test_key.h" diff --git a/Tests/test_motor.c b/Tests/test_motor.c index c6e3d02..dca10df 100644 --- a/Tests/test_motor.c +++ b/Tests/test_motor.c @@ -20,8 +20,8 @@ void test_motor() { // motor_feedback(); - // test_motor_3508(0); - test_motor_6623(0); + test_motor_3508(1); + // test_motor_6623(0); // test_motor_2006(0); // test_motor_3510(0); // test_motor_2305(); @@ -42,12 +42,12 @@ void test_motor_3508(uint8_t rotate) { motor_t motor; size_t i; - can_motor_init(&motor, 0x201, CAN1_ID, M3508); + can_motor_init(&motor, 0x202, CAN1_ID, M3508); motor.out = 200; if (rotate) for (i = 0; i < 1000; i++) - set_can_motor_output(&motor, NULL, NULL, NULL); + set_can_motor_output(NULL, &motor, NULL, NULL); for (i = 0; i < 1000; i++) { get_motor_data(&motor); diff --git a/Tests/test_pid.c b/Tests/test_pid.c index 451d2cf..fbec9d6 100644 --- a/Tests/test_pid.c +++ b/Tests/test_pid.c @@ -28,9 +28,9 @@ void test_pid() { // test_poke(); // test_shoot(); // test_pitch(); - test_yaw(); + // test_yaw(); // test_pid_2006(); - // test_pid_3508(); + test_pid_3508(); } void test_power_speed(void) { @@ -216,7 +216,7 @@ void test_pid_3508() { motor_t mt_3508; pid_ctl_t pid_3508; - can_motor_init(&mt_3508, 0x201, CAN1_ID, M3508); + can_motor_init(&mt_3508, 0x202, CAN1_ID, M3508); pid_init(&pid_3508, CHASSIS_ROTATE, &mt_3508, -2000, 2000, 0, 0, 0, 5, 0, 0, 5000, 0); int32_t target_speed = 800; @@ -224,7 +224,7 @@ void test_pid_3508() { while (1) { mt_3508.out = pid_calc(&pid_3508, target_speed); - set_can_motor_output(&mt_3508, NULL, NULL, NULL); + set_can_motor_output(NULL, &mt_3508, NULL, NULL); osDelayUntil(&mt_3508_wake_time, 20); } } diff --git a/Third_Party_Libraries/data_fifo.h b/Third_Party_Libraries/data_fifo.h index 8259633..9f154e2 100644 --- a/Third_Party_Libraries/data_fifo.h +++ b/Third_Party_Libraries/data_fifo.h @@ -29,7 +29,6 @@ #include "stm32f4xx_hal.h" #include "cmsis_os.h" #include -#include #define ASSERT(x) do { \ while(!(x)); \ From 0c7de2958e8e73365eb0100d8994d69e789254c2 Mon Sep 17 00:00:00 2001 From: Temporary User Date: Fri, 22 Nov 2019 11:59:20 -0600 Subject: [PATCH 3/5] turn off all tests --- Tests/rm_test.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/rm_test.h b/Tests/rm_test.h index dcd709c..cd86ef0 100644 --- a/Tests/rm_test.h +++ b/Tests/rm_test.h @@ -62,7 +62,7 @@ #define TEST_SERVO OFF #define TEST_BSP_PWM OFF #define TEST_BSP_IMU OFF -#define TEST_IMU ON +#define TEST_IMU OFF #define TEST_MOUSE OFF #define TEST_CHASSIS OFF #define TEST_TX2 OFF From dc8a0eae580133bf407706e3c5cb5cb7c8a31c3e Mon Sep 17 00:00:00 2001 From: Temporary User Date: Fri, 22 Nov 2019 13:15:53 -0600 Subject: [PATCH 4/5] move third party prints away --- BSP/bsp_print.c | 913 +------------------- BSP/bsp_print.h | 137 +-- Third_Party_Libraries/tiny-printf/printf.c | 914 +++++++++++++++++++++ Third_Party_Libraries/tiny-printf/printf.h | 117 +++ 4 files changed, 1059 insertions(+), 1022 deletions(-) create mode 100644 Third_Party_Libraries/tiny-printf/printf.c create mode 100644 Third_Party_Libraries/tiny-printf/printf.h diff --git a/BSP/bsp_print.c b/BSP/bsp_print.c index 24ba3c5..3109e4b 100644 --- a/BSP/bsp_print.c +++ b/BSP/bsp_print.c @@ -21,919 +21,8 @@ #include "stm32f4xx_hal.h" +// implement low level io for third party printf library void _putchar(char ch) { HAL_UART_Transmit(&BSP_PRINT_PORT, (uint8_t*)&ch, 1, 0xffff); } -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2019, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. These routines are thread -// safe and reentrant! -// Use this instead of the bloated standard/newlib printf cause these use -// malloc for printf (and may not be thread safe). -// -/////////////////////////////////////////////////////////////////////////////// - -#include -#include - - -// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the -// printf_config.h header file -// default: undefined -#ifdef PRINTF_INCLUDE_CONFIG_H -#include "printf_config.h" -#endif - - -// 'ntoa' conversion buffer size, this must be big enough to hold one converted -// numeric number including padded zeros (dynamically created on stack) -// default: 32 byte -#ifndef PRINTF_NTOA_BUFFER_SIZE -#define PRINTF_NTOA_BUFFER_SIZE 32U -#endif - -// 'ftoa' conversion buffer size, this must be big enough to hold one converted -// float number including padded zeros (dynamically created on stack) -// default: 32 byte -#ifndef PRINTF_FTOA_BUFFER_SIZE -#define PRINTF_FTOA_BUFFER_SIZE 32U -#endif - -// support for the floating point type (%f) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_FLOAT -#define PRINTF_SUPPORT_FLOAT -#endif - -// support for exponential floating point notation (%e/%g) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL -#define PRINTF_SUPPORT_EXPONENTIAL -#endif - -// define the default floating point precision -// default: 6 digits -#ifndef PRINTF_DEFAULT_FLOAT_PRECISION -#define PRINTF_DEFAULT_FLOAT_PRECISION 6U -#endif - -// define the largest float suitable to print with %f -// default: 1e9 -#ifndef PRINTF_MAX_FLOAT -#define PRINTF_MAX_FLOAT 1e9 -#endif - -// support for the long long types (%llu or %p) -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG -#define PRINTF_SUPPORT_LONG_LONG -#endif - -// support for the ptrdiff_t type (%t) -// ptrdiff_t is normally defined in as long or long long type -// default: activated -#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T -#define PRINTF_SUPPORT_PTRDIFF_T -#endif - -/////////////////////////////////////////////////////////////////////////////// - -// internal flag definitions -#define FLAGS_ZEROPAD (1U << 0U) -#define FLAGS_LEFT (1U << 1U) -#define FLAGS_PLUS (1U << 2U) -#define FLAGS_SPACE (1U << 3U) -#define FLAGS_HASH (1U << 4U) -#define FLAGS_UPPERCASE (1U << 5U) -#define FLAGS_CHAR (1U << 6U) -#define FLAGS_SHORT (1U << 7U) -#define FLAGS_LONG (1U << 8U) -#define FLAGS_LONG_LONG (1U << 9U) -#define FLAGS_PRECISION (1U << 10U) -#define FLAGS_ADAPT_EXP (1U << 11U) - - -// import float.h for DBL_MAX -#if defined(PRINTF_SUPPORT_FLOAT) -#include -#endif - - -// output function type -typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen); - - -// wrapper (used as buffer) for output function type -typedef struct { - void (*fct)(char character, void* arg); - void* arg; -} out_fct_wrap_type; - - -// internal buffer output -static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) -{ - if (idx < maxlen) { - ((char*)buffer)[idx] = character; - } -} - - -// internal null output -static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)character; (void)buffer; (void)idx; (void)maxlen; -} - - -// internal _putchar wrapper -static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)buffer; (void)idx; (void)maxlen; - if (character) { - _putchar(character); - } -} - - -// internal output function wrapper -static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)idx; (void)maxlen; - if (character) { - // buffer is the output fct pointer - ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); - } -} - - -// internal secure strlen -// \return The length of the string (excluding the terminating 0) limited by 'maxsize' -static inline unsigned int _strnlen_s(const char* str, size_t maxsize) -{ - const char* s; - for (s = str; *s && maxsize--; ++s); - return (unsigned int)(s - str); -} - - -// internal test if char is a digit (0-9) -// \return true if char is a digit -static inline bool _is_digit(char ch) -{ - return (ch >= '0') && (ch <= '9'); -} - - -// internal ASCII string to unsigned int conversion -static unsigned int _atoi(const char** str) -{ - unsigned int i = 0U; - while (_is_digit(**str)) { - i = i * 10U + (unsigned int)(*((*str)++) - '0'); - } - return i; -} - - -// output the specified string in reverse, taking care of any zero-padding -static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags) -{ - const size_t start_idx = idx; - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - for (size_t i = len; i < width; i++) { - out(' ', buffer, idx++, maxlen); - } - } - - // reverse string - while (len) { - out(buf[--len], buffer, idx++, maxlen); - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) { - out(' ', buffer, idx++, maxlen); - } - } - - return idx; -} - - -// internal itoa format -static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) -{ - // pad leading zeros - if (!(flags & FLAGS_LEFT)) { - if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - // handle hash - if (flags & FLAGS_HASH) { - if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { - len--; - if (len && (base == 16U)) { - len--; - } - } - if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'x'; - } - else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'X'; - } - else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) { - buf[len++] = 'b'; - } - if (len < PRINTF_NTOA_BUFFER_SIZE) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_NTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); -} - - -// internal itoa for 'long' type -static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_NTOA_BUFFER_SIZE]; - size_t len = 0U; - - // no hash for 0 values - if (!value) { - flags &= ~FLAGS_HASH; - } - - // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { - do { - const char digit = (char)(value % base); - buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; - value /= base; - } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); - } - - return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); -} - - -// internal itoa for 'long long' type -#if defined(PRINTF_SUPPORT_LONG_LONG) -static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_NTOA_BUFFER_SIZE]; - size_t len = 0U; - - // no hash for 0 values - if (!value) { - flags &= ~FLAGS_HASH; - } - - // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { - do { - const char digit = (char)(value % base); - buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; - value /= base; - } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); - } - - return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); -} -#endif // PRINTF_SUPPORT_LONG_LONG - - -#if defined(PRINTF_SUPPORT_FLOAT) - -#if defined(PRINTF_SUPPORT_EXPONENTIAL) -// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT -static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags); -#endif - - -// internal ftoa for fixed decimal floating point -static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) -{ - char buf[PRINTF_FTOA_BUFFER_SIZE]; - size_t len = 0U; - double diff = 0.0; - - // powers of 10 - static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; - - // test for special values - if (value != value) - return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags); - if (value < -DBL_MAX) - return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags); - if (value > DBL_MAX) - return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags); - - // test for very large values - // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad - if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) { -#if defined(PRINTF_SUPPORT_EXPONENTIAL) - return _etoa(out, buffer, idx, maxlen, value, prec, width, flags); -#else - return 0U; -#endif - } - - // test for negative - bool negative = false; - if (value < 0) { - negative = true; - value = 0 - value; - } - - // set default precision, if not set explicitly - if (!(flags & FLAGS_PRECISION)) { - prec = PRINTF_DEFAULT_FLOAT_PRECISION; - } - // limit precision to 9, cause a prec >= 10 can lead to overflow errors - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { - buf[len++] = '0'; - prec--; - } - - int whole = (int)value; - double tmp = (value - whole) * pow10[prec]; - unsigned long frac = (unsigned long)tmp; - diff = tmp - frac; - - if (diff > 0.5) { - ++frac; - // handle rollover, e.g. case 0.99 with prec 1 is 1.0 - if (frac >= pow10[prec]) { - frac = 0; - ++whole; - } - } - else if (diff < 0.5) { - } - else if ((frac == 0U) || (frac & 1U)) { - // if halfway, round up if odd OR if last digit is 0 - ++frac; - } - - if (prec == 0U) { - diff = value - (double)whole; - if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) { - // exactly 0.5 and ODD, then round up - // 1.5 -> 2, but 2.5 -> 2 - ++whole; - } - } - else { - unsigned int count = prec; - // now do fractional part, as an unsigned number - while (len < PRINTF_FTOA_BUFFER_SIZE) { - --count; - buf[len++] = (char)(48U + (frac % 10U)); - if (!(frac /= 10U)) { - break; - } - } - // add extra 0s - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { - buf[len++] = '0'; - } - if (len < PRINTF_FTOA_BUFFER_SIZE) { - // add decimal - buf[len++] = '.'; - } - } - - // do whole part, number is reversed - while (len < PRINTF_FTOA_BUFFER_SIZE) { - buf[len++] = (char)(48 + (whole % 10)); - if (!(whole /= 10)) { - break; - } - } - - // pad leading zeros - if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { - if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_FTOA_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } - else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } - else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); -} - - -#if defined(PRINTF_SUPPORT_EXPONENTIAL) -// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse -static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) -{ - // check for NaN and special values - if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) { - return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags); - } - - // determine the sign - const bool negative = value < 0; - if (negative) { - value = -value; - } - - // default precision - if (!(flags & FLAGS_PRECISION)) { - prec = PRINTF_DEFAULT_FLOAT_PRECISION; - } - - // determine the decimal exponent - // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) - union { - uint64_t U; - double F; - } conv; - - conv.F = value; - int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2 - conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2) - // now approximate log10 from the log2 integer part and an expansion of ln around 1.5 - int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168); - // now we want to compute 10^expval but we want to be sure it won't overflow - exp2 = (int)(expval * 3.321928094887362 + 0.5); - const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453; - const double z2 = z * z; - conv.U = (uint64_t)(exp2 + 1023) << 52U; - // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex - conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); - // correct for rounding errors - if (value < conv.F) { - expval--; - conv.F /= 10; - } - - // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters - unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U; - - // in "%g" mode, "prec" is the number of *significant figures* not decimals - if (flags & FLAGS_ADAPT_EXP) { - // do we want to fall-back to "%f" mode? - if ((value >= 1e-4) && (value < 1e6)) { - if ((int)prec > expval) { - prec = (unsigned)((int)prec - expval - 1); - } - else { - prec = 0; - } - flags |= FLAGS_PRECISION; // make sure _ftoa respects precision - // no characters in exponent - minwidth = 0U; - expval = 0; - } - else { - // we use one sigfig for the whole part - if ((prec > 0) && (flags & FLAGS_PRECISION)) { - --prec; - } - } - } - - // will everything fit? - unsigned int fwidth = width; - if (width > minwidth) { - // we didn't fall-back so subtract the characters required for the exponent - fwidth -= minwidth; - } else { - // not enough characters, so go back to default sizing - fwidth = 0U; - } - if ((flags & FLAGS_LEFT) && minwidth) { - // if we're padding on the right, DON'T pad the floating part - fwidth = 0U; - } - - // rescale the float value - if (expval) { - value /= conv.F; - } - - // output the floating part - const size_t start_idx = idx; - idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); - - // output the exponent part - if (minwidth) { - // output the exponential symbol - out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); - // output the exponent value - idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); - // might need to right-pad spaces - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); - } - } - return idx; -} -#endif // PRINTF_SUPPORT_EXPONENTIAL -#endif // PRINTF_SUPPORT_FLOAT - - -// internal vsnprintf -static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va) -{ - unsigned int flags, width, precision, n; - size_t idx = 0U; - - if (!buffer) { - // use null output function - out = _out_null; - } - - while (*format) - { - // format specifier? %[flags][width][.precision][length] - if (*format != '%') { - // no - out(*format, buffer, idx++, maxlen); - format++; - continue; - } - else { - // yes, evaluate it - format++; - } - - // evaluate flags - flags = 0U; - do { - switch (*format) { - case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; - case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; - case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; - case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; - case '#': flags |= FLAGS_HASH; format++; n = 1U; break; - default : n = 0U; break; - } - } while (n); - - // evaluate width field - width = 0U; - if (_is_digit(*format)) { - width = _atoi(&format); - } - else if (*format == '*') { - const int w = va_arg(va, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding - width = (unsigned int)-w; - } - else { - width = (unsigned int)w; - } - format++; - } - - // evaluate precision field - precision = 0U; - if (*format == '.') { - flags |= FLAGS_PRECISION; - format++; - if (_is_digit(*format)) { - precision = _atoi(&format); - } - else if (*format == '*') { - const int prec = (int)va_arg(va, int); - precision = prec > 0 ? (unsigned int)prec : 0U; - format++; - } - } - - // evaluate length field - switch (*format) { - case 'l' : - flags |= FLAGS_LONG; - format++; - if (*format == 'l') { - flags |= FLAGS_LONG_LONG; - format++; - } - break; - case 'h' : - flags |= FLAGS_SHORT; - format++; - if (*format == 'h') { - flags |= FLAGS_CHAR; - format++; - } - break; -#if defined(PRINTF_SUPPORT_PTRDIFF_T) - case 't' : - flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; -#endif - case 'j' : - flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; - case 'z' : - flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - format++; - break; - default : - break; - } - - // evaluate specifier - switch (*format) { - case 'd' : - case 'i' : - case 'u' : - case 'x' : - case 'X' : - case 'o' : - case 'b' : { - // set the base - unsigned int base; - if (*format == 'x' || *format == 'X') { - base = 16U; - } - else if (*format == 'o') { - base = 8U; - } - else if (*format == 'b') { - base = 2U; - } - else { - base = 10U; - flags &= ~FLAGS_HASH; // no hash for dec format - } - // uppercase - if (*format == 'X') { - flags |= FLAGS_UPPERCASE; - } - - // no plus or space flag for u, x, X, o, b - if ((*format != 'i') && (*format != 'd')) { - flags &= ~(FLAGS_PLUS | FLAGS_SPACE); - } - - // ignore '0' flag when precision is given - if (flags & FLAGS_PRECISION) { - flags &= ~FLAGS_ZEROPAD; - } - - // convert the integer - if ((*format == 'i') || (*format == 'd')) { - // signed - if (flags & FLAGS_LONG_LONG) { -#if defined(PRINTF_SUPPORT_LONG_LONG) - const long long value = va_arg(va, long long); - idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); -#endif - } - else if (flags & FLAGS_LONG) { - const long value = va_arg(va, long); - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); - } - else { - const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int); - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); - } - } - else { - // unsigned - if (flags & FLAGS_LONG_LONG) { -#if defined(PRINTF_SUPPORT_LONG_LONG) - idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags); -#endif - } - else if (flags & FLAGS_LONG) { - idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags); - } - else { - const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int); - idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); - } - } - format++; - break; - } -#if defined(PRINTF_SUPPORT_FLOAT) - case 'f' : - case 'F' : - if (*format == 'F') flags |= FLAGS_UPPERCASE; - idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); - format++; - break; -#if defined(PRINTF_SUPPORT_EXPONENTIAL) - case 'e': - case 'E': - case 'g': - case 'G': - if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP; - if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE; - idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); - format++; - break; -#endif // PRINTF_SUPPORT_EXPONENTIAL -#endif // PRINTF_SUPPORT_FLOAT - case 'c' : { - unsigned int l = 1U; - // pre padding - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - // char output - out((char)va_arg(va, int), buffer, idx++, maxlen); - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - format++; - break; - } - - case 's' : { - const char* p = va_arg(va, char*); - unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1); - // pre padding - if (flags & FLAGS_PRECISION) { - l = (l < precision ? l : precision); - } - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - // string output - while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { - out(*(p++), buffer, idx++, maxlen); - } - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - out(' ', buffer, idx++, maxlen); - } - } - format++; - break; - } - - case 'p' : { - width = sizeof(void*) * 2U; - flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; -#if defined(PRINTF_SUPPORT_LONG_LONG) - const bool is_ll = sizeof(uintptr_t) == sizeof(long long); - if (is_ll) { - idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags); - } - else { -#endif - idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags); -#if defined(PRINTF_SUPPORT_LONG_LONG) - } -#endif - format++; - break; - } - - case '%' : - out('%', buffer, idx++, maxlen); - format++; - break; - - default : - out(*format, buffer, idx++, maxlen); - format++; - break; - } - } - - // termination - out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen); - - // return written chars without terminating \0 - return (int)idx; -} - - -/////////////////////////////////////////////////////////////////////////////// - -int printf_(const char* format, ...) -{ - va_list va; - va_start(va, format); - char buffer[1]; - const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); - va_end(va); - return ret; -} - - -int sprintf_(char* buffer, const char* format, ...) -{ - va_list va; - va_start(va, format); - const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va); - va_end(va); - return ret; -} - - -int snprintf_(char* buffer, size_t count, const char* format, ...) -{ - va_list va; - va_start(va, format); - const int ret = _vsnprintf(_out_buffer, buffer, count, format, va); - va_end(va); - return ret; -} - - -int vprintf_(const char* format, va_list va) -{ - char buffer[1]; - return _vsnprintf(_out_char, buffer, (size_t)-1, format, va); -} - - -int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) -{ - return _vsnprintf(_out_buffer, buffer, count, format, va); -} - - -int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) -{ - va_list va; - va_start(va, format); - const out_fct_wrap_type out_fct_wrap = { out, arg }; - const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); - va_end(va); - return ret; -} diff --git a/BSP/bsp_print.h b/BSP/bsp_print.h index 8f7a4c8..3134606 100644 --- a/BSP/bsp_print.h +++ b/BSP/bsp_print.h @@ -1,117 +1,34 @@ -/////////////////////////////////////////////////////////////////////////////// -// \author (c) Marco Paland (info@paland.com) -// 2014-2019, PALANDesign Hannover, Germany -// -// \license The MIT License (MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on -// embedded systems with a very limited resources. -// Use this instead of bloated standard/newlib printf. -// These routines are thread safe and reentrant. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef _PRINTF_H_ -#define _PRINTF_H_ - -#include -#include - - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * Output a character to a custom device like UART, used by the printf() function - * This function is declared here only. You have to write your custom implementation somewhere - * \param character Character to output - */ -void _putchar(char character); - - -/** - * Tiny printf implementation - * You have to implement _putchar if you use printf() - * To avoid conflicts with the regular printf() API it is overridden by macro defines - * and internal underscore-appended functions like printf_() are used - * \param format A string that specifies the format of the output - * \return The number of characters that are written into the array, not counting the terminating null character - */ -#define printf printf_ -int printf_(const char* format, ...); - - -/** - * Tiny sprintf implementation - * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD! - * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output! - * \param format A string that specifies the format of the output - * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character - */ -#define sprintf sprintf_ -int sprintf_(char* buffer, const char* format, ...); - - -/** - * Tiny snprintf/vsnprintf implementation - * \param buffer A pointer to the buffer where to store the formatted string - * \param count The maximum number of characters to store in the buffer, including a terminating null character - * \param format A string that specifies the format of the output - * \param va A value identifying a variable arguments list - * \return The number of characters that COULD have been written into the buffer, not counting the terminating - * null character. A value equal or larger than count indicates truncation. Only when the returned value - * is non-negative and less than count, the string has been completely written. - */ -#define snprintf snprintf_ -#define vsnprintf vsnprintf_ -int snprintf_(char* buffer, size_t count, const char* format, ...); -int vsnprintf_(char* buffer, size_t count, const char* format, va_list va); - - -/** - * Tiny vprintf implementation - * \param format A string that specifies the format of the output - * \param va A value identifying a variable arguments list - * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character - */ -#define vprintf vprintf_ -int vprintf_(const char* format, va_list va); - +/************************************************************************** + * Copyright (C) 2018 + * Illini RoboMaster @ University of Illinois at Urbana-Champaign. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *************************************************************************/ /** - * printf with output function - * You may use this as dynamic alternative to printf() with its fixed _putchar() output - * \param out An output function which takes one character and an argument pointer - * \param arg An argument pointer for user data passed to output function - * \param format A string that specifies the format of the output - * \return The number of characters that are sent to the output function, not counting the terminating null character + * @author Yixiao Sun + * @date 2017-12-20 + * @file bsp_print.h + * @brief + * @log 2018-04-16 nickelliang */ -int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...); +#ifndef _BSP_PRINT_H_ +#define _BSP_PRINT_H_ -#ifdef __cplusplus -} -#endif +#include "printf.h" // third party tiny-printf implemnetations +/** @} */ -#endif // _PRINTF_H_ +#endif // _BSP_PRINT_ diff --git a/Third_Party_Libraries/tiny-printf/printf.c b/Third_Party_Libraries/tiny-printf/printf.c new file mode 100644 index 0000000..8a700ad --- /dev/null +++ b/Third_Party_Libraries/tiny-printf/printf.c @@ -0,0 +1,914 @@ +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2019, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on +// embedded systems with a very limited resources. These routines are thread +// safe and reentrant! +// Use this instead of the bloated standard/newlib printf cause these use +// malloc for printf (and may not be thread safe). +// +/////////////////////////////////////////////////////////////////////////////// + +#include +#include + +#include "printf.h" + + +// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the +// printf_config.h header file +// default: undefined +#ifdef PRINTF_INCLUDE_CONFIG_H +#include "printf_config.h" +#endif + + +// 'ntoa' conversion buffer size, this must be big enough to hold one converted +// numeric number including padded zeros (dynamically created on stack) +// default: 32 byte +#ifndef PRINTF_NTOA_BUFFER_SIZE +#define PRINTF_NTOA_BUFFER_SIZE 32U +#endif + +// 'ftoa' conversion buffer size, this must be big enough to hold one converted +// float number including padded zeros (dynamically created on stack) +// default: 32 byte +#ifndef PRINTF_FTOA_BUFFER_SIZE +#define PRINTF_FTOA_BUFFER_SIZE 32U +#endif + +// support for the floating point type (%f) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_FLOAT +#define PRINTF_SUPPORT_FLOAT +#endif + +// support for exponential floating point notation (%e/%g) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL +#define PRINTF_SUPPORT_EXPONENTIAL +#endif + +// define the default floating point precision +// default: 6 digits +#ifndef PRINTF_DEFAULT_FLOAT_PRECISION +#define PRINTF_DEFAULT_FLOAT_PRECISION 6U +#endif + +// define the largest float suitable to print with %f +// default: 1e9 +#ifndef PRINTF_MAX_FLOAT +#define PRINTF_MAX_FLOAT 1e9 +#endif + +// support for the long long types (%llu or %p) +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG +#define PRINTF_SUPPORT_LONG_LONG +#endif + +// support for the ptrdiff_t type (%t) +// ptrdiff_t is normally defined in as long or long long type +// default: activated +#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T +#define PRINTF_SUPPORT_PTRDIFF_T +#endif + +/////////////////////////////////////////////////////////////////////////////// + +// internal flag definitions +#define FLAGS_ZEROPAD (1U << 0U) +#define FLAGS_LEFT (1U << 1U) +#define FLAGS_PLUS (1U << 2U) +#define FLAGS_SPACE (1U << 3U) +#define FLAGS_HASH (1U << 4U) +#define FLAGS_UPPERCASE (1U << 5U) +#define FLAGS_CHAR (1U << 6U) +#define FLAGS_SHORT (1U << 7U) +#define FLAGS_LONG (1U << 8U) +#define FLAGS_LONG_LONG (1U << 9U) +#define FLAGS_PRECISION (1U << 10U) +#define FLAGS_ADAPT_EXP (1U << 11U) + + +// import float.h for DBL_MAX +#if defined(PRINTF_SUPPORT_FLOAT) +#include +#endif + + +// output function type +typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen); + + +// wrapper (used as buffer) for output function type +typedef struct { + void (*fct)(char character, void* arg); + void* arg; +} out_fct_wrap_type; + + +// internal buffer output +static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) +{ + if (idx < maxlen) { + ((char*)buffer)[idx] = character; + } +} + + +// internal null output +static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)character; (void)buffer; (void)idx; (void)maxlen; +} + + +// internal _putchar wrapper +static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)buffer; (void)idx; (void)maxlen; + if (character) { + _putchar(character); + } +} + + +// internal output function wrapper +static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) +{ + (void)idx; (void)maxlen; + if (character) { + // buffer is the output fct pointer + ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); + } +} + + +// internal secure strlen +// \return The length of the string (excluding the terminating 0) limited by 'maxsize' +static inline unsigned int _strnlen_s(const char* str, size_t maxsize) +{ + const char* s; + for (s = str; *s && maxsize--; ++s); + return (unsigned int)(s - str); +} + + +// internal test if char is a digit (0-9) +// \return true if char is a digit +static inline bool _is_digit(char ch) +{ + return (ch >= '0') && (ch <= '9'); +} + + +// internal ASCII string to unsigned int conversion +static unsigned int _atoi(const char** str) +{ + unsigned int i = 0U; + while (_is_digit(**str)) { + i = i * 10U + (unsigned int)(*((*str)++) - '0'); + } + return i; +} + + +// output the specified string in reverse, taking care of any zero-padding +static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags) +{ + const size_t start_idx = idx; + + // pad spaces up to given width + if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { + for (size_t i = len; i < width; i++) { + out(' ', buffer, idx++, maxlen); + } + } + + // reverse string + while (len) { + out(buf[--len], buffer, idx++, maxlen); + } + + // append pad spaces up to given width + if (flags & FLAGS_LEFT) { + while (idx - start_idx < width) { + out(' ', buffer, idx++, maxlen); + } + } + + return idx; +} + + +// internal itoa format +static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) +{ + // pad leading zeros + if (!(flags & FLAGS_LEFT)) { + if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + width--; + } + while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + } + + // handle hash + if (flags & FLAGS_HASH) { + if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { + len--; + if (len && (base == 16U)) { + len--; + } + } + if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'x'; + } + else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'X'; + } + else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + buf[len++] = 'b'; + } + if (len < PRINTF_NTOA_BUFFER_SIZE) { + buf[len++] = '0'; + } + } + + if (len < PRINTF_NTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); +} + + +// internal itoa for 'long' type +static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || value) { + do { + const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); + } + + return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); +} + + +// internal itoa for 'long long' type +#if defined(PRINTF_SUPPORT_LONG_LONG) +static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_NTOA_BUFFER_SIZE]; + size_t len = 0U; + + // no hash for 0 values + if (!value) { + flags &= ~FLAGS_HASH; + } + + // write if precision != 0 and value is != 0 + if (!(flags & FLAGS_PRECISION) || value) { + do { + const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; + value /= base; + } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); + } + + return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags); +} +#endif // PRINTF_SUPPORT_LONG_LONG + + +#if defined(PRINTF_SUPPORT_FLOAT) + +#if defined(PRINTF_SUPPORT_EXPONENTIAL) +// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT +static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags); +#endif + + +// internal ftoa for fixed decimal floating point +static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) +{ + char buf[PRINTF_FTOA_BUFFER_SIZE]; + size_t len = 0U; + double diff = 0.0; + + // powers of 10 + static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + + // test for special values + if (value != value) + return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags); + if (value < -DBL_MAX) + return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags); + if (value > DBL_MAX) + return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags); + + // test for very large values + // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad + if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) { +#if defined(PRINTF_SUPPORT_EXPONENTIAL) + return _etoa(out, buffer, idx, maxlen, value, prec, width, flags); +#else + return 0U; +#endif + } + + // test for negative + bool negative = false; + if (value < 0) { + negative = true; + value = 0 - value; + } + + // set default precision, if not set explicitly + if (!(flags & FLAGS_PRECISION)) { + prec = PRINTF_DEFAULT_FLOAT_PRECISION; + } + // limit precision to 9, cause a prec >= 10 can lead to overflow errors + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { + buf[len++] = '0'; + prec--; + } + + int whole = (int)value; + double tmp = (value - whole) * pow10[prec]; + unsigned long frac = (unsigned long)tmp; + diff = tmp - frac; + + if (diff > 0.5) { + ++frac; + // handle rollover, e.g. case 0.99 with prec 1 is 1.0 + if (frac >= pow10[prec]) { + frac = 0; + ++whole; + } + } + else if (diff < 0.5) { + } + else if ((frac == 0U) || (frac & 1U)) { + // if halfway, round up if odd OR if last digit is 0 + ++frac; + } + + if (prec == 0U) { + diff = value - (double)whole; + if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) { + // exactly 0.5 and ODD, then round up + // 1.5 -> 2, but 2.5 -> 2 + ++whole; + } + } + else { + unsigned int count = prec; + // now do fractional part, as an unsigned number + while (len < PRINTF_FTOA_BUFFER_SIZE) { + --count; + buf[len++] = (char)(48U + (frac % 10U)); + if (!(frac /= 10U)) { + break; + } + } + // add extra 0s + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { + buf[len++] = '0'; + } + if (len < PRINTF_FTOA_BUFFER_SIZE) { + // add decimal + buf[len++] = '.'; + } + } + + // do whole part, number is reversed + while (len < PRINTF_FTOA_BUFFER_SIZE) { + buf[len++] = (char)(48 + (whole % 10)); + if (!(whole /= 10)) { + break; + } + } + + // pad leading zeros + if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { + if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + width--; + } + while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { + buf[len++] = '0'; + } + } + + if (len < PRINTF_FTOA_BUFFER_SIZE) { + if (negative) { + buf[len++] = '-'; + } + else if (flags & FLAGS_PLUS) { + buf[len++] = '+'; // ignore the space if the '+' exists + } + else if (flags & FLAGS_SPACE) { + buf[len++] = ' '; + } + } + + return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags); +} + + +#if defined(PRINTF_SUPPORT_EXPONENTIAL) +// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse +static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) +{ + // check for NaN and special values + if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) { + return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags); + } + + // determine the sign + const bool negative = value < 0; + if (negative) { + value = -value; + } + + // default precision + if (!(flags & FLAGS_PRECISION)) { + prec = PRINTF_DEFAULT_FLOAT_PRECISION; + } + + // determine the decimal exponent + // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) + union { + uint64_t U; + double F; + } conv; + + conv.F = value; + int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2 + conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2) + // now approximate log10 from the log2 integer part and an expansion of ln around 1.5 + int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168); + // now we want to compute 10^expval but we want to be sure it won't overflow + exp2 = (int)(expval * 3.321928094887362 + 0.5); + const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453; + const double z2 = z * z; + conv.U = (uint64_t)(exp2 + 1023) << 52U; + // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex + conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); + // correct for rounding errors + if (value < conv.F) { + expval--; + conv.F /= 10; + } + + // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters + unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U; + + // in "%g" mode, "prec" is the number of *significant figures* not decimals + if (flags & FLAGS_ADAPT_EXP) { + // do we want to fall-back to "%f" mode? + if ((value >= 1e-4) && (value < 1e6)) { + if ((int)prec > expval) { + prec = (unsigned)((int)prec - expval - 1); + } + else { + prec = 0; + } + flags |= FLAGS_PRECISION; // make sure _ftoa respects precision + // no characters in exponent + minwidth = 0U; + expval = 0; + } + else { + // we use one sigfig for the whole part + if ((prec > 0) && (flags & FLAGS_PRECISION)) { + --prec; + } + } + } + + // will everything fit? + unsigned int fwidth = width; + if (width > minwidth) { + // we didn't fall-back so subtract the characters required for the exponent + fwidth -= minwidth; + } else { + // not enough characters, so go back to default sizing + fwidth = 0U; + } + if ((flags & FLAGS_LEFT) && minwidth) { + // if we're padding on the right, DON'T pad the floating part + fwidth = 0U; + } + + // rescale the float value + if (expval) { + value /= conv.F; + } + + // output the floating part + const size_t start_idx = idx; + idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); + + // output the exponent part + if (minwidth) { + // output the exponential symbol + out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); + // output the exponent value + idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); + // might need to right-pad spaces + if (flags & FLAGS_LEFT) { + while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); + } + } + return idx; +} +#endif // PRINTF_SUPPORT_EXPONENTIAL +#endif // PRINTF_SUPPORT_FLOAT + + +// internal vsnprintf +static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va) +{ + unsigned int flags, width, precision, n; + size_t idx = 0U; + + if (!buffer) { + // use null output function + out = _out_null; + } + + while (*format) + { + // format specifier? %[flags][width][.precision][length] + if (*format != '%') { + // no + out(*format, buffer, idx++, maxlen); + format++; + continue; + } + else { + // yes, evaluate it + format++; + } + + // evaluate flags + flags = 0U; + do { + switch (*format) { + case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; + case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; + case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; + case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break; + case '#': flags |= FLAGS_HASH; format++; n = 1U; break; + default : n = 0U; break; + } + } while (n); + + // evaluate width field + width = 0U; + if (_is_digit(*format)) { + width = _atoi(&format); + } + else if (*format == '*') { + const int w = va_arg(va, int); + if (w < 0) { + flags |= FLAGS_LEFT; // reverse padding + width = (unsigned int)-w; + } + else { + width = (unsigned int)w; + } + format++; + } + + // evaluate precision field + precision = 0U; + if (*format == '.') { + flags |= FLAGS_PRECISION; + format++; + if (_is_digit(*format)) { + precision = _atoi(&format); + } + else if (*format == '*') { + const int prec = (int)va_arg(va, int); + precision = prec > 0 ? (unsigned int)prec : 0U; + format++; + } + } + + // evaluate length field + switch (*format) { + case 'l' : + flags |= FLAGS_LONG; + format++; + if (*format == 'l') { + flags |= FLAGS_LONG_LONG; + format++; + } + break; + case 'h' : + flags |= FLAGS_SHORT; + format++; + if (*format == 'h') { + flags |= FLAGS_CHAR; + format++; + } + break; +#if defined(PRINTF_SUPPORT_PTRDIFF_T) + case 't' : + flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; +#endif + case 'j' : + flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; + case 'z' : + flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); + format++; + break; + default : + break; + } + + // evaluate specifier + switch (*format) { + case 'd' : + case 'i' : + case 'u' : + case 'x' : + case 'X' : + case 'o' : + case 'b' : { + // set the base + unsigned int base; + if (*format == 'x' || *format == 'X') { + base = 16U; + } + else if (*format == 'o') { + base = 8U; + } + else if (*format == 'b') { + base = 2U; + } + else { + base = 10U; + flags &= ~FLAGS_HASH; // no hash for dec format + } + // uppercase + if (*format == 'X') { + flags |= FLAGS_UPPERCASE; + } + + // no plus or space flag for u, x, X, o, b + if ((*format != 'i') && (*format != 'd')) { + flags &= ~(FLAGS_PLUS | FLAGS_SPACE); + } + + // ignore '0' flag when precision is given + if (flags & FLAGS_PRECISION) { + flags &= ~FLAGS_ZEROPAD; + } + + // convert the integer + if ((*format == 'i') || (*format == 'd')) { + // signed + if (flags & FLAGS_LONG_LONG) { +#if defined(PRINTF_SUPPORT_LONG_LONG) + const long long value = va_arg(va, long long); + idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); +#endif + } + else if (flags & FLAGS_LONG) { + const long value = va_arg(va, long); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); + } + else { + const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); + } + } + else { + // unsigned + if (flags & FLAGS_LONG_LONG) { +#if defined(PRINTF_SUPPORT_LONG_LONG) + idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags); +#endif + } + else if (flags & FLAGS_LONG) { + idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags); + } + else { + const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int); + idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); + } + } + format++; + break; + } +#if defined(PRINTF_SUPPORT_FLOAT) + case 'f' : + case 'F' : + if (*format == 'F') flags |= FLAGS_UPPERCASE; + idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); + format++; + break; +#if defined(PRINTF_SUPPORT_EXPONENTIAL) + case 'e': + case 'E': + case 'g': + case 'G': + if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP; + if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE; + idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); + format++; + break; +#endif // PRINTF_SUPPORT_EXPONENTIAL +#endif // PRINTF_SUPPORT_FLOAT + case 'c' : { + unsigned int l = 1U; + // pre padding + if (!(flags & FLAGS_LEFT)) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + // char output + out((char)va_arg(va, int), buffer, idx++, maxlen); + // post padding + if (flags & FLAGS_LEFT) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + format++; + break; + } + + case 's' : { + const char* p = va_arg(va, char*); + unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1); + // pre padding + if (flags & FLAGS_PRECISION) { + l = (l < precision ? l : precision); + } + if (!(flags & FLAGS_LEFT)) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + // string output + while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { + out(*(p++), buffer, idx++, maxlen); + } + // post padding + if (flags & FLAGS_LEFT) { + while (l++ < width) { + out(' ', buffer, idx++, maxlen); + } + } + format++; + break; + } + + case 'p' : { + width = sizeof(void*) * 2U; + flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; +#if defined(PRINTF_SUPPORT_LONG_LONG) + const bool is_ll = sizeof(uintptr_t) == sizeof(long long); + if (is_ll) { + idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags); + } + else { +#endif + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags); +#if defined(PRINTF_SUPPORT_LONG_LONG) + } +#endif + format++; + break; + } + + case '%' : + out('%', buffer, idx++, maxlen); + format++; + break; + + default : + out(*format, buffer, idx++, maxlen); + format++; + break; + } + } + + // termination + out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen); + + // return written chars without terminating \0 + return (int)idx; +} + + +/////////////////////////////////////////////////////////////////////////////// + +int printf_(const char* format, ...) +{ + va_list va; + va_start(va, format); + char buffer[1]; + const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); + va_end(va); + return ret; +} + + +int sprintf_(char* buffer, const char* format, ...) +{ + va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va); + va_end(va); + return ret; +} + + +int snprintf_(char* buffer, size_t count, const char* format, ...) +{ + va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, count, format, va); + va_end(va); + return ret; +} + + +int vprintf_(const char* format, va_list va) +{ + char buffer[1]; + return _vsnprintf(_out_char, buffer, (size_t)-1, format, va); +} + + +int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) +{ + return _vsnprintf(_out_buffer, buffer, count, format, va); +} + + +int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) +{ + va_list va; + va_start(va, format); + const out_fct_wrap_type out_fct_wrap = { out, arg }; + const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); + va_end(va); + return ret; +} diff --git a/Third_Party_Libraries/tiny-printf/printf.h b/Third_Party_Libraries/tiny-printf/printf.h new file mode 100644 index 0000000..6104ccf --- /dev/null +++ b/Third_Party_Libraries/tiny-printf/printf.h @@ -0,0 +1,117 @@ +/////////////////////////////////////////////////////////////////////////////// +// \author (c) Marco Paland (info@paland.com) +// 2014-2019, PALANDesign Hannover, Germany +// +// \license The MIT License (MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on +// embedded systems with a very limited resources. +// Use this instead of bloated standard/newlib printf. +// These routines are thread safe and reentrant. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _PRINTF_H_ +#define _PRINTF_H_ + +#include +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * Output a character to a custom device like UART, used by the printf() function + * This function is declared here only. You have to write your custom implementation somewhere + * \param character Character to output + */ +void _putchar(char character); + + +/** + * Tiny printf implementation + * You have to implement _putchar if you use printf() + * To avoid conflicts with the regular printf() API it is overridden by macro defines + * and internal underscore-appended functions like printf_() are used + * \param format A string that specifies the format of the output + * \return The number of characters that are written into the array, not counting the terminating null character + */ +#define printf printf_ +int printf_(const char* format, ...); + + +/** + * Tiny sprintf implementation + * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD! + * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output! + * \param format A string that specifies the format of the output + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character + */ +#define sprintf sprintf_ +int sprintf_(char* buffer, const char* format, ...); + + +/** + * Tiny snprintf/vsnprintf implementation + * \param buffer A pointer to the buffer where to store the formatted string + * \param count The maximum number of characters to store in the buffer, including a terminating null character + * \param format A string that specifies the format of the output + * \param va A value identifying a variable arguments list + * \return The number of characters that COULD have been written into the buffer, not counting the terminating + * null character. A value equal or larger than count indicates truncation. Only when the returned value + * is non-negative and less than count, the string has been completely written. + */ +#define snprintf snprintf_ +#define vsnprintf vsnprintf_ +int snprintf_(char* buffer, size_t count, const char* format, ...); +int vsnprintf_(char* buffer, size_t count, const char* format, va_list va); + + +/** + * Tiny vprintf implementation + * \param format A string that specifies the format of the output + * \param va A value identifying a variable arguments list + * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character + */ +#define vprintf vprintf_ +int vprintf_(const char* format, va_list va); + + +/** + * printf with output function + * You may use this as dynamic alternative to printf() with its fixed _putchar() output + * \param out An output function which takes one character and an argument pointer + * \param arg An argument pointer for user data passed to output function + * \param format A string that specifies the format of the output + * \return The number of characters that are sent to the output function, not counting the terminating null character + */ +int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...); + + +#ifdef __cplusplus +} +#endif + + +#endif // _PRINTF_H_ From 7728ec7dc17151931a22006aaf36274289322fcf Mon Sep 17 00:00:00 2001 From: Temporary User Date: Fri, 22 Nov 2019 18:32:28 -0600 Subject: [PATCH 5/5] remove doc group for bsp print --- BSP/bsp_print.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/BSP/bsp_print.h b/BSP/bsp_print.h index 3134606..f0ffedb 100644 --- a/BSP/bsp_print.h +++ b/BSP/bsp_print.h @@ -29,6 +29,4 @@ #include "printf.h" // third party tiny-printf implemnetations -/** @} */ - #endif // _BSP_PRINT_