From dad6732f3801960a6c3d43b0b509be47ce1ba3bb Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Sat, 13 Aug 2016 04:45:08 -0300 Subject: [PATCH] Include .h instead of .cpp when possible * Move implementation code from Arduino.h --- Makefile | 2 +- MySensors.h | 12 +- core/MyGatewayTransportEthernet.cpp | 2 + drivers/Linux/Arduino.h | 180 +--------------------------- drivers/Linux/EthernetClient.cpp | 41 +++---- drivers/Linux/EthernetClient.h | 44 +++---- drivers/Linux/EthernetServer.cpp | 48 ++++---- drivers/Linux/EthernetServer.h | 49 ++++---- drivers/Linux/IPAddress.cpp | 39 +++--- drivers/Linux/IPAddress.h | 38 +++--- drivers/Linux/utility.cpp | 177 +++++++++++++++++++++++++++ 11 files changed, 327 insertions(+), 305 deletions(-) create mode 100644 drivers/Linux/utility.cpp diff --git a/Makefile b/Makefile index 3f74ade..b5ba3a8 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ CONFIG_FILE=Makefile.inc include $(CONFIG_FILE) GATEWAY=examples_RPi/MySGateway -GATEWAY_SOURCES=examples_RPi/MySGateway.cpp +GATEWAY_SOURCES=$(wildcard drivers/Linux/*.cpp) examples_RPi/MySGateway.cpp GATEWAY_OBJECTS=$(patsubst %.cpp,%.o,$(GATEWAY_SOURCES)) DEPS+=$(patsubst %.cpp,%.d,$(GATEWAY_SOURCES)) diff --git a/MySensors.h b/MySensors.h index 05d145f..b2f6890 100644 --- a/MySensors.h +++ b/MySensors.h @@ -222,9 +222,9 @@ #include "core/MyProtocolMySensors.cpp" #if defined(MY_GATEWAY_LINUX) - #include "drivers/Linux/EthernetClient.cpp" - #include "drivers/Linux/EthernetServer.cpp" - #include "drivers/Linux/IPAddress.cpp" + #include "drivers/Linux/EthernetClient.h" + #include "drivers/Linux/EthernetServer.h" + #include "drivers/Linux/IPAddress.h" #endif #include "drivers/PubSubClient/PubSubClient.cpp" #include "core/MyGatewayTransportMQTTClient.cpp" @@ -250,9 +250,9 @@ #include "core/MyGatewayTransportEthernet.cpp" #elif defined(MY_GATEWAY_LINUX) // GATEWAY - Generic Linux (RaspberryPi, BBB, ...) - #include "drivers/Linux/EthernetClient.cpp" - #include "drivers/Linux/EthernetServer.cpp" - #include "drivers/Linux/IPAddress.cpp" + #include "drivers/Linux/EthernetClient.h" + #include "drivers/Linux/EthernetServer.h" + #include "drivers/Linux/IPAddress.h" #include "core/MyGatewayTransportEthernet.cpp" #elif defined(MY_GATEWAY_W5100) // GATEWAY - W5100 diff --git a/core/MyGatewayTransportEthernet.cpp b/core/MyGatewayTransportEthernet.cpp index f974593..ee8b7a8 100644 --- a/core/MyGatewayTransportEthernet.cpp +++ b/core/MyGatewayTransportEthernet.cpp @@ -56,6 +56,8 @@ typedef struct #if defined(MY_USE_UDP) EthernetUDP _ethernetServer; +#elif defined(MY_GATEWAY_LINUX) + EthernetServer _ethernetServer(_ethernetGatewayPort, MY_GATEWAY_MAX_CLIENTS); #else EthernetServer _ethernetServer(_ethernetGatewayPort); #endif diff --git a/drivers/Linux/Arduino.h b/drivers/Linux/Arduino.h index e352dd1..39f0f6e 100644 --- a/drivers/Linux/Arduino.h +++ b/drivers/Linux/Arduino.h @@ -2,6 +2,7 @@ #define Arduino_h #include +#include #ifdef __cplusplus #include @@ -45,180 +46,11 @@ void yield(void); typedef bool boolean; typedef uint8_t byte; -/** -* C++ version 0.4 char* style "itoa": -* Written by Lukás Chmela -* Released under GPLv3. -*/ -char *itoa(int value, char* result, int base) { - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } - - char* ptr = result, *ptr1 = result, tmp_char; - int tmp_value; - - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); - - // Apply negative sign - // Apply negative sign - if (tmp_value < 0) *ptr++ = '-'; - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; -} - -/** - * C++ version 0.4 char* style "itoa": - * Written by Lukás Chmela - * Released under GPLv3. - */ -char *ltoa(long value, char* result, int base) { - // check that the base if valid - if (base < 2 || base > 36) { *result = '\0'; return result; } - - char* ptr = result, *ptr1 = result, tmp_char; - long tmp_value; - - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; - } while ( value ); - - // Apply negative sign - if (tmp_value < 0) *ptr++ = '-'; - *ptr-- = '\0'; - while(ptr1 < ptr) { - tmp_char = *ptr; - *ptr--= *ptr1; - *ptr1++ = tmp_char; - } - return result; -} - -char *ultoa(long num, char *str, int radix) -{ - unsigned long value; - char *sp = str; - char *sp2; - - value = num; - - /* Store sign at start of buffer for negative base-10 values */ - if (10 == radix && 0 > num) { - *sp++ = '-'; - value = -num; - } - - sp2 = sp; - - do { - char rem = value % radix; - value /= radix; - if (10 > rem) { - *sp++ = '0' + rem; - } else { - *sp++ = 'A' + rem - 10; - } - } while (0 < value); - - /* Mark end of string */ - *sp-- = 0; - - /* Reverse string contents (excluding sign) in place */ - while (sp2 < sp) { - char tmp = *sp2; - *sp2++ = *sp; - *sp-- = tmp; - } - - return str; -} - -/** - * Copyright (c) 2012, Peter A. Bigot - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of the software nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -char *utoa(int num, char *str, int radix) -{ - unsigned int value; - char *sp = str; - char *sp2; - - value = num; - - /* Store sign at start of buffer for negative base-10 values */ - if (10 == radix && 0 > num) { - *sp++ = '-'; - value = -num; - } - - sp2 = sp; - - do { - char rem = value % radix; - value /= radix; - if (10 > rem) { - *sp++ = '0' + rem; - } else { - *sp++ = 'A' + rem - 10; - } - } while (0 < value); - - /* Mark end of string */ - *sp-- = 0; - - /* Reverse string contents (excluding sign) in place */ - while (sp2 < sp) { - char tmp = *sp2; - *sp2++ = *sp; - *sp-- = tmp; - } - - return str; -} - -char *dtostrf(float f, int width, int decimals, char *result) -{ - sprintf(result,"%*.*f", width, decimals, f); - return result; -} +char *itoa(int value, char* result, int base); +char *ltoa(long value, char* result, int base); +char *ultoa(long num, char *str, int radix); +char *utoa(int num, char *str, int radix); +char *dtostrf(float f, int width, int decimals, char *result); #ifdef __cplusplus } diff --git a/drivers/Linux/EthernetClient.cpp b/drivers/Linux/EthernetClient.cpp index c6a9389..5e80848 100644 --- a/drivers/Linux/EthernetClient.cpp +++ b/drivers/Linux/EthernetClient.cpp @@ -1,24 +1,25 @@ /* -* The MySensors Arduino library handles the wireless radio link and protocol -* between your home built sensors/actuators and HA controller of choice. -* The sensors forms a self healing radio network with optional repeaters. Each -* repeater and gateway builds a routing tables in EEPROM which keeps track of the -* network topology allowing messages to be routed to nodes. -* -* Created by Marcelo Aquino -* Copyright (C) 2016 Marcelo Aquino -* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors -* -* Documentation: http://www.mysensors.org -* Support Forum: http://forum.mysensors.org -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* version 2 as published by the Free Software Foundation. -* -* Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. -*/ + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Marcelo Aquino + * Copyright (C) 2016 Marcelo Aquino + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. + */ +#include #include #include #include @@ -249,6 +250,6 @@ bool EthernetClient::operator==(const EthernetClient& rhs) { return _sock == rhs._sock && _sock != -1 && rhs._sock != -1; } -uint8_t EthernetClient::getSocketNumber() { +int EthernetClient::getSocketNumber() { return _sock; } diff --git a/drivers/Linux/EthernetClient.h b/drivers/Linux/EthernetClient.h index b2ad1b1..0627436 100644 --- a/drivers/Linux/EthernetClient.h +++ b/drivers/Linux/EthernetClient.h @@ -1,26 +1,26 @@ /* -* The MySensors Arduino library handles the wireless radio link and protocol -* between your home built sensors/actuators and HA controller of choice. -* The sensors forms a self healing radio network with optional repeaters. Each -* repeater and gateway builds a routing tables in EEPROM which keeps track of the -* network topology allowing messages to be routed to nodes. -* -* Created by Marcelo Aquino -* Copyright (C) 2016 Marcelo Aquino -* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors -* -* Documentation: http://www.mysensors.org -* Support Forum: http://forum.mysensors.org -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* version 2 as published by the Free Software Foundation. -* -* Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. -*/ + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Marcelo Aquino + * Copyright (C) 2016 Marcelo Aquino + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. + */ -#ifndef ethernetclient_h -#define ethernetclient_h +#ifndef EthernetClient_h +#define EthernetClient_h #include "Client.h" #include "IPAddress.h" @@ -69,7 +69,7 @@ public: virtual bool operator!=(const bool value) { return bool() != value; } virtual bool operator==(const EthernetClient&); virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }; - uint8_t getSocketNumber(); + int getSocketNumber(); friend class EthernetServer; diff --git a/drivers/Linux/EthernetServer.cpp b/drivers/Linux/EthernetServer.cpp index 506eb15..be82704 100644 --- a/drivers/Linux/EthernetServer.cpp +++ b/drivers/Linux/EthernetServer.cpp @@ -1,24 +1,25 @@ /* -* The MySensors Arduino library handles the wireless radio link and protocol -* between your home built sensors/actuators and HA controller of choice. -* The sensors forms a self healing radio network with optional repeaters. Each -* repeater and gateway builds a routing tables in EEPROM which keeps track of the -* network topology allowing messages to be routed to nodes. -* -* Created by Marcelo Aquino -* Copyright (C) 2016 Marcelo Aquino -* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors -* -* Documentation: http://www.mysensors.org -* Support Forum: http://forum.mysensors.org -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* version 2 as published by the Free Software Foundation. -* -* Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. -*/ + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Marcelo Aquino + * Copyright (C) 2016 Marcelo Aquino + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. + */ +#include #include #include #include @@ -33,6 +34,7 @@ struct server_vars { struct IPAddress address; std::list *new_clients; std::vector *clients; + uint16_t max_clients; }; static pthread_mutex_t new_clients_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -40,9 +42,9 @@ static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER; void *incoming_connections(void *); -EthernetServer::EthernetServer(uint16_t port) : port(port) +EthernetServer::EthernetServer(uint16_t port, uint16_t max_clients) : port(port), max_clients(max_clients) { - clients.reserve(MY_GATEWAY_MAX_CLIENTS); + clients.reserve(max_clients); } void EthernetServer::begin() @@ -60,6 +62,7 @@ void EthernetServer::begin(IPAddress address) vars->address = address; vars->new_clients = &new_clients; vars->clients = &clients; + vars->max_clients = max_clients; pthread_attr_init(&detached_attr); pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED); @@ -137,6 +140,7 @@ size_t EthernetServer::write(const char *str) { if (str == NULL) return 0; return write((const uint8_t *)str, strlen(str)); } + size_t EthernetServer::write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); } @@ -221,7 +225,7 @@ void *incoming_connections(void* thread_arg) pthread_mutex_lock(&clients_mutex); int connected_clients = vars->new_clients->size() + vars->clients->size(); - if (connected_clients == MY_GATEWAY_MAX_CLIENTS) { + if (connected_clients == vars->max_clients) { pthread_mutex_unlock(&clients_mutex); pthread_mutex_unlock(&new_clients_mutex); ETHERNETSERVER_DEBUG("Max number of ethernet clients reached."); diff --git a/drivers/Linux/EthernetServer.h b/drivers/Linux/EthernetServer.h index 10f1a4d..2127829 100644 --- a/drivers/Linux/EthernetServer.h +++ b/drivers/Linux/EthernetServer.h @@ -1,26 +1,26 @@ /* -* The MySensors Arduino library handles the wireless radio link and protocol -* between your home built sensors/actuators and HA controller of choice. -* The sensors forms a self healing radio network with optional repeaters. Each -* repeater and gateway builds a routing tables in EEPROM which keeps track of the -* network topology allowing messages to be routed to nodes. -* -* Created by Marcelo Aquino -* Copyright (C) 2016 Marcelo Aquino -* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors -* -* Documentation: http://www.mysensors.org -* Support Forum: http://forum.mysensors.org -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* version 2 as published by the Free Software Foundation. -* -* Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. -*/ + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Marcelo Aquino + * Copyright (C) 2016 Marcelo Aquino + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Based on Arduino ethernet library, Copyright (c) 2010 Arduino LLC. All right reserved. + */ -#ifndef ethernetserver_h -#define ethernetserver_h +#ifndef EthernetServer_h +#define EthernetServer_h #include #include @@ -31,6 +31,10 @@ #define ETHERNETSERVER_BACKLOG 10 #endif +#ifndef ETHERNETSERVER_MAX_CLIENTS + #define ETHERNETSERVER_MAX_CLIENTS ETHERNETSERVER_BACKLOG +#endif + // debug #if defined(ETHERNETSERVER_VERBOSE) #define ETHERNETSERVER_DEBUG(x,...) debug(x, ##__VA_ARGS__) @@ -46,9 +50,10 @@ private: uint16_t port; std::list new_clients; std::vector clients; + uint16_t max_clients; public: - EthernetServer(uint16_t); + EthernetServer(uint16_t port, uint16_t max_clients = ETHERNETSERVER_MAX_CLIENTS); virtual void begin(); void begin(IPAddress); void beginPacket(IPAddress, uint16_t); diff --git a/drivers/Linux/IPAddress.cpp b/drivers/Linux/IPAddress.cpp index bec1728..dd67b59 100644 --- a/drivers/Linux/IPAddress.cpp +++ b/drivers/Linux/IPAddress.cpp @@ -1,25 +1,26 @@ /* - IPAddress.cpp - Base class that provides IPAddress - Copyright (c) 2011 Adrian McEwen. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - - Modified by Marcelo Aquino for MySensors use + * IPAddress.cpp - Base class that provides IPAddress + * Copyright (c) 2011 Adrian McEwen. All right reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * + * Modified by Marcelo Aquino for MySensors use */ +#include #include #include "IPAddress.h" diff --git a/drivers/Linux/IPAddress.h b/drivers/Linux/IPAddress.h index 2fb9c6f..f4b05c0 100644 --- a/drivers/Linux/IPAddress.h +++ b/drivers/Linux/IPAddress.h @@ -1,23 +1,23 @@ /* - IPAddress.h - Base class that provides IPAddress - Copyright (c) 2011 Adrian McEwen. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - - Modified by Marcelo Aquino for MySensors use + * IPAddress.h - Base class that provides IPAddress + * Copyright (c) 2011 Adrian McEwen. All right reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * + * Modified by Marcelo Aquino for MySensors use */ #ifndef IPAddress_h diff --git a/drivers/Linux/utility.cpp b/drivers/Linux/utility.cpp new file mode 100644 index 0000000..aa36905 --- /dev/null +++ b/drivers/Linux/utility.cpp @@ -0,0 +1,177 @@ +#include +#include "Arduino.h" + +/** +* C++ version 0.4 char* style "itoa": +* Written by Lukás Chmela +* Released under GPLv3. +*/ +char *itoa(int value, char* result, int base) { + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } + + char* ptr = result, *ptr1 = result, tmp_char; + int tmp_value; + + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + // Apply negative sign + // Apply negative sign + if (tmp_value < 0) *ptr++ = '-'; + *ptr-- = '\0'; + while(ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; +} + +/** + * C++ version 0.4 char* style "itoa": + * Written by Lukás Chmela + * Released under GPLv3. + */ +char *ltoa(long value, char* result, int base) { + // check that the base if valid + if (base < 2 || base > 36) { *result = '\0'; return result; } + + char* ptr = result, *ptr1 = result, tmp_char; + long tmp_value; + + do { + tmp_value = value; + value /= base; + *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; + } while ( value ); + + // Apply negative sign + if (tmp_value < 0) *ptr++ = '-'; + *ptr-- = '\0'; + while(ptr1 < ptr) { + tmp_char = *ptr; + *ptr--= *ptr1; + *ptr1++ = tmp_char; + } + return result; +} + +char *ultoa(long num, char *str, int radix) +{ + unsigned long value; + char *sp = str; + char *sp2; + + value = num; + + /* Store sign at start of buffer for negative base-10 values */ + if (10 == radix && 0 > num) { + *sp++ = '-'; + value = -num; + } + + sp2 = sp; + + do { + char rem = value % radix; + value /= radix; + if (10 > rem) { + *sp++ = '0' + rem; + } else { + *sp++ = 'A' + rem - 10; + } + } while (0 < value); + + /* Mark end of string */ + *sp-- = 0; + + /* Reverse string contents (excluding sign) in place */ + while (sp2 < sp) { + char tmp = *sp2; + *sp2++ = *sp; + *sp-- = tmp; + } + + return str; +} + +/** + * Copyright (c) 2012, Peter A. Bigot + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of the software nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +char *utoa(int num, char *str, int radix) +{ + unsigned int value; + char *sp = str; + char *sp2; + + value = num; + + /* Store sign at start of buffer for negative base-10 values */ + if (10 == radix && 0 > num) { + *sp++ = '-'; + value = -num; + } + + sp2 = sp; + + do { + char rem = value % radix; + value /= radix; + if (10 > rem) { + *sp++ = '0' + rem; + } else { + *sp++ = 'A' + rem - 10; + } + } while (0 < value); + + /* Mark end of string */ + *sp-- = 0; + + /* Reverse string contents (excluding sign) in place */ + while (sp2 < sp) { + char tmp = *sp2; + *sp2++ = *sp; + *sp-- = tmp; + } + + return str; +} + +char *dtostrf(float f, int width, int decimals, char *result) +{ + sprintf(result,"%*.*f", width, decimals, f); + return result; +}