2021-01-07 10:29:31 -04:00
/*
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 < http : //www.gnu.org/licenses/>.
*/
/*
Printf . cpp : We demonstrate the use of the printf ( ) and snprintf ( ) functions
*/
2015-08-11 03:28:43 -03:00
# include <AP_Common/AP_Common.h>
2015-10-20 09:34:10 -03:00
# include <AP_HAL/AP_HAL.h>
2013-05-28 04:21:56 -03:00
2021-01-07 10:29:31 -04:00
void setup ( ) ; // declaration of the setup() function
void loop ( ) ; // declaration of the loop() function
2017-04-13 08:31:16 -03:00
2021-01-07 10:29:31 -04:00
const AP_HAL : : HAL & hal = AP_HAL : : get_HAL ( ) ; // create a reference to AP_HAL::HAL object to get access to hardware specific functions. For more info see <https://ardupilot.org/dev/docs/learning-ardupilot-the-example-sketches.html/>
2013-05-28 04:21:56 -03:00
2021-01-07 10:29:31 -04:00
// the setup function runs once when the board powers up
2013-05-28 04:21:56 -03:00
void setup ( void ) {
2021-01-07 10:29:31 -04:00
hal . console - > printf ( " Starting Printf test \n " ) ; // print a starting message
2013-05-28 04:21:56 -03:00
}
2021-01-07 10:29:31 -04:00
//create a struct array float_tests
2013-05-28 04:21:56 -03:00
static const struct {
2021-01-07 10:29:31 -04:00
// holds the char sequence representing the format specifier
2013-05-28 04:21:56 -03:00
const char * fmt ;
2021-01-07 10:29:31 -04:00
// holds a float value
float v ;
// holds the char sequence that would be printed in accordance with the format specifier defined(fmt)
const char * result ;
2013-05-28 04:21:56 -03:00
} 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 " } ,
2013-09-21 02:45:05 -03:00
{ " %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 " } ,
2013-09-21 02:45:05 -03:00
{ " %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 " } ,
2013-05-28 04:21:56 -03:00
} ;
2021-01-07 10:29:31 -04:00
// test_printf_floats(void) : tests the printf() and snprintf() function against the float numbers defined in float_tests
2018-09-06 00:31:49 -03:00
static void test_printf_floats ( void )
2013-05-28 04:21:56 -03:00
{
2018-09-06 00:31:49 -03:00
hal . console - > printf ( " Starting Printf floats test \n " ) ;
2013-05-28 04:21:56 -03:00
uint8_t i ;
2021-01-07 10:29:31 -04:00
// 30 bytes long char buffer(Expected length of string : 29)
2013-05-28 04:21:56 -03:00
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 + + ) {
2021-01-07 10:29:31 -04:00
// create a format string(buf) in accordance with the format specifier and the float values defined for every element of float_tests[]
int ret = hal . util - > snprintf ( buf , sizeof ( buf ) , float_tests [ i ] . fmt , ( double ) float_tests [ i ] . v ) ; //For more info, see : http://www.cplusplus.com/reference/cstdio/snprintf/
// comparing the results of the snprintf() and results defined in the float_tests[] :
// 1. check whether the strings are equal or not
2013-05-28 04:21:56 -03:00
if ( strcmp ( buf , float_tests [ i ] . result ) ! = 0 ) {
2016-05-03 22:21:03 -03:00
hal . console - > printf ( " Failed float_tests[%u] '%s' -> '%s' should be '%s' \n " ,
( unsigned ) i ,
2013-05-28 04:21:56 -03:00
float_tests [ i ] . fmt ,
2013-09-21 00:29:52 -03:00
buf ,
float_tests [ i ] . result ) ;
2013-05-28 04:21:56 -03:00
failures + + ;
}
2021-01-07 10:29:31 -04:00
// 2. check whether the len of the strings is equal or not
2016-05-03 22:25:58 -03:00
if ( ret ! = ( int ) strlen ( float_tests [ i ] . result ) ) {
2016-05-03 22:21:03 -03:00
hal . console - > printf ( " Failed float_tests[%u] ret=%d/%d '%s' should be '%s' \n " ,
( unsigned ) i ,
2013-09-21 02:45:05 -03:00
ret , ( int ) strlen ( float_tests [ i ] . result ) ,
float_tests [ i ] . fmt ,
float_tests [ i ] . result ) ;
2016-05-03 22:21:03 -03:00
failures + + ;
2013-09-21 02:45:05 -03:00
}
2013-05-28 04:21:56 -03:00
}
hal . console - > printf ( " %u failures \n " , ( unsigned ) failures ) ;
}
2021-01-07 10:29:31 -04:00
// test_printf_null_termination(void) : tests the printf() and the snprintf() function against a char sequence and whether they consider the terminating null character '\0'
2018-09-06 00:31:49 -03:00
static void test_printf_null_termination ( void )
{
hal . console - > printf ( " Starting Printf null-termination tests \n " ) ;
2021-01-07 10:29:31 -04:00
// 10 bytes long char buffer(Expected length of string : 9)
char buf [ 10 ] ;
// create a format string(buf) in accordance with "ABCDEABCDE" and length of buf[]
int ret = hal . util - > snprintf ( buf , sizeof ( buf ) , " %s " , " ABCDEABCDE " ) ; //For more info, see : http://www.cplusplus.com/reference/cstdio/snprintf/
// store the expected length of string
const int want = 9 ;
// comparing the results of the snprintf() function :
// 1. check whether the expected length of the string is equal to the buffer(buf) length or not
if ( ret ! = want ) {
hal . console - > printf ( " snprintf returned %d expected %d \n " , ret , want ) ;
2018-09-06 00:31:49 -03:00
}
2021-01-07 10:29:31 -04:00
// 2. check whether the buffer(buf) is equal to "ABCDEABCD" or not
if ( ! strncmp ( buf , " ABCDEABCD " , sizeof ( buf ) ) ) {
hal . console - > printf ( " Bad snprintf string (%s) \n " , buf ) ;
2018-09-06 00:31:49 -03:00
}
}
2021-01-07 10:29:31 -04:00
2018-09-06 00:31:49 -03:00
static void test_printf ( void )
{
test_printf_floats ( ) ;
test_printf_null_termination ( ) ;
}
2021-01-07 10:29:31 -04:00
// the loop function runs over and over again forever
2016-05-03 22:21:03 -03:00
void loop ( void )
{
2013-05-28 04:21:56 -03:00
test_printf ( ) ;
2021-01-07 10:29:31 -04:00
// give a delay of 1000ms or 1s
2013-05-28 04:21:56 -03:00
hal . scheduler - > delay ( 1000 ) ;
}
2021-01-07 10:29:31 -04:00
AP_HAL_MAIN ( ) ; // HAL Macro that declares the main function. For more info see <https://ardupilot.org/dev/docs/learning-ardupilot-the-example-sketches.html/>