PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PokittoItoa.cpp Source File

PokittoItoa.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     PokittoItoa.cpp
00004     @author   Jonne Valola
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2016, Jonne Valola
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 #include "PokittoItoa.h "
00038 #include <string.h>
00039 #include <stdint.h>
00040 
00041 
00042 void pokItoa(uint16_t value, char *str, int base)
00043 {
00044     /** pokItoa:  convert value n to characters
00045      * Needed to emulate AVR LibC non-standard functions, this is a very fast version of itoa
00046      *
00047      * @param value unsigned integer to be converted
00048      * @param *str pointer to string where function stores the result as characters
00049      * @param base (not implemented in this function, base is always 10)
00050      */
00051 
00052     char *p = str;
00053     uint8_t v;
00054     if (value > 499)
00055     {
00056         if (value > 799)
00057         {
00058             if (value > 999)
00059             {
00060                 // Treated as a special case as there
00061                 // are only 24 values above 999
00062                 *p++ = '1';
00063                 *p++ = '0';
00064                 uint8_t v = value - 1000;
00065 
00066                 if      (v > 19) { *p++ = '2'; v -= 20; }
00067                 else if (v >  9) { *p++ = '1'; v -= 10; }
00068                 else             { *p++ = '0'; }
00069 
00070                 *p++ = v + '0';
00071                 *p = 0;
00072                 return;
00073             }
00074 
00075             if (value > 899) { *p++ = '9'; v = value - 900; }
00076             else             { *p++ = '8'; v = value - 800; }
00077         }
00078         else
00079         {
00080             if      (value > 699) { *p++ = '7'; v = value - 700; }
00081             else if (value > 599) { *p++ = '6'; v = value - 600; }
00082             else                  { *p++ = '5'; v = value - 500; }
00083         }
00084     }
00085     else
00086     {
00087         if (value > 299)
00088         {
00089             if (value > 399) { *p++ = '4'; v = value - 400; }
00090             else             { *p++ = '3'; v = value - 300; }
00091         }
00092         else
00093         {
00094             if      (value > 199) { *p++ = '2'; v = value - 200; }
00095             else if (value >  99) { *p++ = '1'; v = value - 100; }
00096             else                  {             v = value; }
00097         }
00098     }
00099 
00100     if (v > 49)
00101     {
00102         if (v > 69)
00103         {
00104             if      (v > 89) { *p++ = '9'; v -= 90; }
00105             else if (v > 79) { *p++ = '8'; v -= 80; }
00106             else             { *p++ = '7'; v -= 70; }
00107         }
00108         else
00109         {
00110             if (v > 59) { *p++ = '6'; v -= 60; }
00111             else        { *p++ = '5'; v -= 50; }
00112         }
00113     }
00114     else
00115     {
00116         if (v > 19)
00117         {
00118             if      (v > 39) { *p++ = '4'; v -= 40; }
00119             else if (v > 29) { *p++ = '3'; v -= 30; }
00120             else             { *p++ = '2'; v -= 20; }
00121         }
00122         else
00123         {
00124             if      (v >  9)   { *p++ = '1'; v -= 10; }
00125             else if (p != str) { *p++ = '0'; }
00126         }
00127     }
00128 
00129     *p++ = v + '0';
00130     *p = 0;
00131 }
00132 
00133 void pokUtoa(uint16_t value, char *str, int base) {
00134     pokItoa(value,str,base);
00135 }
00136 
00137 void pokLtoa(long value, char *str, int base) {
00138     pokItoa(value,str,base);
00139 }
00140