Peter Cooper / Mbed 2 deprecated Dome

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers useful.c Source File

useful.c

00001 #include "mbed.h"
00002 #include <stdarg.h>
00003 #include "main.h"
00004 #include "serial.h"
00005 #include "local_defines.h"
00006 #include "useful.h"
00007 
00008 extern int var[MAX_VAR];
00009 extern int sys_state;
00010 
00011 /* Function: hexview
00012  *  Prints an array of char to stdout in hex.
00013  *  The data is grouped in two 8 byte groups per line.
00014  *  Each byte is displayed as 2 hex digits and every 
00015  *  line starts with the address of the first byte.
00016  *
00017  *  There is no text view of a line.
00018  *
00019  * Variables:
00020  *  buffer - The array to display.
00021  *  size - The length of buffer.
00022  * 
00023  * Author: rmeyer
00024  */
00025 void hexview(char *buffer, unsigned int size) {
00026     printf("\n");
00027     for(int i = 0; i < size; ++i) {
00028         if((i%16)!=0) {
00029             printf(" ");
00030         } else {
00031             printf("%04X:  ", (i));
00032         }
00033         printf("%02hhx", buffer[i]);
00034         if((i%16) ==  7) { 
00035             printf(" ");
00036         }
00037         if((i%16) == 15) {
00038             printf("\n\r");
00039         }
00040     }
00041     printf("\n\r\n\r\n\r");
00042 }
00043 
00044 /******************************************/
00045 /*                                        */
00046 /* Lprintf, Bring the printf under our    */
00047 /* control, so we can use either rs232    */
00048 /* or the USB-Serial connections as an    */
00049 /* I/O channel.                           */
00050 /*                                        */
00051 /******************************************/
00052 
00053 void lprintf(const char* fmt, ...)
00054 {
00055     char    str[0x1000];
00056     
00057     va_list ap;
00058     va_start(ap, fmt); 
00059     vsprintf(str, fmt, ap);   
00060     if (sys_state & TO_RS232){
00061         rs232_output_string(str);
00062     } else if (sys_state & TO_USB){
00063         pc_output_string(str);           
00064     }
00065     va_end(ap);
00066 }
00067 
00068 /******************************************/
00069 /*                                        */
00070 /* Read a value in from a user, workout   */
00071 /* if its hex or decimal, and return an   */
00072 /* int to the user                        */
00073 /*                                        */
00074 /******************************************/
00075 int htoi(char *str)
00076 {
00077     if(strlen(str)>=2){
00078         if(str[0]=='v' || str[0]=='V'){
00079             return(var[return_var(str)]);
00080         } else if(str[1]=='x' || str[1]=='X'){
00081             return(strtol(str, NULL, 16));
00082         }        
00083     }
00084     return(atoi(str));
00085 }
00086 
00087 /******************************************/
00088 /*                                        */
00089 /* Given the input string, return the     */
00090 /* number of the variable the user wants  */
00091 /* its added to htoi, so all inputs       */
00092 /* should get access to the variable data */
00093 /*                                        */
00094 /******************************************/
00095 
00096 int return_var(char *str)
00097 {
00098     char    buf[0x4];
00099     int     a;
00100     
00101     buf[0] = str[1];
00102     buf[1] = str[2];
00103     buf[2] = str[3];
00104     buf[3] = str[4];
00105     a = atoi(buf);
00106     if(a > MAX_VAR){
00107         lprintf("In Return_var, var number %d more than MAXVAR %d, returned -1\n",
00108             a,MAX_VAR);
00109         return(-1);
00110     }
00111     return(a);
00112 }