ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
Data Structures | Macros | Typedefs | Functions
mathio.h File Reference

Math IO functions, and verious conversion code with floating point support. More...

Go to the source code of this file.

Data Structures

struct  _printf_t
 undefine any potential macro version of these functions More...
 
union  f_t
 format specifier flags More...
 

Macros

#define MEMSPACE
 
#define __memx
 
#define WEAK_ATR   __attribute__((weak))
 

Typedefs

typedef struct _printf_t printf_t
 undefine any potential macro version of these functions More...
 

Functions

int putchar (int c)
 put a character to stdout See fdevopen() sets stream->put get for TTY devices More...
 
MEMSPACE int atodigit (int c, int radix)
 Convert ASCII character to radix based digit , or -1. More...
 
MEMSPACE long atoh (const char *p)
 Convert ASCII hex string to long integer. More...
 
MEMSPACE long aton (char *str, int base)
 Convert ASCII string to number in a given base. More...
 
MEMSPACE int mul10str (uint8_t *str, int size)
 Fast multiply number of any size by 10. More...
 
MEMSPACE long strtol (const char *nptr, char **endptr, int base)
 Convert ASCII string to number in a given base. More...
 
MEMSPACE long long strtoll (const char *nptr, char **endptr, int base)
 Convert ASCII string to number in a given base. More...
 
MEMSPACE int atoi (const char *str)
 Convert ASCII string to number in base 10. More...
 
MEMSPACE long atol (const char *str)
 Convert ASCII string to number in base 10. More...
 
MEMSPACE double iexp (double num, int exp)
 
MEMSPACE double scale10 (double num, int *exp)
 
MEMSPACE double strtod (const char *nptr, char **endptr)
 
MEMSPACE double atof (const char *str)
 
MEMSPACE size_t WEAK_ATR strlen (const char *str)
 String Length. More...
 
MEMSPACE int WEAK_ATR isdigit (int c)
 test if a character is a digit More...
 
MEMSPACE void WEAK_ATR reverse (char *str)
 Reverse a string in place Example: abcdef -> fedcba. More...
 
MEMSPACE void WEAK_ATR strupper (char *str)
 UPPERCASE a string. More...
 
MEMSPACE int bin2num (uint8_t *str, int strmax, int nummin, int base, uint8_t *nump, int numsize, int sign_ch)
 Convert an unsigned number (numsize bytes in size) to ASCII in specified base Notes: No limit except available memory on number size. More...
 
MEMSPACE void pch_init (char *str, int max)
 Initialize character buffer with limits. More...
 
MEMSPACE int pch (char ch)
 Put character in buffer with limits. More...
 
MEMSPACE int pch_ind (void)
 Return current index of character buffer with limits. More...
 
MEMSPACE int pch_max_ind (void)
 Return maximum valid index for character buffer. More...
 
MEMSPACE void print_flags (f_t f)
 print flags set in t_t structure More...
 
MEMSPACE int p_ntoa (uint8_t *nump, int numsize, char *str, int strmax, int radix, int width, int prec, f_t f)
 Convert number an base 2 .. 16 to ASCII with optional sign Notes: 1) Numbers can be any number of digits long - limited only by memory available To print negative numbers convert to positive before calling this function and set f.b.neg 2) Warning: as with printf width and prec are only minimum sizes - results can be bigger We assume all numbers are positive: More...
 
MEMSPACE int p_ftoa (double val, char *str, int max, int width, int prec, f_t f)
 
MEMSPACE int p_etoa (double val, char *str, int max, int width, int prec, f_t f)
 
MEMSPACE void _puts_pad (printf_t *fn, char *s, int width, int count, int left)
 
MEMSPACE void _printf_fn (printf_t *fn, __memx const char *fmt, va_list va)
 vsnprintf function More...
 
MEMSPACE void _putc_buffer_fn (struct _printf_t *p, char ch)
 _putc_buffer_fn - character output to a string buffer Used by snprintf and vsnprintf You can make _printf_fn call this helper for each character More...
 
MEMSPACE int vsnprintf (char *str, size_t size, const char *format, va_list va)
 vsnprintf function More...
 
MEMSPACE int snprintf (char *str, size_t size, const char *format,...)
 snprintf function More...
 
MEMSPACE int printf (const char *format,...)
 
int sscanf (const char *strp, const char *fmt,...)
 

Detailed Description

Math IO functions, and verious conversion code with floating point support.

Copyright © 2014-2017 Mike Gore, All rights reserved. GPL License
See also
http://github.com/magore/hp85disk
http://github.com/magore/hp85disk/COPYRIGHT.md for specific Copyright details
You are free to use this code under the terms of GPL
please retain a copy of this notice in any code you use it in.

This 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 software 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/.

Definition in file mathio.h.

Macro Definition Documentation

#define __memx

Definition at line 38 of file mathio.h.

Referenced by _printf_fn(), and snprintf().

#define MEMSPACE

Definition at line 33 of file mathio.h.

#define WEAK_ATR   __attribute__((weak))

Definition at line 45 of file mathio.h.

Typedef Documentation

typedef struct _printf_t printf_t

undefine any potential macro version of these functions

We let printf use user defined I/O functions

Function Documentation

MEMSPACE void _printf_fn ( printf_t fn,
__memx const char *  fmt,
va_list  va 
)

vsnprintf function

Parameters
[out]fnoutput character function pointer
[in]fmtprintf forat string
[in]vava_list arguments
Returns
size of string

Calling Variadic Functions

  • exceprt from https://www.gnu.org/software/libc/manual/html_node/Calling-Variadics.html Since the prototype doesn’t specify types for optional arguments, in a call to a variadic function the default argument promotions are performed on the optional argument values. This means the objects of type char or short int (whether signed or not) are promoted to either int or unsigned int, as appropriate; and that objects of type float are promoted to type double. So, if the caller passes a char as an optional argument, it is promoted to an int, and the function can access it with va_arg (ap, int).

Definition at line 741 of file printf.c.

Referenced by fprintf(), snprintf(), tft_printf(), uart0_printf(), vsnprintf(), and vsock_printf().

MEMSPACE void _putc_buffer_fn ( struct _printf_t p,
char  ch 
)

_putc_buffer_fn - character output to a string buffer Used by snprintf and vsnprintf You can make _printf_fn call this helper for each character

Parameters
[in]*pstructure with pointers and buffer to be written to
[in]chcharacter to place in buffer
Returns
void

Definition at line 1103 of file printf.c.

Referenced by snprintf(), and vsnprintf().

MEMSPACE void _puts_pad ( printf_t fn,
char *  s,
int  width,
int  count,
int  left 
)

Definition at line 683 of file printf.c.

Referenced by _printf_fn().

MEMSPACE int atodigit ( int  c,
int  radix 
)

Convert ASCII character to radix based digit , or -1.

Parameters
[in]ccharacter
[in]radixradix
Returns
radix based digit , or -1
See also
strtol

Definition at line 58 of file mathio.c.

Referenced by process_args(), strtol(), and strtoll().

MEMSPACE double atof ( const char *  str)

Referenced by atol().

MEMSPACE long atoh ( const char *  p)

Convert ASCII hex string to long integer.

Parameters
[in]pString to convert.
Returns
long integer result.
See also
strtol() for return overflow and underflow results.

Definition at line 79 of file mathio.c.

MEMSPACE int atoi ( const char *  str)

Convert ASCII string to number in base 10.

Parameters
[in]strstring
Returns
int value
See also
strtol

Definition at line 281 of file mathio.c.

MEMSPACE long atol ( const char *  str)

Convert ASCII string to number in base 10.

Parameters
[in]strstring
Returns
long value
See also
strtol

Definition at line 295 of file mathio.c.

Referenced by fatfs_tests().

MEMSPACE long aton ( char *  str,
int  base 
)

Convert ASCII string to number in a given base.

Parameters
[in]strstring
[in]baseradix
Returns
long value
See also
strtol

Definition at line 93 of file mathio.c.

MEMSPACE int bin2num ( uint8_t str,
int  strmax,
int  nummin,
int  base,
uint8_t nump,
int  numsize,
int  sign_ch 
)

Convert an unsigned number (numsize bytes in size) to ASCII in specified base Notes: No limit except available memory on number size.

  • Does not use divide or of multiply instructions - great on 8bit CPU's How it works: Say you have a table of powers of 2 in a given base base
  • To get the result add each table entry, in the base, that corresponds to each 1 bit in the binary number.
  • Now instead of a table we can start with 1 and multiple by 2 contraining each digit into the base we want - this is the same as using the table except we build it on the fly
    Parameters
    [out]strASCII number string result
    [in]strmaxmaximum size of str in bytes
    [in]numminminimum number of digits to display
    [in]*numpPointer to binary number
    [in]numsizesize of binary number in bytes
    [in]sign_chsign of binary number return converted number string numsize in bytes

Definition at line 139 of file printf.c.

Referenced by p_ntoa().

MEMSPACE double iexp ( double  num,
int  exp 
)

Referenced by atol().

MEMSPACE int WEAK_ATR isdigit ( int  c)

test if a character is a digit

Parameters
[in]ccharacter
Returns
true or false

Definition at line 48 of file stringsup.c.

Referenced by _printf_fn(), and atol().

MEMSPACE int mul10str ( uint8_t str,
int  size 
)

Fast multiply number of any size by 10.

Parameters
[in]strstring
[in]sizestring size
Returns
non-zero on overflow , 0 if ok

Definition at line 108 of file mathio.c.

MEMSPACE int p_etoa ( double  val,
char *  str,
int  max,
int  width,
int  prec,
f_t  f 
)

Referenced by _printf_fn(), and p_ntoa().

MEMSPACE int p_ftoa ( double  val,
char *  str,
int  max,
int  width,
int  prec,
f_t  f 
)

Referenced by _printf_fn(), and p_ntoa().

MEMSPACE int p_ntoa ( uint8_t nump,
int  numsize,
char *  str,
int  strmax,
int  radix,
int  width,
int  prec,
f_t  f 
)

Convert number an base 2 .. 16 to ASCII with optional sign Notes: 1) Numbers can be any number of digits long - limited only by memory available To print negative numbers convert to positive before calling this function and set f.b.neg 2) Warning: as with printf width and prec are only minimum sizes - results can be bigger We assume all numbers are positive:

Parameters
[in]numpnumber pointer
[in]numsizenumber size in bytes
[out]*strstring result
[in]strmaxstrmaximum length of string result
[in]radixRadix may be 2 .. 16
[in]widthWidth of result padded if needed
[in]precminumum number of digits, zero padded if needed
[in]fflags f.b.left justify left f.b.plus display + for positive number, - for negative numbers f.b.space display ' ' for positive, - for negative f.b.zero pad with zeros if needed f.b.alt Alternate display form - work in progress f.b.width Width of result - pad if required f.b.prec Zero padd to prec if sepcified f.b.neg Sign of number is negative

Definition at line 314 of file printf.c.

Referenced by _printf_fn().

MEMSPACE int pch ( char  ch)

Put character in buffer with limits.

Parameters
[in]strstring
[in]maxmaximum number of characters plus EOS init_p_ch
Returns
void

Definition at line 237 of file printf.c.

Referenced by p_ntoa().

MEMSPACE int pch_ind ( void  )

Return current index of character buffer with limits.

Returns
Buffer index

Definition at line 250 of file printf.c.

Referenced by p_ntoa().

MEMSPACE void pch_init ( char *  str,
int  max 
)

Initialize character buffer with limits.

Parameters
[in]strstring
[in]maxmaximum number of characters plus EOS
Returns
void

base of string to write to

current string index

maximum string size including EOS

Definition at line 222 of file printf.c.

Referenced by p_ntoa().

MEMSPACE int pch_max_ind ( void  )

Return maximum valid index for character buffer.

Returns
Buffer index

Definition at line 258 of file printf.c.

MEMSPACE void print_flags ( f_t  f)

print flags set in t_t structure

Parameters
[in]ff_t flags structure
Returns
void

Definition at line 270 of file printf.c.

MEMSPACE int printf ( const char *  format,
  ... 
)

Referenced by abort(), arg_name(), arg_value(), buffer_read_open(), cal_dex_dump_hex(), calloc(), cat(), clock_elapsed_end(), copy(), cordic_quad(), create_connection(), delete_connection(), DeleteRowCol(), Determinant(), display_clock(), display_ipv4(), display_ts(), dump_stat(), fatfs_filinfo_list(), fatfs_help(), fatfs_ls(), fatfs_status(), fatfs_tests(), fgetc(), find_connection(), font_attr(), fputc(), free(), hexdump(), html_msg(), html_status(), http_value(), init_done(), init_timers(), initialize_clock(), Invert(), led_off(), led_on(), logfile(), loop(), loop_task(), ls(), ls_info(), lseek(), main(), malloc(), MatAlloc(), MATCHARGS(), MatFree(), MatMul(), MatPrint(), MatRead(), MatWrite(), mkfs(), mmc_init(), mmc_install_timer(), mmc_test(), mmc_test_timeout(), ms_init(), next_arg(), normalize(), ntp_setup(), parse_http_request(), perror(), posix_help(), posix_tests(), print_dst(), print_dst_gmt(), print_flags(), PrintRam(), PrintXYZ(), process_args(), put_rc(), rtc_init(), rtc_read(), rtc_write(), rwbuf_create(), safecalloc(), safefree(), select(), send_message(), sep(), servertest_setup(), set_timers(), setdate(), setdate_r(), setup(), setup_networking(), snprintf(), spi_begin(), spi_end(), sum(), tcp_accept(), test_types(), testpage(), tft_FillPolyLine(), tft_floodline(), tft_push_xy(), timetests(), tm2epoch(), upload(), usage(), user_help(), user_init(), user_tasks(), user_tests(), wait_send(), web_data_connect_callback(), web_data_disconnect_callback(), web_data_error_callback(), web_data_receive_callback(), web_data_sent_callback(), web_sep(), wifi_event_cb(), and write_buffer().

int putchar ( int  c)

put a character to stdout See fdevopen() sets stream->put get for TTY devices

  • man page putchar (3).
Parameters
[in]streamPOSIX stream pointer.
Returns
character.

Definition at line 351 of file posix.c.

Referenced by fatfs_ls(), hexdump(), snprintf(), and write_buffer().

MEMSPACE void WEAK_ATR reverse ( char *  str)

Reverse a string in place Example: abcdef -> fedcba.

Parameters
[in]strstring
Returns
string length

Definition at line 231 of file stringsup.c.

Referenced by bin2num().

MEMSPACE double scale10 ( double  num,
int *  exp 
)

Referenced by atol().

MEMSPACE int snprintf ( char *  str,
size_t  size,
const char *  format,
  ... 
)

snprintf function

Parameters
[out]strstring buffer for result
[in]sizemaximum length of converted string
[in]formatprintf forat string
[in]...list of arguments
Returns
string size

Definition at line 1164 of file printf.c.

int sscanf ( const char *  strp,
const char *  fmt,
  ... 
)

Referenced by main(), MatRead(), ReadBdf(), and setdate_r().

MEMSPACE size_t WEAK_ATR strlen ( const char *  str)
MEMSPACE double strtod ( const char *  nptr,
char **  endptr 
)

Referenced by atol(), and MatRead().

MEMSPACE long strtol ( const char *  nptr,
char **  endptr,
int  base 
)

Convert ASCII string to number in a given base.

Parameters
[in]nptrstring
[in]endptrpointer to string pointer return value
[in]baseradix
Returns
long value

Definition at line 136 of file mathio.c.

Referenced by atoh(), atoi(), atol(), aton(), get_value(), main(), posix_tests(), and setdate_r().

MEMSPACE long long strtoll ( const char *  nptr,
char **  endptr,
int  base 
)

Convert ASCII string to number in a given base.

Parameters
[in]nptrstring
[in]endptrpointer to string pointer return value
[in]baseradix
Returns
long long

Definition at line 183 of file mathio.c.

MEMSPACE void WEAK_ATR strupper ( char *  str)

UPPERCASE a string.

Parameters
[in]strstring
Returns
void

Definition at line 253 of file stringsup.c.

Referenced by _printf_fn().

MEMSPACE int vsnprintf ( char *  str,
size_t  size,
const char *  format,
va_list  va 
)

vsnprintf function

Parameters
[out]strstring buffer for result
[in]sizemaximum length of converted string
[in]formatprintf forat string
[in]vava_list list of arguments
Returns
string size

Definition at line 1135 of file printf.c.

Referenced by html_msg(), and snprintf().