mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitCompat.cpp Source File

MicroBitCompat.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 /**
00027   * This file contains functions used to maintain compatability and portability.
00028   * It also contains constants that are used elsewhere in the DAL.
00029   */
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitCompat.h"
00032 #include "ErrorNo.h"
00033 
00034 
00035 /**
00036   * Performs an in buffer reverse of a given char array.
00037   *
00038   * @param s the string to reverse.
00039   *
00040   * @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
00041   */
00042 int string_reverse(char *s)
00043 {
00044     //sanity check...
00045     if(s == NULL)
00046         return MICROBIT_INVALID_PARAMETER;
00047 
00048     char *j;
00049     int c;
00050 
00051     j = s + strlen(s) - 1;
00052 
00053     while(s < j)
00054     {
00055         c = *s;
00056         *s++ = *j;
00057         *j-- = c;
00058     }
00059 
00060     return MICROBIT_OK;
00061 }
00062 
00063 /**
00064   * Converts a given integer into a string representation.
00065   *
00066   * @param n The number to convert.
00067   *
00068   * @param s A pointer to the buffer where the resulting string will be stored.
00069   *
00070   * @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
00071   */
00072 int itoa(int n, char *s)
00073 {
00074     int i = 0;
00075     int positive = (n >= 0);
00076 
00077     if (s == NULL)
00078         return MICROBIT_INVALID_PARAMETER;
00079 
00080     // Record the sign of the number,
00081     // Ensure our working value is positive.
00082     if (positive)
00083         n = -n;
00084 
00085     // Calculate each character, starting with the LSB.
00086     do {
00087          s[i++] = abs(n % 10) + '0';
00088     } while (abs(n /= 10) > 0);
00089 
00090     // Add a negative sign as needed
00091     if (!positive)
00092         s[i++] = '-';
00093 
00094     // Terminate the string.
00095     s[i] = '\0';
00096 
00097     // Flip the order.
00098     string_reverse(s);
00099 
00100     return MICROBIT_OK;
00101 }