/*
* util.cpp - a random assortment of useful functions
* This file is part of the TinyG2 project
*
* Copyright (c) 2010 - 2013 Alden S. Hart Jr.
*
* This file ("the software") is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by the
* Free Software Foundation. You should have received a copy of the GNU General Public
* License, version 2 along with the software. If not, see .
*
* As a special exception, you may use this file as part of a software library without
* restriction. Specifically, if other files instantiate templates or use macros or
* inline functions from this file, or you compile this file and link it with other
* files to produce an executable, this file does not by itself cause the resulting
* executable to be covered by the GNU General Public License. This exception does not
* however invalidate any other reasons why the executable file might be covered by the
* GNU General Public License.
*
* THE SOFTWARE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* util contains a dog's breakfast of supporting functions that are
* not specific to tinyg: including:
* - math and min/max utilities and extensions
* - vector manipulation utilities
*/
#include "tinyg2.h"
#include "util.h"
#ifdef __cplusplus
extern "C"{
#endif
/**** Vector utilities ****
* copy_vector() - copy vector of arbitrary length
* copy_axis_vector() - copy an axis vector
* get_axis_vector_length() - return the length of an axis vector
* set_vector() - load values into vector form
* set_vector_by_axis() - load a single value into a zero vector
*/
float vector[AXES]; // statically allocated global for vector utilities
/*
void copy_vector(float dst[], const float src[], uint8_t length)
{
for (uint8_t i=0; i max) { max = x2;}
if (x3 > max) { return (x3);}
return (max);
}
float max4(float x1, float x2, float x3, float x4)
{
float max = x1;
if (x2 > max) { max = x2;}
if (x3 > max) { max = x3;}
if (x4 > max) { return (x4);}
return (max);
}
/**** String utilities ****
* strcpy_U() - strcpy workalike to get around initial NUL for blank string - possibly wrong
* isnumber() - isdigit that also accepts plus, minus, and decimal point
* escape_string() - add escapes to a string - currently for quotes only
* read_float() - read a float from a normalized char array
*/
/*
#ifdef __ARM
uint8_t * strcpy_U( uint8_t * dst, const uint8_t * src )
{
uint16_t index = 0;
do {
dst[index] = src[index];
} while (src[index++] != 0);
return dst;
}
#endif
*/
uint8_t isnumber(char_t c)
{
if (c == '.') { return (true); }
if (c == '-') { return (true); }
if (c == '+') { return (true); }
return (isdigit(c));
}
char_t *escape_string(char_t *dst, char_t *src)
{
char_t c;
char_t *start_dst = dst;
while ((c = *(src++)) != 0) { // NUL
if (c == '"') { *(dst++) = '\\'; }
*(dst++) = c;
}
return (start_dst);
}
/*
* read_float() - read a float from a normalized char array
*
* buf normalized char array (line)
* i char array index must point to start of number
* float_ptr pointer to float to write value into
*
* The line is normalized when it is all caps, has no white space,
* no non-alphnumeric characters, and no newline or CR.
*/
/*
uint8_t read_float(char *buf, uint8_t *i, float *float_ptr)
{
char *start = buf + *i;
char *end;
*float_ptr = strtof(start, &end);
if(end == start) {
return(false);
}
*i = (uint8_t)(end - buf);
return(true);
}
*/
/*
* compute_checksum() - calculate the checksum for a string
*
* Stops calculation on null termination or length value if non-zero.
*
* This is based on the the Java hashCode function.
* See http://en.wikipedia.org/wiki/Java_hashCode()
*/
#define HASHMASK 9999
uint16_t compute_checksum(char_t const *string, const uint16_t length)
{
uint32_t h = 0;
uint16_t len = strlen(string);
if (length != 0) {
len = min(len, length);
}
for (uint16_t i=0; i