From 2599d92e82c857c3f1c4dd80abc24f64a88bf687 Mon Sep 17 00:00:00 2001 From: Gabriel Huau Date: Wed, 26 Aug 2015 21:48:59 -0700 Subject: [PATCH] move code to use uprintf instead of snprintf+serial_write This simplify a lot the code to output anything on any UART. --- adc/main.c | 11 ++--------- eeprom/main.c | 16 ++++------------ i2c_temp/main.c | 4 +--- ultrasonic_sensor/main.c | 5 +---- 4 files changed, 8 insertions(+), 28 deletions(-) diff --git a/adc/main.c b/adc/main.c index 7a8cec1..b4ef4e7 100644 --- a/adc/main.c +++ b/adc/main.c @@ -86,11 +86,7 @@ int adc_display(int adc_num, int uart_num) if (ret < 0) { return ret; } else { - /* Always display, even if value has already been read */ - char buff[40]; - int len = 0; - len = snprintf(buff, 40, "ADC(%d): %d (raw: 0x%04x)\r\n", adc_num, val, val); - serial_write(uart_num, buff, len); + uprintf(uart_num, "ADC(%d): %d (raw: 0x%04x)\r\n", adc_num, val, val); } return val; } @@ -111,15 +107,12 @@ void TMP36_display(int adc_num, int uart_num) msleep(8); ret = adc_get_value(&val, adc_num); if (ret == 0) { - char buff[60]; - int len = 0; int micro_volts = 0; /* depends on vref, should use a precise 3.0V Vref and multiply by 3000 */ micro_volts = (val * 3200); int converted = ((micro_volts / 100) - 5000); - len = snprintf(buff, 60, "TMP36: %d,%d (orig: %d, raw: %04x)\r\n", + uprintf(uart_num, "TMP36: %d,%d (orig: %d, raw: %04x)\r\n", (converted / 100), (converted % 100), val, val); - serial_write(uart_num, buff, len); } } diff --git a/eeprom/main.c b/eeprom/main.c index 24eb6d8..88383a5 100644 --- a/eeprom/main.c +++ b/eeprom/main.c @@ -144,22 +144,14 @@ void module_desc_dump(uint8_t serial_num) } /* Send the content of the header */ serial_write(serial_num, "Module :\r\n", 10); - len = snprintf(buff, 16, "serial: %d, ", desc.serial_number); - len += snprintf((buff + len), 15, "ver: %d\r\n", desc.version); - len += snprintf((buff + len), 16, "cap: 0x%04x\r\n", desc.capabilities); + uprintf(1, "serial: %d, ", desc.serial_number); + uprintf(1, "ver: %d\r\n", desc.version); + uprintf(1, "cap: 0x%04x\r\n", desc.capabilities); /* Get and send the module name */ if (desc.name_size >= DUMP_BUFF_SIZE) { desc.name_size = DUMP_BUFF_SIZE - len - 3; } - ret = eeprom_read(EEPROM_ADDR, desc.name_offset, (buff + len), desc.name_size); - if (ret == desc.name_size) { - len += ret; - len += snprintf((buff + len), 3, "\r\n"); - } - ret = 0; - do { - ret += serial_write(serial_num, (buff + ret), (len - ret)); - } while (ret < len); + eeprom_read(EEPROM_ADDR, desc.name_offset, (buff + len), desc.name_size); mod_gpio_demo_eeprom_cs_release(); } diff --git a/i2c_temp/main.c b/i2c_temp/main.c index 8f3f3ff..1039383 100644 --- a/i2c_temp/main.c +++ b/i2c_temp/main.c @@ -94,7 +94,6 @@ void temp_config(uint8_t addr, int uart_num) void temp_display(uint8_t addr, int uart_num) { - char buff[40]; uint16_t raw = 0; int deci_degrees = 0; int len = 0; @@ -105,9 +104,8 @@ void temp_display(uint8_t addr, int uart_num) if (len != 0) { serial_write(uart_num, "Temp read error\r\n", 19); } else { - len = snprintf(buff, 40, "Temp read: %d,%d - raw: 0x%04x.\r\n", + uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\r\n", (deci_degrees/10), (deci_degrees%10), raw); - serial_write(uart_num, buff, len); } } diff --git a/ultrasonic_sensor/main.c b/ultrasonic_sensor/main.c index 2a6112a..930acb3 100644 --- a/ultrasonic_sensor/main.c +++ b/ultrasonic_sensor/main.c @@ -123,8 +123,6 @@ void pulse_feedback(uint32_t gpio) { /***************************************************************************** */ int main(void) { - char buff[60]; - int len = 0; uint32_t next_time = 0; uint32_t delay = 0; @@ -156,8 +154,7 @@ int main(void) { distance = ((pulse_duration * 10) / (get_main_clock() / (1000*1000))); distance = distance / 29; /* Send value on serial */ - len = snprintf(buff, 60, "dist: %dmm\n", distance); - serial_write(0, buff, len); + uprintf(0, "dist: %dmm\n", distance); /* And wait at least 50ms between loops */ delay = next_time - systick_get_tick_count(); -- 2.43.0