Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers util.c Source File

util.c

00001 // convert character to an int
00002 //
00003 int ctoi(char c)
00004 {
00005   int i=-1;
00006   
00007   if (c >= '0' && c <= '9') {
00008     i = c - '0';
00009   }
00010 
00011   //printf("char: %c  int %d\n", c, i); 
00012  
00013   return i;
00014 }
00015 
00016 
00017 // convert string to floating point
00018 //
00019 double cvstof(char *s)
00020 {
00021   double f=0.0;
00022   double mult = 0.1;
00023   bool neg = false;
00024   //char dec = 1;
00025   
00026   // leading spaces
00027   while (*s == ' ' || *s == '\t') {
00028     s++;
00029     if (*s == 0) break;
00030   }
00031 
00032   // What about negative numbers?
00033   if (*s == '-') {
00034     neg = true;
00035     s++;
00036   }
00037 
00038   // before the decimal
00039   //
00040   while (*s != 0) {
00041     if (*s == '.') {
00042       s++;
00043       break;
00044     }
00045     f = (f * 10.0) + (double) ctoi(*s);
00046     s++;
00047   }
00048   // after the decimal
00049   while (*s != 0 && *s >= '0' && *s <= '9') {
00050     f += (double) ctoi(*s) * mult;
00051     mult /= 10;
00052     s++;
00053   }
00054   
00055   // if we were negative...
00056   if (neg) f = -f;
00057   
00058   return f;
00059 }
00060 
00061 // copy t to s until delimiter is reached
00062 // return location of delimiter+1 in t
00063 // if s or t null, return null
00064 char *split(char *s, char *t, int max, char delim)
00065 {
00066   int i = 0;
00067   
00068   if (s == 0 || t == 0)
00069     return 0;
00070 
00071   while (*t != 0 && *t != '\n' && *t != delim && i < max) {
00072     *s++ = *t++;
00073     i++;
00074   }
00075   *s = 0;
00076     
00077   return t+1;
00078 }