ardupilot/libraries/AP_HAL/examples/Printf/Printf.cpp

112 lines
3.6 KiB
C++
Raw Normal View History

#include <AP_HAL/AP_HAL.h>
#include <AP_HAL_AVR/AP_HAL_AVR.h>
#include <AP_HAL_SITL/AP_HAL_SITL.h>
#include <AP_HAL_PX4/AP_HAL_PX4.h>
#include <AP_HAL_Linux/AP_HAL_Linux.h>
#include <AP_HAL_Empty/AP_HAL_Empty.h>
#include <AP_Common/AP_Common.h>
#include <AP_Baro/AP_Baro.h>
#include <AP_ADC/AP_ADC.h>
#include <AP_GPS/AP_GPS.h>
#include <AP_InertialSensor/AP_InertialSensor.h>
#include <AP_Notify/AP_Notify.h>
#include <DataFlash/DataFlash.h>
#include <GCS_MAVLink/GCS_MAVLink.h>
#include <AP_Mission/AP_Mission.h>
#include <StorageManager/StorageManager.h>
#include <AP_Terrain/AP_Terrain.h>
#include <AP_Compass/AP_Compass.h>
#include <AP_Declination/AP_Declination.h>
#include <SITL/SITL.h>
#include <Filter/Filter.h>
#include <AP_Param/AP_Param.h>
#include <AP_Progmem/AP_Progmem.h>
#include <AP_Math/AP_Math.h>
#include <AP_AHRS/AP_AHRS.h>
#include <AP_Airspeed/AP_Airspeed.h>
#include <AP_Vehicle/AP_Vehicle.h>
#include <AP_ADC_AnalogSource/AP_ADC_AnalogSource.h>
#include <AP_NavEKF/AP_NavEKF.h>
#include <AP_Rally/AP_Rally.h>
#include <AP_BattMonitor/AP_BattMonitor.h>
#include <AP_RangeFinder/AP_RangeFinder.h>
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
void setup(void) {
hal.console->println_P(PSTR("Starting Printf test"));
}
static const struct {
const char *fmt;
float v;
const char *result;
} float_tests[] = {
{ "%f", 3.71f, "3.710000" },
{ "%.1f", 3.71f, "3.7" },
{ "%.1f", 3.75f, "3.8" },
{ "%.2f", 3.75f, "3.75" },
2013-09-21 00:29:52 -03:00
{ "%.7f", 3.75f, "3.750000" },
{ "%f", 10.4f, "10.40000" },
{ "%f", 10.6f, "10.60000" },
{ "%f", 1020.4f, "1020.400" },
{ "%f", 1030.6f, "1030.600" },
{ "%f", 10.123456f, "10.12346" },
{ "%f", 102.123456f, "102.1235" },
{ "%f", 1020.123456f, "1020.123" },
{ "%.6f", 10.123456f, "10.12346" },
{ "%.6f", 102.123456f, "102.1235" },
{ "%.6f", 1020.123456f, "1020.123" },
{ "%f", 10304052.6f, "1.030405e+07" },
{ "%f", 103040501.6f, "1.030405e+08" },
{ "%f", 1030405023.6f, "1.030405e+09" },
2013-09-21 00:29:52 -03:00
{ "%f", -1030.6f, "-1030.600" },
{ "%f", -10304052.6f, "-1.030405e+07" },
{ "%f", -103040501.6f, "-1.030405e+08" },
{ "%f", -1030405023.6f, "-1.030405e+09" },
{ "%e", 103040501.6f, "1.030405e+08" },
{ "%g", 103040501.6f, "1.03041e+08" },
{ "%e", -103040501.6f, "-1.030405e+08" },
{ "%g", -103040501.6f, "-1.03041e+08" },
2013-09-21 00:29:52 -03:00
{ "%.0f", 10.4f, "10" },
{ "%.0f", 10.6f, "11" },
{ "%.1f", 10.4f, "10.4" },
{ "%.1f", 10.6f, "10.6" },
};
static void test_printf(void)
{
uint8_t i;
char buf[30];
uint8_t failures = 0;
hal.console->printf("Running printf tests\n");
2015-07-20 16:53:47 -03:00
for (i=0; i < ARRAY_SIZE(float_tests); i++) {
int ret = hal.util->snprintf(buf, sizeof(buf), float_tests[i].fmt, float_tests[i].v);
if (strcmp(buf, float_tests[i].result) != 0) {
2013-09-21 00:29:52 -03:00
hal.console->printf("Failed float_tests[%u] '%s' -> '%s' should be '%s'\n",
(unsigned)i,
float_tests[i].fmt,
2013-09-21 00:29:52 -03:00
buf,
float_tests[i].result);
failures++;
}
if (ret != strlen(float_tests[i].result)) {
hal.console->printf("Failed float_tests[%u] ret=%d/%d '%s' should be '%s'\n",
(unsigned)i,
ret, (int)strlen(float_tests[i].result),
float_tests[i].fmt,
float_tests[i].result);
failures++;
}
}
hal.console->printf("%u failures\n", (unsigned)failures);
}
void loop(void)
{
test_printf();
hal.scheduler->delay(1000);
}
AP_HAL_MAIN();