Llosa de Viango Tollos – tollosUtil.c

Introduction

Features

Getting started

Download


Libraries

  Tollos libraries

  Device libraries

    boards

    microcontrollers

    peripherals


Background

  Sample application

  Troubleshooting

tollosUtil.c – useful macros and functions for Tollos

The macros are defined in tollosUtil.h, and are:

abs(x) – return the absolute value of x

deg2rad(d) – degrees to radians

iswap(x,y) – swap the values of the ints x and y using XOR

max(a,b) – return the larger of x and y

min(a,b) – return the smaller of x and y

rad2deg(r) – radians to degrees

signum(x) – return the sign of x (−1, 0, or 1)

The functions have various purposes.
Functions
 byte2hex   convert a byte to two hex chars
 fastrand   return a random number in the range 0 to n−1
 hex2byte   convert two hexadecimal characters to a byte
 isqrtg   return the square root of an int, with guess
 i2str   layout an integer as a string
 nop   do nothing with an int
 streq   caseless string compare
 strf   a reentrant mini printf to send a formatted string

byte2hex – convert a byte to two hex chars

void byte2hex(byte b, char *hex, flag upper);

b – byte to convert

hex – points to where ASCII bytes should go

upper – 1 to use ABCDEF, 0 for abcdef

This writes only the two characters; no terminating zero char is added. No error is possible.

fastrand – return a random number in the range 0 to n−1

int fastrand(uint n, uint *seed);

n – defines the random number range

seed – static or persistent seed

returns the pseudo-random number

n should be <30,000,000; the seed is provided by the caller initially but should not then be altered by the caller.

This uses the conventional X(n+1)=mod((A*X(n) + C),M) as used in the Rexx function package, but without some refinements.

The values for A and M are from the table in D.E.Knuth’s book, “Seminumerical Algorithms”, Addison-Wesley, 1981, page 102 (section 3.3.2). These particular values are attributed to Lavaux and Janssens.

  • C is chosen as 1
  • M is 2**32
  • A is 1664525 (RNDFACT)

hex2byte – convert two hexadecimal characters to a byte

int hex2byte(char *hex, byte *b);

hex – points to two ASCII bytes

b – points to byte which will receive the converted hexadecimal; set to 0x00 if error

returns 0 if OK, −1 if error

This accepts both uppercase and lowercase A->F and a->f.

isqrtg – return the square root of an int, with guess

int isqrtg(int num, int guess);

num – the number to square root

guess – is the number to converge from (e.g., last similar result); if < 1, isqrtg will make the first guess

returns the integer square root of num, such that result*result < num; if num is less than zero then 0 is returned

Note: the maximum input value is 2**31−1 (2147483647) hence the maximum possible returned value is 46340.

i2str – layout an integer as a string

int i2str(char *string, int num, int len);

string – where to layout the integer

num – the integer to lay out (not 0x80000000)

len – width of the field (string) to lay out into

returns 0 if OK, 1 if overflow (does not fit)

This writes num right-aligned and signed in the string field. No terminating zero char is written.

nop – do nothing with an int

int nop(int i);

i – an integer

returns i

This is used for placating compilers when parameters are intentionally or temporarily unused.

streq – caseless string compare

int streq(const char *char1, const char *char2);

char1 – first string to compare

char2 – second string

returns 1 if the strings caseless-compare equal, 0 otherwise

If both strings are empty they are equal.

strf – a reentrant mini printf to send a formatted string

int strf(putter putC, const char *format, va_list args);

putC – the function to which to send formatted characters

format – format and data string

args – list of other arguments as required

returns the number (count) of characters sent

The following format indicators are supported:

%% – ‘%’

%c – single char

%d, i – signed int

%m – minidec decimal number [iff MINIDEC is defined]

%s – string

%u – unsigned int

%x, X – hexadecimal

%\n – LF only (otherwise \n becomes CRLF)

The five printf format flags (+ - 0 # and space) are supported for all formats as in C99 (e.g., # only affects ‘x’, here).

The ‘h’, ‘l’, and ‘L’ modifiers are not supported.

Field width (e.g., %10i, %08x) is supported for all formats.

Field precision (e.g., %5.7i) is not supported; for decimals, use Quantize on a number to set its ‘precision’.

A \n in the string is expanded to \r\n unless preceded by %.

Almost no error checking is carried out, so, for example, the flags and field width can be used even if the format character is not one of those listed; the format character then appears in the output.

Tollos and these web pages were written by Mike Cowlishaw; Please send me any corrections, suggestions, etc.
All content © Mike Cowlishaw, 2010–2012, except where marked otherwise. All rights reserved. The pages here are for non-commercial use only (see the separate licence for Tollos source code). Privacy policy: the Speleotrove website records no personal information and sets no ‘cookies’. However, statistics, etc. might be recorded by the web hosting service.

This page was last updated on 2011-11-18 by c2wiki.