AVR Z-LINKŪ


utilities.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00024 /* === Includes ============================================================ */
00025 #include <stdint.h>
00026 #include <stdbool.h>
00027 
00028 #include "compiler.h"
00029 #include "utilities.h"
00030 /* ==== Macros ============================================================= */
00031 /* === Typedefs ============================================================ */
00032 /* === Variables =========================================================== */
00033 /* === Prototypes ========================================================== */
00034 /* === Implementation ====================================================== */
00035 
00036 
00047 bool stringCompare( const uint8_t *str, const uint8_t __flash *fstr ){
00048         
00049         //Run until one of the strings are terminated.
00050         while( !( ( *str == END_OF_STRING ) || ( *fstr == END_OF_STRING ) ) ){
00051                 
00052                 //If they are not equal before END_OF_STRING, return false.
00053                 if( *str != *fstr ){
00054                 
00055                         break;
00056                 }
00057                 
00058                 str++;
00059                 fstr++;
00060         }
00061         
00062         //Both strings equal and of same length.
00063         if( ( ( *str == END_OF_STRING ) && ( *fstr == END_OF_STRING ) ) ){
00064         
00065                 return true;  
00066         }
00067         
00068         //Equal, unequal or of different legth.
00069         else{
00070         
00071                 return false;
00072         }
00073 }
00074 
00085 bool htoi8( uint8_t *hex, uint8_t *ret ){
00086         
00087         uint8_t i = 0x00;
00088         uint8_t radix = 1;
00089         *ret = 0x00;
00090         
00091         //Run through and check that the length is valid.
00092         while( *hex != '\0' ){
00093                 
00094                 //Return with false with the ascii char is not in 
00095                 //the [0->F] range.
00096                 if( !( ( ( *hex >= '0' ) && ( *hex <= '9' ) ) || 
00097                      ( ( *hex >= 'A' ) && ( *hex <= 'F' ) ) ) ){
00098                         
00099                         return false;  
00100                 }
00101                 
00102                 hex++;
00103                 i++;
00104         }
00105         
00106         //Only two or less digits. Valid conversion.
00107         if( i <= 0x02 ){
00108                 
00109                 hex--;
00110                 
00111                 //Do the actual conversion.
00112                 for( ; i > 0; i-- ){
00113                 
00114                         // 0->9
00115                         if( ( *hex >= '0' ) && ( *hex <= '9' ) ){
00116                 
00117                                 *ret += radix * ( *hex - '0' );  
00118                         }
00119                 
00120                         //A->F
00121                         else{
00122 
00123                                 *ret += radix * ( *hex - 'A' + 10 );                              
00124                         }
00125                 
00126                         hex--;
00127                         radix = radix << 4; // Multiply by 16.
00128                 }
00129                 
00130                 return true;  
00131         }
00132         
00133         //Return with an error.
00134         else{
00135           
00136                 return false;
00137         }  
00138 }
00139 
00150 bool htoi16( uint8_t *hex, uint16_t *ret ){
00151         
00152         uint8_t i = 0x00;
00153         uint16_t radix = 1;
00154         *ret = 0x0000;
00155         
00156         //Run through and check that the length is valid.
00157         while( *hex != '\0' ){
00158                 
00159                 //Return with false with the ascii char is not in 
00160                 //the [0->F] range.
00161                 if( !( ( ( *hex >= '0' ) && ( *hex <= '9' ) ) || 
00162                      ( ( *hex >= 'A' ) && ( *hex <= 'F' ) ) ) ){
00163                         
00164                         return false;  
00165                 }
00166                 
00167                 hex++;
00168                 i++;
00169         }
00170         
00171         //Only Four or less digits. Valid conversion.
00172         if( i <= 0x04 ){
00173                 
00174                 hex--;
00175                 
00176                 //Do the actual conversion.
00177                 for( ; i > 0; i-- ){
00178                 
00179                         // 0->9
00180                         if( ( *hex >= '0' ) && ( *hex <= '9' ) ){
00181                 
00182                                 *ret += radix * ( *hex - '0' );  
00183                         }
00184                 
00185                         //A->F
00186                         else{
00187 
00188                                 *ret += radix * ( *hex - 'A' + 10 );                              
00189                         }
00190                 
00191                         hex--;
00192                         radix = radix << 4;
00193                 }
00194                 
00195                 return true;  
00196         }
00197         
00198         //Return with an error.
00199         else{
00200           
00201                 return false;
00202         }
00203 }
@DOC_TITLE@
Generated on Sat Dec 2 16:05:51 2006 for AVR414 User's Guide - ATAVRRZ502 - Accessory Kit by doxygen 1.4.7