initial commit

This commit is contained in:
proddy
2020-07-05 18:29:08 +02:00
parent 26b201ea2f
commit c5933e8c14
739 changed files with 86566 additions and 20952 deletions

View File

@@ -0,0 +1,19 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/common.h>

View File

@@ -0,0 +1,65 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/common.h>
#include <Arduino.h>
namespace uuid {
#define UPTIME_OVERFLOW 4294967295 // Uptime overflow value
// returns system uptime in seconds
uint32_t get_uptime_sec() {
static uint32_t last_uptime = 0;
static uint8_t uptime_overflows = 0;
if (millis() < last_uptime) {
++uptime_overflows;
}
last_uptime = millis();
uint32_t uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
return uptime_seconds;
}
uint64_t get_uptime_ms() {
static uint32_t high_millis = 0;
static uint32_t low_millis = 0;
if (get_uptime() < low_millis) {
high_millis++;
}
low_millis = get_uptime();
return ((uint64_t)high_millis << 32) | low_millis;
}
// added by proddy
static uint32_t now_millis;
void set_uptime() {
now_millis = ::millis();
}
uint32_t get_uptime() {
return now_millis;
}
} // namespace uuid

View File

@@ -0,0 +1,28 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/common.h>
namespace uuid {
void loop() {
set_uptime(); // added by proddy
get_uptime_ms();
}
} // namespace uuid

View File

@@ -0,0 +1,62 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/common.h>
#include <Arduino.h>
#include <string>
namespace uuid {
class PrintableString : public ::Print {
public:
explicit PrintableString(std::string & output)
: output_(output) {
}
~PrintableString() = default;
size_t write(uint8_t data) final override {
output_.append(1, reinterpret_cast<unsigned char>(data));
return 1;
}
size_t write(const uint8_t * buffer, size_t size) final override {
output_.append(reinterpret_cast<const char *>(buffer), size);
return size;
}
private:
std::string & output_;
};
size_t print_to_string(const Printable & printable, std::string & output) {
PrintableString pstr{output};
return printable.printTo(pstr);
}
std::string printable_to_string(const Printable & printable) {
std::string str;
print_to_string(printable, str);
return str;
}
} // namespace uuid

View File

@@ -0,0 +1,35 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uuid/common.h>
#include <Arduino.h>
#include <string>
namespace uuid {
std::string read_flash_string(const __FlashStringHelper * flash_str) {
std::string str(::strlen_P(reinterpret_cast<PGM_P>(flash_str)), '\0');
::strncpy_P(&str[0], reinterpret_cast<PGM_P>(flash_str), str.capacity() + 1);
return str;
}
} // namespace uuid

View File

@@ -0,0 +1,96 @@
/*
* uuid-common - Microcontroller common utilities
* Copyright 2019 Simon Arlott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UUID_COMMON_H_
#define UUID_COMMON_H_
#include <Arduino.h>
#include <string>
#include <vector>
/**
* Common utilities.
*
* - <a href="https://github.com/nomis/mcu-uuid-common/">Git Repository</a>
* - <a href="https://mcu-uuid-common.readthedocs.io/">Documentation</a>
*/
namespace uuid {
/**
* Read a string from flash and convert it to a std::string.
*
* The flash string must be stored with appropriate alignment for
* reading it on the platform.
*
* @param[in] flash_str Pointer to string stored in flash.
* @return A string copy of the flash string.
* @since 1.0.0
*/
std::string read_flash_string(const __FlashStringHelper * flash_str);
/**
* Append to a std::string by printing a Printable object.
*
* @param[in] printable Printable object.
* @param[in,out] output String to append to.
* @return The number of bytes that were written.
* @since 1.1.0
*/
size_t print_to_string(const Printable & printable, std::string & output);
/**
* Create a std::string from a Printable object.
*
* @param[in] printable Printable object.
* @return A string containing the printed output.
* @since 1.1.0
*/
std::string printable_to_string(const Printable & printable);
/**
* Type definition for a std::vector of flash strings.
*
* @since 1.0.0
*/
using flash_string_vector = std::vector<const __FlashStringHelper *>;
/**
* Loop function that must be called regularly to detect a 32-bit
* uptime overflow.
*
* @since 1.0.0
*/
void loop();
/**
* Get the current uptime as a 64-bit milliseconds value.
*
* @return The current uptime in milliseconds.
* @since 1.0.0
*/
uint64_t get_uptime_ms();
uint32_t get_uptime(); // added by proddy
uint32_t get_uptime_sec(); // added by proddy
void set_uptime();
} // namespace uuid
#endif