From da30a410a5bec9cc944addab67a3bf242c51967e Mon Sep 17 00:00:00 2001 From: riban-bw Date: Mon, 10 Dec 2018 08:51:36 +0000 Subject: [PATCH 01/16] Refactored DateTime Moved from Services to SmingCore Rationalised conversion method names to... from... Marked previous named methods as deprecated Updated dependant libraries --- .../DateTime/DateTime.cpp | 12 ++-- .../DateTime/DateTime.h | 61 +++++++++++++++++-- .../SmingCore/Network/Http/HttpConnection.cpp | 4 +- Sming/SmingCore/Network/Http/HttpConnection.h | 2 +- Sming/SmingCore/Network/NtpClient.h | 2 +- Sming/SmingCore/SmingCore.h | 2 +- Sming/SmingCore/SystemClock.h | 2 +- 7 files changed, 69 insertions(+), 16 deletions(-) rename Sming/{Services => SmingCore}/DateTime/DateTime.cpp (91%) mode change 100644 => 100755 rename Sming/{Services => SmingCore}/DateTime/DateTime.h (71%) mode change 100644 => 100755 mode change 100644 => 100755 Sming/SmingCore/Network/Http/HttpConnection.cpp mode change 100644 => 100755 Sming/SmingCore/Network/Http/HttpConnection.h mode change 100644 => 100755 Sming/SmingCore/Network/NtpClient.h mode change 100644 => 100755 Sming/SmingCore/SmingCore.h mode change 100644 => 100755 Sming/SmingCore/SystemClock.h diff --git a/Sming/Services/DateTime/DateTime.cpp b/Sming/SmingCore/DateTime/DateTime.cpp old mode 100644 new mode 100755 similarity index 91% rename from Sming/Services/DateTime/DateTime.cpp rename to Sming/SmingCore/DateTime/DateTime.cpp index 5f96ae7285..70e847e696 --- a/Sming/Services/DateTime/DateTime.cpp +++ b/Sming/SmingCore/DateTime/DateTime.cpp @@ -14,7 +14,7 @@ #define LEAP_YEAR(year) ((year % 4) == 0) /* - * Used to parse HTTP date strings - see parseHttpDate() + * Used to parse HTTP date strings - see fromHttpDate() */ static DEFINE_FSTR(flashMonthNames, "Jan\0Feb\0Mar\0Apr\0May\0Jun\0Jul\0Aug\0Sep\0Oct\0Nov\0Dec"); static DEFINE_FSTR(flashDayNames, "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat"); @@ -41,7 +41,7 @@ DateTime::DateTime(time_t time) void DateTime::setTime(time_t time) { - convertFromUnixTime(time, &Second, &Minute, &Hour, &Day, &DayofWeek, &Month, &Year); + fromUnixTime(time, &Second, &Minute, &Hour, &Day, &DayofWeek, &Month, &Year); Milliseconds = 0; } @@ -62,7 +62,7 @@ bool DateTime::isNull() Milliseconds == 0; } -bool DateTime::parseHttpDate(const String& httpDate) +bool DateTime::fromHttpDate(const String& httpDate) { int first = httpDate.indexOf(','); if(first < 0 || httpDate.length() - first < 20) @@ -120,7 +120,7 @@ bool DateTime::parseHttpDate(const String& httpDate) time_t DateTime::toUnixTime() { - return convertToUnixTime(Second + (Milliseconds / 1000), Minute, Hour, Day, Month, Year); + return toUnixTime(Second + (Milliseconds / 1000), Minute, Hour, Day, Month, Year); } String DateTime::toShortDateString() @@ -171,7 +171,7 @@ void DateTime::addMilliseconds(long add) Milliseconds = ms; } -void DateTime::convertFromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday, +void DateTime::fromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday, int8_t* pmonth, int16_t* pyear) { // convert the given time_t to time components @@ -218,7 +218,7 @@ void DateTime::convertFromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int *pday = epoch + 1; // day of month } -time_t DateTime::convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) +time_t DateTime::toUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) { // converts time components to time_t // note year argument is full four digit year (or digits since 2000), i.e.1975, (year 8 is 2008) diff --git a/Sming/Services/DateTime/DateTime.h b/Sming/SmingCore/DateTime/DateTime.h old mode 100644 new mode 100755 similarity index 71% rename from Sming/Services/DateTime/DateTime.h rename to Sming/SmingCore/DateTime/DateTime.h index 19e28b2219..05ddf138ee --- a/Sming/Services/DateTime/DateTime.h +++ b/Sming/SmingCore/DateTime/DateTime.h @@ -112,14 +112,26 @@ class DateTime * @note Also supports obsolete RFC 850 date format, e.g. Sunday, 06-Nov-94 08:49:37 GMT where 2 digit year represents range 1970-2069 * @note GMT suffix is optional and is always assumed / ignored */ - bool parseHttpDate(const String& httpDate); + bool fromHttpDate(const String& httpDate); + + /** @brief Parse a HTTP full date and set time and date + * @param httpDate HTTP full date in RFC 1123 format, e.g. Sun, 06 Nov 1994 08:49:37 GMT + * @retval bool True on success + * @note Also supports obsolete RFC 850 date format, e.g. Sunday, 06-Nov-94 08:49:37 GMT where 2 digit year represents range 1970-2069 + * @note GMT suffix is optional and is always assumed / ignored + * @deprecated Use 'fromHttpDat' instead + */ + bool parseHttpDate(const String& httpDate) __attribute__ ((deprecated)) + { + return fromHttpDate(httpDate); + } /** @brief Check if time date object is initialised * @retval True if object has no value. False if initialised. */ bool isNull(); - /** @brief Get Unix time + /** @brief Get Unix time * @retval time_t Unix time, quantity of seconds since 00:00:00 1970-01-01 * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. */ @@ -173,7 +185,44 @@ class DateTime * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. * @note All of the return values are optional, specify nullptr if not required */ - static void convertFromUnixTime(time_t timep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear); + static void fromUnixTime(time_t timep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear); + + // functions to convert to and from time components (hrs, secs, days, years etc) to time_t + /** @brief Convert from Unix time to individual time components + * @param timep Unix time date value to convert + * @param psec Pointer to integer to hold resulting seconds + * @param pmin Pointer to integer to hold resulting minutes + * @param phour Pointer to integer to hold resulting hour + * @param pday Pointer to integer to hold resulting day of month + * @param pwday Pointer to integer to hold resulting day of week + * @param pmonth Pointer to integer to hold resulting month + * @param pyear Pointer to integer to hold resulting year + * @note This is a more compact version of the C library localtime function + * @note Pass the Unix timedate value and pointers to existing integers. The integers are updated with the converted values + * @note This static function may be used without instantiating a DateTime object, e.g. DateTime::convertFromUnixTime(...); + * @note 32-bit Unix time has year 2036 issue. + * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. + * @note All of the return values are optional, specify nullptr if not required + * @deprecated Use 'fromUnixTime' instead + */ + static void convertFromUnixTime(time_t tImep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear) __attribute__ ((deprecated)) + { + return fromUnixTime( tImep, psec, pmin, phour, pday, pwday, pmonth, pyear); + } + + /** @brief Convert from individual time components to Unix time + * @param sec Seconds + * @param min Minutes + * @param hour Hours + * @param day Days + * @param month Month (0-11, Jan=0, Feb=1, ...Dec=11) + * @param year Year (1901-2036), either full 4 digit year or 2 digits for 1970-2036 + * @note Seconds, minutes, hours and days may be any value, e.g. to calculate the value for 300 days since 1970 (epoch), set day=300 + * @note This static function may be used without instantiating a DateTime object, e.g. time_t unixTime = DateTime::convertToUnixTime(...); + * @note 32-bit Unix time is valid between 1901-12-13 and 03:14:07 2038-01-19 + * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. + */ + static time_t toUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year); /** @brief Convert from individual time components to Unix time * @param sec Seconds @@ -186,8 +235,12 @@ class DateTime * @note This static function may be used without instantiating a DateTime object, e.g. time_t unixTime = DateTime::convertToUnixTime(...); * @note 32-bit Unix time is valid between 1901-12-13 and 03:14:07 2038-01-19 * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. + * @deprecated Use 'toUnixTime' instead */ - static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year); + static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) __attribute__ ((deprecated)) + { + return toUnixTime(sec, min, hour, day, month, year); + } public: int8_t Hour = 0; ///< Hour (0-23) diff --git a/Sming/SmingCore/Network/Http/HttpConnection.cpp b/Sming/SmingCore/Network/Http/HttpConnection.cpp old mode 100644 new mode 100755 index c303c439bf..aaf7521b03 --- a/Sming/SmingCore/Network/Http/HttpConnection.cpp +++ b/Sming/SmingCore/Network/Http/HttpConnection.cpp @@ -89,7 +89,7 @@ DateTime HttpConnection::getLastModifiedDate() { DateTime res; String strLM = response.headers[HTTP_HEADER_LAST_MODIFIED]; - if(res.parseHttpDate(strLM)) + if(res.fromHttpDate(strLM)) return res; else return DateTime(); @@ -99,7 +99,7 @@ DateTime HttpConnection::getServerDate() { DateTime res; String strSD = response.headers[HTTP_HEADER_DATE]; - if(res.parseHttpDate(strSD)) + if(res.fromHttpDate(strSD)) return res; else return DateTime(); diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h old mode 100644 new mode 100755 index 8282e0aecc..571deaa29c --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -14,7 +14,7 @@ #define _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ #include "HttpConnectionBase.h" -#include "../Services/DateTime/DateTime.h" +#include #include "Data/ObjectQueue.h" /** @defgroup HTTP client connection diff --git a/Sming/SmingCore/Network/NtpClient.h b/Sming/SmingCore/Network/NtpClient.h old mode 100644 new mode 100755 index da7215e244..3d3b295555 --- a/Sming/SmingCore/Network/NtpClient.h +++ b/Sming/SmingCore/Network/NtpClient.h @@ -18,7 +18,7 @@ #include "UdpConnection.h" #include "Platform/System.h" #include "Timer.h" -#include "../Services/DateTime/DateTime.h" +#include #include "Delegate.h" #define NTP_PORT 123 diff --git a/Sming/SmingCore/SmingCore.h b/Sming/SmingCore/SmingCore.h old mode 100644 new mode 100755 index a456f5d90b..a9fd8980e1 --- a/Sming/SmingCore/SmingCore.h +++ b/Sming/SmingCore/SmingCore.h @@ -55,7 +55,7 @@ #include "Data/Stream/FileStream.h" #include "Data/Stream/TemplateFileStream.h" -#include "../Services/DateTime/DateTime.h" +#include #include "../Services/FATFS/ff.h" #include "../Services/Yeelight/YeelightBulb.h" diff --git a/Sming/SmingCore/SystemClock.h b/Sming/SmingCore/SystemClock.h old mode 100644 new mode 100755 index 17bb6ea64d..d79869d0d8 --- a/Sming/SmingCore/SystemClock.h +++ b/Sming/SmingCore/SystemClock.h @@ -13,7 +13,7 @@ #ifndef SMINGCORE_SYSTEMCLOCK_H_ #define SMINGCORE_SYSTEMCLOCK_H_ -#include "../Services/DateTime/DateTime.h" +#include #include "WString.h" /** @addtogroup constants From f310a2253bc5f6a80e968e072aa43cfc0d8c1b7c Mon Sep 17 00:00:00 2001 From: riban-bw Date: Mon, 10 Dec 2018 19:16:09 +0000 Subject: [PATCH 02/16] Adds DateTime::format method --- Sming/SmingCore/DateTime/DateTime.cpp | 176 +++++++++++++++++- Sming/SmingCore/DateTime/DateTime.h | 11 ++ Sming/SmingCore/Network/Http/HttpConnection.h | 2 +- Sming/SmingCore/Network/NtpClient.h | 2 +- Sming/SmingCore/SmingCore.h | 3 +- Sming/SmingCore/SystemClock.h | 2 +- 6 files changed, 188 insertions(+), 8 deletions(-) diff --git a/Sming/SmingCore/DateTime/DateTime.cpp b/Sming/SmingCore/DateTime/DateTime.cpp index 70e847e696..f837bf07ef 100755 --- a/Sming/SmingCore/DateTime/DateTime.cpp +++ b/Sming/SmingCore/DateTime/DateTime.cpp @@ -16,8 +16,8 @@ /* * Used to parse HTTP date strings - see fromHttpDate() */ -static DEFINE_FSTR(flashMonthNames, "Jan\0Feb\0Mar\0Apr\0May\0Jun\0Jul\0Aug\0Sep\0Oct\0Nov\0Dec"); -static DEFINE_FSTR(flashDayNames, "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat"); +static DEFINE_FSTR(flashMonthNames, "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December"); +static DEFINE_FSTR(flashDayNames, "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday"); /** @brief Get the number of days in a month, taking leap years into account * @param month 0=jan @@ -89,7 +89,7 @@ bool DateTime::fromHttpDate(const String& httpDate) return false; // Search is case insensitive - Month = CStringArray(flashMonthNames).indexOf(monthName); + Month = CStringArray(flashMonthNames).indexOf(monthName); //!@todo This is broken by long month names if(Month < 0) return false; // Invalid month @@ -156,7 +156,7 @@ String DateTime::toISO8601() String DateTime::toHTTPDate() { char buf[30]; - m_snprintf(buf, sizeof(buf), _F("%s, %02d %s %04d %02d:%02d:%02d GMT"), CStringArray(flashDayNames)[DayofWeek], Day, CStringArray(flashMonthNames)[Month], Year, Hour, Minute, Second); + m_snprintf(buf, sizeof(buf), _F("%s, %02d %s %04d %02d:%02d:%02d GMT"), getShortName(CStringArray(flashDayNames)[DayofWeek]).c_str(), Day, getShortName(CStringArray(flashMonthNames)[Month]).c_str(), Year, Hour, Minute, Second); return String(buf); } @@ -245,3 +245,171 @@ time_t DateTime::toUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int seconds += sec; return seconds; } + +String DateTime::format(String sFormat) +{ + //!@todo Add localisation (maybe via pre-compiler conditions) + //!@todo Rationalise comments (taken from two sources) + String sReturn; + char buf[64]; //!@todo Check size of buffer is optimal + + for(unsigned int pos = 0; pos < sFormat.length(); ++pos) + { + if(sFormat[pos] == '%') + { + if(pos < sFormat.length() - 1) //Ignore % as last character + { + buf[0] = '\0'; //Empty string in case no format character matches + switch(sFormat[pos + 1]) + { + //Year (not implemented: EY, Oy, Ey, EC, G, g) + case 'Y': //writes year as a decimal number, e.g. 2017 + m_snprintf(buf, sizeof(buf), _F("%04d"), Year); + break; + case 'y': //writes last 2 digits of year as a decimal number (range [00,99]) + m_snprintf(buf, sizeof(buf), _F("%02d"), Year%100); + break; + case 'C': //writes first 2 digits of year as a decimal number (range [00,99]) + m_snprintf(buf, sizeof(buf), _F("%02d"), Year/100); + break; + //Month (not implemented: Om) + case 'b': //writes abbreviated month name, e.g. Oct (always English) + case 'h': //synonym of b + m_snprintf(buf, sizeof(buf), _F("%s"), getShortName(CStringArray(flashMonthNames)[Month]).c_str()); + break; + case 'B': //writes full month name, e.g. October (locale dependent) + m_snprintf(buf, sizeof(buf), _F("%s"), CStringArray(flashMonthNames)[Month]); + break; + case 'm': //writes month as a decimal number (range [01,12]) + m_snprintf(buf, sizeof(buf), _F("%02d"), Month + 1); + break; + //Week (not implemented: OU, OW, V, OV) + case 'U': //writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]) + //!@todo Implement week of the year (from Sunday) + m_snprintf(buf, sizeof(buf), _F("??")); + break; + case 'w': //Weekday as a decimal number with Sunday as 0 (0-6) + m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); + break; + case 'W': //writes week of the year as a decimal number (Monday is the first day of the week) (range [00,53]) + //!@todo Implement week of the year (from Monday) + m_snprintf(buf, sizeof(buf), _F("??")); + break; + case 'x': //Date representation + //!@todo Date locale + m_snprintf(buf, sizeof(buf), _F("%s"), format("%d/%m/%Y").c_str()); + break; + case 'X': //Time representation + //!@todo Time locale + m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); + break; + // Day of year/month (Not implemented: Od, e, Oe) + case 'j': //writes day of the year as a decimal number (range [001,366]) + { + //!@todo Store day of year during timestamp parsing + unsigned int nDays = 0; + for(unsigned int i=0; i 12)?Hour-12:Hour):12); + break; + case 'M': //writes minute as a decimal number (range [00,59]) + m_snprintf(buf, sizeof(buf), _F("%02d"), Minute); + break; + case 'S': //writes second as a decimal number (range [00,60]) + m_snprintf(buf, sizeof(buf), _F("%02d"), Second); + break; + // Other (not implemented: Ec, Ex, EX, z, Z) + case 'c': //writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (English only) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%a %b %d %H:%M:%S %Y").c_str()); + break; + case 'D': //equivalent to "%m/%d/%y" + m_snprintf(buf, sizeof(buf), _F("%s"), format("%m/%d/%y").c_str()); + break; + case 'F': //equivalent to "%Y-%m-%d" (the ISO 8601 date format) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%Y-%m-%d").c_str()); + break; + case 'r': //12-hour clock time + //!@todo Adjust for locale + //!@todo 12 hour time looks wrong with leading zero + m_snprintf(buf, sizeof(buf), _F("%s"), format("%I:%M:%S %p").c_str()); + break; + case 'R': //equivalent to "%H:%M" + m_snprintf(buf, sizeof(buf), _F("%02d:%02d"), Hour, Minute); + break; + case 'T': //equivalent to "%H:%M:%S" (the ISO 8601 time format) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); + break; + case 'p': //writes localized a.m. or p.m. (English only) + m_snprintf(buf, sizeof(buf), _F("%s"), (Hour < 12)?"AM":"PM"); //!@todo Should am/pm, upper/lowercase, with/without delimitets? + break; + case '%': //writes literal %. The full conversion specification must be %% + m_snprintf(buf, sizeof(buf), _F("%s"), "%"); + break; + case 'n': //writes newline character + m_snprintf(buf, sizeof(buf), _F("%s"), "\n"); + break; + case 't': //writes horizontal tab character + m_snprintf(buf, sizeof(buf), _F("%s"), "\t"); + break; + default: + m_snprintf(buf, sizeof(buf), _F("%s"), "??"); + } + sReturn += buf; + ++pos; //Skip format charachter + } + } + else + { + //Normal charachter + sReturn += sFormat[pos]; + } + } + return sReturn; +} + +String DateTime::getShortName(const char* longname) +{ + String sReturn(longname, 3); + return sReturn; +} + diff --git a/Sming/SmingCore/DateTime/DateTime.h b/Sming/SmingCore/DateTime/DateTime.h index 05ddf138ee..ea9e4f8ebb 100755 --- a/Sming/SmingCore/DateTime/DateTime.h +++ b/Sming/SmingCore/DateTime/DateTime.h @@ -242,6 +242,17 @@ class DateTime return toUnixTime(sec, min, hour, day, month, year); } + /** @brief Create string with arbitary formatting + * @param format String including date and time formatting + * @retval String Formatted string + * @note Uses strftime style formatting, e.g. format("Today is %a, %d %b %Y") returns "Today is Mon, 10 Dec 2012" + * @note Does not support localisation + */ + String format(String format); + +private: + String getShortName(const char* longname); + public: int8_t Hour = 0; ///< Hour (0-23) int8_t Minute = 0; ///< Minute (0-59) diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index 571deaa29c..1e2312d04b 100755 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -14,7 +14,7 @@ #define _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ #include "HttpConnectionBase.h" -#include +#include #include "Data/ObjectQueue.h" /** @defgroup HTTP client connection diff --git a/Sming/SmingCore/Network/NtpClient.h b/Sming/SmingCore/Network/NtpClient.h index 3d3b295555..4dac5aebde 100755 --- a/Sming/SmingCore/Network/NtpClient.h +++ b/Sming/SmingCore/Network/NtpClient.h @@ -18,7 +18,7 @@ #include "UdpConnection.h" #include "Platform/System.h" #include "Timer.h" -#include +#include #include "Delegate.h" #define NTP_PORT 123 diff --git a/Sming/SmingCore/SmingCore.h b/Sming/SmingCore/SmingCore.h index a9fd8980e1..1c43047cbe 100755 --- a/Sming/SmingCore/SmingCore.h +++ b/Sming/SmingCore/SmingCore.h @@ -55,7 +55,8 @@ #include "Data/Stream/FileStream.h" #include "Data/Stream/TemplateFileStream.h" -#include +#include "DateTime/DateTime.h" + #include "../Services/FATFS/ff.h" #include "../Services/Yeelight/YeelightBulb.h" diff --git a/Sming/SmingCore/SystemClock.h b/Sming/SmingCore/SystemClock.h index d79869d0d8..ed57983d73 100755 --- a/Sming/SmingCore/SystemClock.h +++ b/Sming/SmingCore/SystemClock.h @@ -13,7 +13,7 @@ #ifndef SMINGCORE_SYSTEMCLOCK_H_ #define SMINGCORE_SYSTEMCLOCK_H_ -#include +#include #include "WString.h" /** @addtogroup constants From 7fced8aaed80499f10fe28b97b916bc3e50e3454 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Mon, 10 Dec 2018 22:00:24 +0000 Subject: [PATCH 03/16] Fixes fromHttpDate not setting DayOfWeek. Fixes fromHttpDate to work with long names in day / month arrays. Use format method for all string convenience methods. Rationalise comments. Remove "??" from unimplemented format options. Add sample project. --- Sming/SmingCore/DateTime/DateTime.cpp | 139 ++++++++--------- samples/Basic_DateTime/.cproject | 151 +++++++++++++++++++ samples/Basic_DateTime/.project | 28 ++++ samples/Basic_DateTime/Makefile | 20 +++ samples/Basic_DateTime/Makefile-user.mk | 39 +++++ samples/Basic_DateTime/app/application.cpp | 113 ++++++++++++++ samples/Basic_DateTime/include/user_config.h | 42 ++++++ 7 files changed, 463 insertions(+), 69 deletions(-) create mode 100644 samples/Basic_DateTime/.cproject create mode 100644 samples/Basic_DateTime/.project create mode 100644 samples/Basic_DateTime/Makefile create mode 100644 samples/Basic_DateTime/Makefile-user.mk create mode 100755 samples/Basic_DateTime/app/application.cpp create mode 100644 samples/Basic_DateTime/include/user_config.h diff --git a/Sming/SmingCore/DateTime/DateTime.cpp b/Sming/SmingCore/DateTime/DateTime.cpp index f837bf07ef..017310753d 100755 --- a/Sming/SmingCore/DateTime/DateTime.cpp +++ b/Sming/SmingCore/DateTime/DateTime.cpp @@ -64,19 +64,24 @@ bool DateTime::isNull() bool DateTime::fromHttpDate(const String& httpDate) { - int first = httpDate.indexOf(','); - if(first < 0 || httpDate.length() - first < 20) + if(httpDate.length() < 29) return false; - first++; // Skip ',' - if(httpDate[first] == ' ') - first++; - - auto ptr = httpDate.c_str() + first; + auto ptr = httpDate.c_str(); // Parse and return a decimal number and update ptr to the first non-numeric character after it auto parseNumber = [&ptr]() { return strtol(ptr, const_cast(&ptr), 10); }; + char dayName[] = {ptr[0], ptr[1], ptr[2], '\0'}; + for(DayofWeek = 0; DayofWeek < 7; ++DayofWeek) + { + if(strncmp(CStringArray(flashDayNames)[DayofWeek], dayName, 3) == 0) + break; + } + if(DayofWeek > 6) + return false; // Invalid day of week + ptr += 5; + Day = parseNumber(); if(*ptr == '\0') return false; @@ -85,12 +90,14 @@ bool DateTime::fromHttpDate(const String& httpDate) ptr++; char monthName[] = {ptr[0], ptr[1], ptr[2], '\0'}; ptr += 4; // Skip space as well as month - if(*ptr == '\0') - return false; // Search is case insensitive - Month = CStringArray(flashMonthNames).indexOf(monthName); //!@todo This is broken by long month names - if(Month < 0) + for(Month = 0; Month < 12; ++Month) + { + if(strncmp(CStringArray(flashMonthNames)[Month], monthName, 3) == 0) + break; + } + if(Month > 11) return false; // Invalid month Year = parseNumber(); @@ -125,39 +132,30 @@ time_t DateTime::toUnixTime() String DateTime::toShortDateString() { - char buf[16]; - m_snprintf(buf, sizeof(buf), _F("%02d.%02d.%d"), Day, Month + 1, Year); - return String(buf); + return format(_F("%d.%m.%Y")); } String DateTime::toShortTimeString(bool includeSeconds /* = false*/) { - char buf[16]; if(includeSeconds) - m_snprintf(buf, sizeof(buf), _F("%02d:%02d:%02d"), Hour, Minute, Second); + return format(_F("%T")); else - m_snprintf(buf, sizeof(buf), _F("%02d:%02d"), Hour, Minute); - - return String(buf); + return format(_F("%r")); } String DateTime::toFullDateTimeString() { - return toShortDateString() + ' ' + toShortTimeString(true); + return format(_F("%x %T")); } String DateTime::toISO8601() { - char buf[32]; - m_snprintf(buf, sizeof(buf), _F("%04d-%02d-%02dT%02d:%02d:%02dZ"), Year, Month + 1, Day, Hour, Minute, Second); - return String(buf); + return format(_F("%FT%TZ")); } String DateTime::toHTTPDate() { - char buf[30]; - m_snprintf(buf, sizeof(buf), _F("%s, %02d %s %04d %02d:%02d:%02d GMT"), getShortName(CStringArray(flashDayNames)[DayofWeek]).c_str(), Day, getShortName(CStringArray(flashMonthNames)[Month]).c_str(), Year, Hour, Minute, Second); - return String(buf); + return format(_F("%a, %d %b %Y %T GMT")); } void DateTime::addMilliseconds(long add) @@ -249,7 +247,6 @@ time_t DateTime::toUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int String DateTime::format(String sFormat) { //!@todo Add localisation (maybe via pre-compiler conditions) - //!@todo Rationalise comments (taken from two sources) String sReturn; char buf[64]; //!@todo Check size of buffer is optimal @@ -263,48 +260,48 @@ String DateTime::format(String sFormat) switch(sFormat[pos + 1]) { //Year (not implemented: EY, Oy, Ey, EC, G, g) - case 'Y': //writes year as a decimal number, e.g. 2017 + case 'Y': //Full year as a decimal number, e.g. 2018 m_snprintf(buf, sizeof(buf), _F("%04d"), Year); break; - case 'y': //writes last 2 digits of year as a decimal number (range [00,99]) + case 'y': //Year, last 2 digits as a decimal number [00..99] m_snprintf(buf, sizeof(buf), _F("%02d"), Year%100); break; - case 'C': //writes first 2 digits of year as a decimal number (range [00,99]) + case 'C': //Year, first 2 digits as a decimal number [00..99] m_snprintf(buf, sizeof(buf), _F("%02d"), Year/100); break; //Month (not implemented: Om) - case 'b': //writes abbreviated month name, e.g. Oct (always English) - case 'h': //synonym of b + case 'b': //Abbreviated month name, e.g. Oct (always English) + case 'h': //Synonym of b + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), getShortName(CStringArray(flashMonthNames)[Month]).c_str()); break; - case 'B': //writes full month name, e.g. October (locale dependent) + case 'B': //Full month name, e.g. October (always English) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), CStringArray(flashMonthNames)[Month]); break; - case 'm': //writes month as a decimal number (range [01,12]) + case 'm': //Month as a decimal number [01..12] m_snprintf(buf, sizeof(buf), _F("%02d"), Month + 1); break; //Week (not implemented: OU, OW, V, OV) - case 'U': //writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]) + case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] //!@todo Implement week of the year (from Sunday) - m_snprintf(buf, sizeof(buf), _F("??")); break; - case 'w': //Weekday as a decimal number with Sunday as 0 (0-6) + case 'w': //Weekday as a decimal number with Sunday as 0 [0..6] m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); break; - case 'W': //writes week of the year as a decimal number (Monday is the first day of the week) (range [00,53]) + case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] //!@todo Implement week of the year (from Monday) - m_snprintf(buf, sizeof(buf), _F("??")); break; - case 'x': //Date representation - //!@todo Date locale + case 'x': //Short date (DD/MM/YYYY) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format("%d/%m/%Y").c_str()); break; - case 'X': //Time representation - //!@todo Time locale + case 'X': //Time (HH:MM:SS) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); break; - // Day of year/month (Not implemented: Od, e, Oe) - case 'j': //writes day of the year as a decimal number (range [001,366]) + // Day of year/month (Not implemented: Od, Oe) + case 'j': //Day of the year as a decimal number [001..366] { //!@todo Store day of year during timestamp parsing unsigned int nDays = 0; @@ -329,70 +326,72 @@ String DateTime::format(String sFormat) m_snprintf(buf, sizeof(buf), _F("%03d"), nDays); break; } - case 'd': //writes day of the month as a decimal number (range [01,31]) + case 'd': //Day of the month as a decimal number [01..31] m_snprintf(buf, sizeof(buf), _F("%02d"), Day); break; - case 'e': //writes day of the month as a decimal number (range [1,31]) + case 'e': //Day of the month as a decimal number [ 1,31] m_snprintf(buf, sizeof(buf), _F("% 2d"), Day); break; // Day of week (Not implemented: Ow, Ou) - case 'a': //writes abbreviated weekday name, e.g. Fri (locale dependent) + case 'a': //Abbreviated weekday name, e.g. Fri (English only) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), getShortName(CStringArray(flashDayNames)[DayofWeek]).c_str()); break; - case 'A': //writes full weekday name, e.g. Friday (locale dependent) + case 'A': //Full weekday name, e.g. Friday (English only) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), (CStringArray(flashDayNames)[DayofWeek])); break; - case 'u': //writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) + case 'u': //Weekday as a decimal number, where Monday is 1 (ISO 8601 format) [1..7] m_snprintf(buf, sizeof(buf), _F("%d"), (DayofWeek == 0)?7:DayofWeek); break; - //Hour, minute, second (not implemented: OH, OI, OM, OS) - case 'H': //writes hour as a decimal number, 24 hour clock (range [00-23]) + //Time (not implemented: OH, OI, OM, OS) + case 'H': //Hour as a decimal number, 24 hour clock [00..23] m_snprintf(buf, sizeof(buf), _F("%02d"), Hour); break; - case 'I': //writes hour as a decimal number, 12 hour clock (range [1,12]) + case 'I': //Hour as a decimal number, 12 hour clock [0..12] m_snprintf(buf, sizeof(buf), _F("%02d"), Hour?((Hour > 12)?Hour-12:Hour):12); break; - case 'M': //writes minute as a decimal number (range [00,59]) + case 'M': //Minute as a decimal number [00..59] m_snprintf(buf, sizeof(buf), _F("%02d"), Minute); break; - case 'S': //writes second as a decimal number (range [00,60]) + case 'S': //Second as a decimal number [00..61] m_snprintf(buf, sizeof(buf), _F("%02d"), Second); break; // Other (not implemented: Ec, Ex, EX, z, Z) case 'c': //writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (English only) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format("%a %b %d %H:%M:%S %Y").c_str()); break; - case 'D': //equivalent to "%m/%d/%y" + case 'D': //Short date (MM/DD/YY) m_snprintf(buf, sizeof(buf), _F("%s"), format("%m/%d/%y").c_str()); break; - case 'F': //equivalent to "%Y-%m-%d" (the ISO 8601 date format) + case 'F': //ISO 8601 date format (YYYY-mm-dd) m_snprintf(buf, sizeof(buf), _F("%s"), format("%Y-%m-%d").c_str()); break; - case 'r': //12-hour clock time - //!@todo Adjust for locale - //!@todo 12 hour time looks wrong with leading zero + case 'r': //12-hour clock time (hh:MM:SS AM) + //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format("%I:%M:%S %p").c_str()); break; - case 'R': //equivalent to "%H:%M" + case 'R': //Short time (HH:MM) m_snprintf(buf, sizeof(buf), _F("%02d:%02d"), Hour, Minute); break; - case 'T': //equivalent to "%H:%M:%S" (the ISO 8601 time format) + case 'T': //ISO 8601 time format (HH:MM:SS) m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); break; - case 'p': //writes localized a.m. or p.m. (English only) - m_snprintf(buf, sizeof(buf), _F("%s"), (Hour < 12)?"AM":"PM"); //!@todo Should am/pm, upper/lowercase, with/without delimitets? + case 'p': //Meridiem [AM,PM] + m_snprintf(buf, sizeof(buf), _F("%s"), (Hour < 12)?"AM":"PM"); break; - case '%': //writes literal %. The full conversion specification must be %% + case '%': //Literal percent (%). The full conversion specification must be %% m_snprintf(buf, sizeof(buf), _F("%s"), "%"); break; - case 'n': //writes newline character + case 'n': //Newline character (\n) m_snprintf(buf, sizeof(buf), _F("%s"), "\n"); break; - case 't': //writes horizontal tab character + case 't': //Horizontal tab (\t) m_snprintf(buf, sizeof(buf), _F("%s"), "\t"); break; - default: - m_snprintf(buf, sizeof(buf), _F("%s"), "??"); + default: //Silently ignore % and process next character + --pos; } sReturn += buf; ++pos; //Skip format charachter @@ -407,6 +406,8 @@ String DateTime::format(String sFormat) return sReturn; } +// Helper function to return first 3 characters +//!@todo Return a c_str as we don't use any features of String and have to convert back to c_str each time String DateTime::getShortName(const char* longname) { String sReturn(longname, 3); diff --git a/samples/Basic_DateTime/.cproject b/samples/Basic_DateTime/.cproject new file mode 100644 index 0000000000..67c056d24e --- /dev/null +++ b/samples/Basic_DateTime/.cproject @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + -f ${ProjDirPath}/Makefile + all + true + true + true + + + make + -f ${ProjDirPath}/Makefile + clean + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flash + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashonefile + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashinit + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashboot + true + true + true + + + make + -f ${ProjDirPath}/Makefile + rebuild + true + true + true + + + + + + + + + + + + + + + + + + + + diff --git a/samples/Basic_DateTime/.project b/samples/Basic_DateTime/.project new file mode 100644 index 0000000000..045ddeccff --- /dev/null +++ b/samples/Basic_DateTime/.project @@ -0,0 +1,28 @@ + + + Basic_Blink + + + SmingFramework + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/samples/Basic_DateTime/Makefile b/samples/Basic_DateTime/Makefile new file mode 100644 index 0000000000..77fee85347 --- /dev/null +++ b/samples/Basic_DateTime/Makefile @@ -0,0 +1,20 @@ +##################################################################### +#### Please don't change this file. Use Makefile-user.mk instead #### +##################################################################### +# Including user Makefile. +# Should be used to set project-specific parameters +include ./Makefile-user.mk + +# Important parameters check. +# We need to make sure SMING_HOME and ESP_HOME variables are set. +# You can use Makefile-user.mk in each project or use enviromental variables to set it globally. + +ifndef SMING_HOME +$(error SMING_HOME is not set. Please configure it in Makefile-user.mk) +endif +ifndef ESP_HOME +$(error ESP_HOME is not set. Please configure it in Makefile-user.mk) +endif + +# Include application Makefile +include $(SMING_HOME)/Makefile-rboot.mk \ No newline at end of file diff --git a/samples/Basic_DateTime/Makefile-user.mk b/samples/Basic_DateTime/Makefile-user.mk new file mode 100644 index 0000000000..bd088ca354 --- /dev/null +++ b/samples/Basic_DateTime/Makefile-user.mk @@ -0,0 +1,39 @@ +## Local build configuration +## Parameters configured here will override default and ENV values. +## Uncomment and change examples: + +## Add your source directories here separated by space +# MODULES = app +# EXTRA_INCDIR = include + +## ESP_HOME sets the path where ESP tools and SDK are located. +## Windows: +# ESP_HOME = c:/Espressif + +## MacOS / Linux: +# ESP_HOME = /opt/esp-open-sdk + +## SMING_HOME sets the path where Sming framework is located. +## Windows: +# SMING_HOME = c:/tools/sming/Sming + +## MacOS / Linux +# SMING_HOME = /opt/sming/Sming + +## COM port parameter is reqruied to flash firmware correctly. +## Windows: +# COM_PORT = COM3 + +## MacOS / Linux: +# COM_PORT = /dev/tty.usbserial + +## Com port speed +# COM_SPEED = 115200 + +## Configure flash parameters (for ESP12-E and other new boards): +# SPI_MODE = dio + +## SPIFFS options +DISABLE_SPIFFS = 1 +# SPIFF_FILES = files + diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp new file mode 100755 index 0000000000..f059797384 --- /dev/null +++ b/samples/Basic_DateTime/app/application.cpp @@ -0,0 +1,113 @@ +/** Basic_DateTime sample application + Author: Brian Walton (brian@riban.co.uk) + Date: 2018-12-10 + Provides serial interface, accepting Unix timestamp + Prints each type of DateTime::format option +*/ +#include +#include + +time_t g_timestamp = 0; +size_t g_tsLength = 0; + +void showTime(time_t timestamp) +{ + DateTime dt(timestamp); + //Non-time + Serial.println(dt.format("%%%% Percent sign: %%")); + Serial.println(dt.format("%%n New-line character: %n")); + Serial.println(dt.format("%%t Horizontal-tab character: >|%t|<")); + //Year + Serial.println(dt.format("%%Y Full year (YYYY): %Y")); + Serial.println(dt.format("%%C Year, first two digits (00-99)%: %C")); + Serial.println(dt.format("%%y Year, last two digits (00-99): %y")); + //Month + Serial.println(dt.format("%%B Full month name (e.g. June): %B")); + Serial.println(dt.format("%%b Abbreviated month name (e.g. Jun): %b")); + Serial.println(dt.format("%%h Abbreviated month name (e.g. Jun): %h")); + Serial.println(dt.format("%%m Month as a decimal number (01-12): %m")); + //Week + Serial.println(dt.format("%%U Week number with the first Sunday as the first day of week one (00-53): %U")); //NYI + Serial.println(dt.format("%%W Week number with the first Monday as the first day of week one (00-53): %W")); //NYI + Serial.println(dt.format("%%V ISO 8601 week number (01-53): %V")); //NYI + //Day + Serial.println(dt.format("%%j Day of the year (001-366): %j")); + Serial.println(dt.format("%%d Day of the month, zero-padded (01-31)%: %d")); + Serial.println(dt.format("%%e Day of the month, space-padded ( 1-31): %e")); + Serial.println(dt.format("%%A Full weekday name (e.g. Monday): %A")); + Serial.println(dt.format("%%a Abbreviated weekday name (e.g. Mon): %a")); + Serial.println(dt.format("%%w Weekday as a decimal number with Sunday as 0 (0-6): %w")); + Serial.println(dt.format("%%u ISO 8601 weekday as number with Monday as 1 (1-7): %u")); + //Hour + Serial.println(dt.format("%%p Meridiem (AM|PM): %p")); + Serial.println(dt.format("%%H Hour in 24h format (00-23): %H")); + Serial.println(dt.format("%%h Hour in 12h format (01-12): %I")); + //Mnute + Serial.println(dt.format("%%M Minute (00-59): %M")); + //Second + Serial.println(dt.format("%%S Second (00-61): %S")); + //Formatted strings + Serial.println(dt.format("%%R 24-hour time (HH:MM): %R")); + Serial.println(dt.format("%%r 12-hour time (hh:MM AM): %r")); + Serial.println(dt.format("%%c Date and time (ddd mmm DD HH:MM:SS YYYY): %c")); + Serial.println(dt.format("%%D Short date (MM/DD/YY: %D")); + Serial.println(dt.format("%%F Short date (YYYY-MM-DD): %F")); + Serial.println(dt.format("%%T ISO 8601 time format (HH:MM:SS): %T")); + Serial.println(dt.format("%%x Local date (DD/MM/YYYY): %x")); + Serial.println(dt.format("%%X Local time (HH:MM:SS): %X")); + //HTTP date + Serial.print("toHTTPDate: "); + Serial.println(dt.toHTTPDate()); + DateTime dt2; + dt2.fromHttpDate(dt.toHTTPDate()); + Serial.print("fromHTTPDate: "); + Serial.println(dt2.toHTTPDate()); +} + +void onRx(Stream& source, char arrivedChar, unsigned short availableCharsCount) +{ + switch(arrivedChar) + { + case '\n': + Serial.println(); + Serial.println(); + Serial.print("****Showing DateTime formating options for Unix timestamp: "); + Serial.println(g_timestamp); + showTime(g_timestamp); + Serial.print("Enter Unix timestamp: "); + g_timestamp = 0; + g_tsLength = 0; + break; + case '0' ... '9': + g_timestamp *= 10; + g_timestamp += arrivedChar - '0'; + ++g_tsLength; + Serial.print(arrivedChar); + break; + case '\b': + if(g_tsLength) + { + Serial.print('\b'); + Serial.print(" "); + Serial.print('\b'); + --g_tsLength; + g_timestamp /= 10; + } + break; + case 27: + g_timestamp = 0; + g_tsLength = 0; + Serial.print('\r'); + Serial.print(" "); + Serial.print('\r'); + Serial.print("Enter Unix timestamp: "); + } +} + +void init() +{ + Serial.begin(115200); + delay(2000); + Serial.print("Enter Unix timestamp: "); + Serial.setCallback(onRx); +} diff --git a/samples/Basic_DateTime/include/user_config.h b/samples/Basic_DateTime/include/user_config.h new file mode 100644 index 0000000000..6b33f4a9c5 --- /dev/null +++ b/samples/Basic_DateTime/include/user_config.h @@ -0,0 +1,42 @@ +#ifndef __USER_CONFIG_H__ +#define __USER_CONFIG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +// UART config +#define SERIAL_BAUD_RATE COM_SPEED_SERIAL + +// ESP SDK config +#define LWIP_OPEN_SRC +#define USE_US_TIMER + +// Default types +#define __CORRECT_ISO_CPP_STDLIB_H_PROTO +#include +#include + +// Override c_types.h include and remove buggy espconn +#define _C_TYPES_H_ +#define _NO_ESPCON_ + +// Updated, compatible version of c_types.h +// Just removed types declared in +#include + +// System API declarations +#include + +// C++ Support +#include +// Extended string conversion for compatibility +#include +// Network base API +#include + +#ifdef __cplusplus +} +#endif + +#endif From 09d38af271c21878313e6cfacadaf18c97931e72 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Tue, 11 Dec 2018 07:20:10 +0000 Subject: [PATCH 04/16] Add 'w' option (weekday as decimal). Add day of year. Add more examples to sample. --- Sming/SmingCore/DateTime/DateTime.cpp | 66 +++++++++++----------- Sming/SmingCore/DateTime/DateTime.h | 3 +- samples/Basic_DateTime/app/application.cpp | 13 ++++- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/Sming/SmingCore/DateTime/DateTime.cpp b/Sming/SmingCore/DateTime/DateTime.cpp index 017310753d..b368f49d6c 100755 --- a/Sming/SmingCore/DateTime/DateTime.cpp +++ b/Sming/SmingCore/DateTime/DateTime.cpp @@ -43,6 +43,7 @@ void DateTime::setTime(time_t time) { fromUnixTime(time, &Second, &Minute, &Hour, &Day, &DayofWeek, &Month, &Year); Milliseconds = 0; + calcDayOfYear(); } void DateTime::setTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) @@ -54,6 +55,7 @@ void DateTime::setTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t m Month = month; Year = year; Milliseconds = 0; + calcDayOfYear(); } bool DateTime::isNull() @@ -121,6 +123,7 @@ bool DateTime::fromHttpDate(const String& httpDate) ptr++; Second = parseNumber(); Milliseconds = 0; + calcDayOfYear(); return true; } @@ -273,7 +276,7 @@ String DateTime::format(String sFormat) case 'b': //Abbreviated month name, e.g. Oct (always English) case 'h': //Synonym of b //!@todo Implement locale - m_snprintf(buf, sizeof(buf), _F("%s"), getShortName(CStringArray(flashMonthNames)[Month]).c_str()); + m_snprintf(buf, 4, _F("%s"), CStringArray(flashMonthNames)[Month]); break; case 'B': //Full month name, e.g. October (always English) //!@todo Implement locale @@ -284,13 +287,13 @@ String DateTime::format(String sFormat) break; //Week (not implemented: OU, OW, V, OV) case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] - //!@todo Implement week of the year (from Sunday) + //!@todo Implement week number (Sunday as first day of week) break; - case 'w': //Weekday as a decimal number with Sunday as 0 [0..6] - m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); + case 'V': //ISO 8601 week number (01-53) + //!@todo Implement ISO 8601 week number break; case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] - //!@todo Implement week of the year (from Monday) + //!@todo Implement week number (Monday as first day of week) break; case 'x': //Short date (DD/MM/YYYY) //!@todo Implement locale @@ -303,27 +306,7 @@ String DateTime::format(String sFormat) // Day of year/month (Not implemented: Od, Oe) case 'j': //Day of the year as a decimal number [001..366] { - //!@todo Store day of year during timestamp parsing - unsigned int nDays = 0; - for(unsigned int i=0; i Date: Tue, 11 Dec 2018 10:38:27 +0000 Subject: [PATCH 05/16] Reverts erroneous change of file permission. Adds localisation framework (default en-gb). Fixes 2 digit date check (was <69). Improves documentation for format method. Corrects typo. --- Sming/SmingCore/{DateTime => }/DateTime.cpp | 33 +++++------- Sming/SmingCore/{DateTime => }/DateTime.h | 58 ++++++++++++++++++--- Sming/SmingCore/SmingCore.h | 0 3 files changed, 65 insertions(+), 26 deletions(-) rename Sming/SmingCore/{DateTime => }/DateTime.cpp (91%) rename Sming/SmingCore/{DateTime => }/DateTime.h (79%) mode change 100755 => 100644 Sming/SmingCore/SmingCore.h diff --git a/Sming/SmingCore/DateTime/DateTime.cpp b/Sming/SmingCore/DateTime.cpp similarity index 91% rename from Sming/SmingCore/DateTime/DateTime.cpp rename to Sming/SmingCore/DateTime.cpp index b368f49d6c..692b404bb6 100755 --- a/Sming/SmingCore/DateTime/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -16,8 +16,8 @@ /* * Used to parse HTTP date strings - see fromHttpDate() */ -static DEFINE_FSTR(flashMonthNames, "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December"); -static DEFINE_FSTR(flashDayNames, "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday"); +static DEFINE_FSTR(flashMonthNames, LOCALE_MONTH_NAMES); +static DEFINE_FSTR(flashDayNames, LOCALE_DAY_NAMES); /** @brief Get the number of days in a month, taking leap years into account * @param month 0=jan @@ -106,7 +106,7 @@ bool DateTime::fromHttpDate(const String& httpDate) if(*ptr == '\0') return false; - if(Year < 69) + if(Year < 70) Year += 2000; else if(Year < 100) Year += 1900; @@ -275,11 +275,9 @@ String DateTime::format(String sFormat) //Month (not implemented: Om) case 'b': //Abbreviated month name, e.g. Oct (always English) case 'h': //Synonym of b - //!@todo Implement locale m_snprintf(buf, 4, _F("%s"), CStringArray(flashMonthNames)[Month]); break; case 'B': //Full month name, e.g. October (always English) - //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), CStringArray(flashMonthNames)[Month]); break; case 'm': //Month as a decimal number [01..12] @@ -295,13 +293,12 @@ String DateTime::format(String sFormat) case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] //!@todo Implement week number (Monday as first day of week) break; - case 'x': //Short date (DD/MM/YYYY) - //!@todo Implement locale - m_snprintf(buf, sizeof(buf), _F("%s"), format("%d/%m/%Y").c_str()); + case 'x': //Locale preferred date format + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE).c_str()); break; - case 'X': //Time (HH:MM:SS) + case 'X': //Locale preferred time format //!@todo Implement locale - m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_TIME).c_str()); break; // Day of year/month (Not implemented: Od, Oe) case 'j': //Day of the year as a decimal number [001..366] @@ -319,12 +316,10 @@ String DateTime::format(String sFormat) case 'w': //Weekday as a decimal number with Sunday as 0 [0..6] m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); break; - case 'a': //Abbreviated weekday name, e.g. Fri (English only) - //!@todo Implement locale + case 'a': //Abbreviated weekday name, e.g. Fri m_snprintf(buf, 4, _F("%s"), CStringArray(flashDayNames)[DayofWeek]); break; - case 'A': //Full weekday name, e.g. Friday (English only) - //!@todo Implement locale + case 'A': //Full weekday name, e.g. Friday m_snprintf(buf, sizeof(buf), _F("%s"), (CStringArray(flashDayNames)[DayofWeek])); break; case 'u': //Weekday as a decimal number, where Monday is 1 (ISO 8601 format) [1..7] @@ -344,18 +339,16 @@ String DateTime::format(String sFormat) m_snprintf(buf, sizeof(buf), _F("%02d"), Second); break; // Other (not implemented: Ec, Ex, EX, z, Z) - case 'c': //writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (English only) - //!@todo Implement locale - m_snprintf(buf, sizeof(buf), _F("%s"), format("%a %b %d %H:%M:%S %Y").c_str()); + case 'c': //Locale preferred date and time format, e.g. Tue Dec 11 08:48:32 2018 + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE_TIME).c_str()); break; - case 'D': //Short date (MM/DD/YY) + case 'D': //US date (MM/DD/YY) m_snprintf(buf, sizeof(buf), _F("%s"), format("%m/%d/%y").c_str()); break; case 'F': //ISO 8601 date format (YYYY-mm-dd) m_snprintf(buf, sizeof(buf), _F("%s"), format("%Y-%m-%d").c_str()); break; case 'r': //12-hour clock time (hh:MM:SS AM) - //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format("%I:%M:%S %p").c_str()); break; case 'R': //Short time (HH:MM) @@ -385,7 +378,7 @@ String DateTime::format(String sFormat) } else { - //Normal charachter + //Normal character sReturn += sFormat[pos]; } } diff --git a/Sming/SmingCore/DateTime/DateTime.h b/Sming/SmingCore/DateTime.h similarity index 79% rename from Sming/SmingCore/DateTime/DateTime.h rename to Sming/SmingCore/DateTime.h index 540c682c95..1b364447bf 100755 --- a/Sming/SmingCore/DateTime/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -50,6 +50,16 @@ /** Get quantity of seconds since midnight at start of previous Sunday from given Unix time */ #define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY) ) +/** Define default locale settings as en_GB:en */ +#ifndef LOCALE +#define LOCALE en_GB:en +#define LOCALE_MONTH_NAMES "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" +#define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" +#define LOCALE_DATE "%d/%m/%Y" +#define LOCALE_TIME "%H:%M:%S" +#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" +#endif // LOCALE + // todo add date math macros /*============================================================================*/ @@ -82,7 +92,7 @@ class DateTime { } - /** @brief Instantiate a date and time object + /** @brief Instantiate a date and time object from a Unix timestamp * @param time Unix time to assign to object */ DateTime(time_t time); @@ -92,7 +102,7 @@ class DateTime */ operator time_t() { return toUnixTime(); } - /** @brief Set time using Unix time + /** @brief Set time using Unix timestamp * @param time Unix time to set object time to */ void setTime(time_t time); @@ -119,7 +129,7 @@ class DateTime * @retval bool True on success * @note Also supports obsolete RFC 850 date format, e.g. Sunday, 06-Nov-94 08:49:37 GMT where 2 digit year represents range 1970-2069 * @note GMT suffix is optional and is always assumed / ignored - * @deprecated Use 'fromHttpDat' instead + * @deprecated Use 'fromHttpDate' instead */ bool parseHttpDate(const String& httpDate) __attribute__ ((deprecated)) { @@ -242,11 +252,47 @@ class DateTime return toUnixTime(sec, min, hour, day, month, year); } - /** @brief Create string with arbitary formatting + /** @brief Create string with strftime style formatting * @param format String including date and time formatting * @retval String Formatted string - * @note Uses strftime style formatting, e.g. format("Today is %a, %d %b %Y") returns "Today is Mon, 10 Dec 2012" - * @note Does not support localisation + * @note Uses strftime style formatting, e.g. format("Today is %a, %d %b %Y") returns "Today is Mon, 10 Dec 2018" + * @note Localisation may be implemented in libsming at compile time + * @note Formatting parameters (braced param not yet implemented): + * | Param | Description | Locale | + * | :----:| :---------- | :----: | + * | %%a | Abbreviated weekday name| * | + * | %%A | Full weekday name | * | + * | %%b | Abbreviate month name | * | + * | %%B | Full month name | * | + * | %%c | Locale preferred date and time format | * | + * | %%C | Century number (2 digits) | | + * | %%d | Day of month as decimal number with leading zero (2 digits) | | + * | %%D | US date format (mm/dd/yyyy) | | + * | %%e | Day of month as decimal number with leading space (2 digits) | | + * | %%F | ISO 8601 date format (YYYY-mm-dd) | | + * | %%h | Equivalent to %%b | * | + * | %%H | Hour as a decimal number using a 24-hour clock (range 00 to 23) | | + * | %%I | Hour as a decimal number using a 12-hour clock (range 00 to 12 | | + * | %%j | Day of the year as a decimal number (range 001 to 366) | | + * | %%m | Month as a decimal number (range 01 to 12) | | + * | %%M | Minute as a decimal number (range 00 to 59) | | + * | %%n | Newline | | + * | %%p | Meridiem indicator: AM or PM. Midnight is AM and noon is PM | | + * | %%r | Locale 12-hour clock time notation. This is equivalent to %%I:%%M:%%S %%p | * | + * | %%R | Time in 24-hour notation (HH:MM) | | + * | %%S | Second as a decimal number (range 00 to 60) | | + * | %%t | Horizontal tab | | + * | %%T | Time in 24-hour notation (HH:MM:SS) | | + * | %%u | Day of the week as a decimal (range 1 to 7, Monday is 1) | | + * | (%%U) | Week number as a decimal number (range 00 to 53, first Sunday as the first day of week 01) | | + * | (%%V) | ISO 8601 week number as a decimal number (range 01 to 53, where week 1 is the first week including a Thursday) | | + * | %%w | Day of the week as a decimal (range 0 to 6, Sunday is 0) | | + * | (%%W) | Week number as a decimal number (range 00 to 53, first Monday as the first day of week 01) | | + * | %%x | Locale preferred date representation | * | + * | %%X | Locale preferred time representation | * | + * | %%y | Year as a decimal number without a century (range 00 to 99) | | + * | %%Y | The year as a decimal number (range 1970 to ...) | | + * | %% | Percent sign | | */ String format(String format); diff --git a/Sming/SmingCore/SmingCore.h b/Sming/SmingCore/SmingCore.h old mode 100755 new mode 100644 From cf993960bcb3e037c8e8e7c9dca3b457ecba2e7f Mon Sep 17 00:00:00 2001 From: riban-bw Date: Tue, 11 Dec 2018 12:32:11 +0000 Subject: [PATCH 06/16] Corrects path to DateTime.h in various libs. Fixes coding style. --- Sming/SmingCore/DateTime.cpp | 265 +++++++++--------- Sming/SmingCore/DateTime.h | 78 +++--- Sming/SmingCore/Digital.h | 2 +- Sming/SmingCore/Network/Http/HttpConnection.h | 2 +- .../Network/Http/HttpConnectionBase.cpp | 2 +- Sming/SmingCore/Network/MqttClient.h | 2 +- Sming/SmingCore/Network/NtpClient.h | 2 +- Sming/SmingCore/SPI.cpp | 18 +- Sming/SmingCore/SmingCore.h | 2 +- Sming/SmingCore/SystemClock.h | 2 +- Sming/SmingCore/Wire.cpp | 2 +- samples/Basic_DateTime/app/application.cpp | 71 +++-- samples/Basic_Delegates/app/application.cpp | 4 +- samples/Basic_ProgMem/app/TestProgmem.cpp | 5 +- samples/SDCard/app/application.cpp | 2 +- 15 files changed, 228 insertions(+), 231 deletions(-) mode change 100755 => 100644 Sming/SmingCore/DateTime.cpp mode change 100755 => 100644 Sming/SmingCore/DateTime.h mode change 100755 => 100644 samples/Basic_DateTime/app/application.cpp diff --git a/Sming/SmingCore/DateTime.cpp b/Sming/SmingCore/DateTime.cpp old mode 100755 new mode 100644 index 692b404bb6..9e8e4a5d3f --- a/Sming/SmingCore/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -75,8 +75,7 @@ bool DateTime::fromHttpDate(const String& httpDate) auto parseNumber = [&ptr]() { return strtol(ptr, const_cast(&ptr), 10); }; char dayName[] = {ptr[0], ptr[1], ptr[2], '\0'}; - for(DayofWeek = 0; DayofWeek < 7; ++DayofWeek) - { + for(DayofWeek = 0; DayofWeek < 7; ++DayofWeek) { if(strncmp(CStringArray(flashDayNames)[DayofWeek], dayName, 3) == 0) break; } @@ -94,8 +93,7 @@ bool DateTime::fromHttpDate(const String& httpDate) ptr += 4; // Skip space as well as month // Search is case insensitive - for(Month = 0; Month < 12; ++Month) - { + for(Month = 0; Month < 12; ++Month) { if(strncmp(CStringArray(flashMonthNames)[Month], monthName, 3) == 0) break; } @@ -173,7 +171,7 @@ void DateTime::addMilliseconds(long add) } void DateTime::fromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday, - int8_t* pmonth, int16_t* pyear) + int8_t* pmonth, int16_t* pyear) { // convert the given time_t to time components // this is a more compact version of the C library localtime function @@ -253,131 +251,126 @@ String DateTime::format(String sFormat) String sReturn; char buf[64]; //!@todo Check size of buffer is optimal - for(unsigned int pos = 0; pos < sFormat.length(); ++pos) - { - if(sFormat[pos] == '%') - { + for(unsigned int pos = 0; pos < sFormat.length(); ++pos) { + if(sFormat[pos] == '%') { if(pos < sFormat.length() - 1) //Ignore % as last character { buf[0] = '\0'; //Empty string in case no format character matches - switch(sFormat[pos + 1]) + switch(sFormat[pos + 1]) { + //Year (not implemented: EY, Oy, Ey, EC, G, g) + case 'Y': //Full year as a decimal number, e.g. 2018 + m_snprintf(buf, sizeof(buf), _F("%04d"), Year); + break; + case 'y': //Year, last 2 digits as a decimal number [00..99] + m_snprintf(buf, sizeof(buf), _F("%02d"), Year % 100); + break; + case 'C': //Year, first 2 digits as a decimal number [00..99] + m_snprintf(buf, sizeof(buf), _F("%02d"), Year / 100); + break; + //Month (not implemented: Om) + case 'b': //Abbreviated month name, e.g. Oct (always English) + case 'h': //Synonym of b + m_snprintf(buf, 4, _F("%s"), CStringArray(flashMonthNames)[Month]); + break; + case 'B': //Full month name, e.g. October (always English) + m_snprintf(buf, sizeof(buf), _F("%s"), CStringArray(flashMonthNames)[Month]); + break; + case 'm': //Month as a decimal number [01..12] + m_snprintf(buf, sizeof(buf), _F("%02d"), Month + 1); + break; + //Week (not implemented: OU, OW, V, OV) + case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] + //!@todo Implement week number (Sunday as first day of week) + break; + case 'V': //ISO 8601 week number (01-53) + //!@todo Implement ISO 8601 week number + break; + case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] + //!@todo Implement week number (Monday as first day of week) + break; + case 'x': //Locale preferred date format + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE).c_str()); + break; + case 'X': //Locale preferred time format + //!@todo Implement locale + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_TIME).c_str()); + break; + // Day of year/month (Not implemented: Od, Oe) + case 'j': //Day of the year as a decimal number [001..366] { - //Year (not implemented: EY, Oy, Ey, EC, G, g) - case 'Y': //Full year as a decimal number, e.g. 2018 - m_snprintf(buf, sizeof(buf), _F("%04d"), Year); - break; - case 'y': //Year, last 2 digits as a decimal number [00..99] - m_snprintf(buf, sizeof(buf), _F("%02d"), Year%100); - break; - case 'C': //Year, first 2 digits as a decimal number [00..99] - m_snprintf(buf, sizeof(buf), _F("%02d"), Year/100); - break; - //Month (not implemented: Om) - case 'b': //Abbreviated month name, e.g. Oct (always English) - case 'h': //Synonym of b - m_snprintf(buf, 4, _F("%s"), CStringArray(flashMonthNames)[Month]); - break; - case 'B': //Full month name, e.g. October (always English) - m_snprintf(buf, sizeof(buf), _F("%s"), CStringArray(flashMonthNames)[Month]); - break; - case 'm': //Month as a decimal number [01..12] - m_snprintf(buf, sizeof(buf), _F("%02d"), Month + 1); - break; - //Week (not implemented: OU, OW, V, OV) - case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] - //!@todo Implement week number (Sunday as first day of week) - break; - case 'V': //ISO 8601 week number (01-53) - //!@todo Implement ISO 8601 week number - break; - case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] - //!@todo Implement week number (Monday as first day of week) - break; - case 'x': //Locale preferred date format - m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE).c_str()); - break; - case 'X': //Locale preferred time format - //!@todo Implement locale - m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_TIME).c_str()); - break; - // Day of year/month (Not implemented: Od, Oe) - case 'j': //Day of the year as a decimal number [001..366] - { - m_snprintf(buf, sizeof(buf), _F("%03d"), DayofYear); - break; - } - case 'd': //Day of the month as a decimal number [01..31] - m_snprintf(buf, sizeof(buf), _F("%02d"), Day); - break; - case 'e': //Day of the month as a decimal number [ 1,31] - m_snprintf(buf, sizeof(buf), _F("% 2d"), Day); - break; - // Day of week (Not implemented: Ow, Ou) - case 'w': //Weekday as a decimal number with Sunday as 0 [0..6] - m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); - break; - case 'a': //Abbreviated weekday name, e.g. Fri - m_snprintf(buf, 4, _F("%s"), CStringArray(flashDayNames)[DayofWeek]); - break; - case 'A': //Full weekday name, e.g. Friday - m_snprintf(buf, sizeof(buf), _F("%s"), (CStringArray(flashDayNames)[DayofWeek])); - break; - case 'u': //Weekday as a decimal number, where Monday is 1 (ISO 8601 format) [1..7] - m_snprintf(buf, sizeof(buf), _F("%d"), (DayofWeek == 0)?7:DayofWeek); - break; - //Time (not implemented: OH, OI, OM, OS) - case 'H': //Hour as a decimal number, 24 hour clock [00..23] - m_snprintf(buf, sizeof(buf), _F("%02d"), Hour); - break; - case 'I': //Hour as a decimal number, 12 hour clock [0..12] - m_snprintf(buf, sizeof(buf), _F("%02d"), Hour?((Hour > 12)?Hour-12:Hour):12); - break; - case 'M': //Minute as a decimal number [00..59] - m_snprintf(buf, sizeof(buf), _F("%02d"), Minute); - break; - case 'S': //Second as a decimal number [00..61] - m_snprintf(buf, sizeof(buf), _F("%02d"), Second); - break; - // Other (not implemented: Ec, Ex, EX, z, Z) - case 'c': //Locale preferred date and time format, e.g. Tue Dec 11 08:48:32 2018 - m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE_TIME).c_str()); - break; - case 'D': //US date (MM/DD/YY) - m_snprintf(buf, sizeof(buf), _F("%s"), format("%m/%d/%y").c_str()); - break; - case 'F': //ISO 8601 date format (YYYY-mm-dd) - m_snprintf(buf, sizeof(buf), _F("%s"), format("%Y-%m-%d").c_str()); - break; - case 'r': //12-hour clock time (hh:MM:SS AM) - m_snprintf(buf, sizeof(buf), _F("%s"), format("%I:%M:%S %p").c_str()); - break; - case 'R': //Short time (HH:MM) - m_snprintf(buf, sizeof(buf), _F("%02d:%02d"), Hour, Minute); - break; - case 'T': //ISO 8601 time format (HH:MM:SS) - m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); - break; - case 'p': //Meridiem [AM,PM] - m_snprintf(buf, sizeof(buf), _F("%s"), (Hour < 12)?"AM":"PM"); - break; - case '%': //Literal percent (%). The full conversion specification must be %% - m_snprintf(buf, sizeof(buf), _F("%s"), "%"); - break; - case 'n': //Newline character (\n) - m_snprintf(buf, sizeof(buf), _F("%s"), "\n"); - break; - case 't': //Horizontal tab (\t) - m_snprintf(buf, sizeof(buf), _F("%s"), "\t"); - break; - default: //Silently ignore % and process next character - --pos; + m_snprintf(buf, sizeof(buf), _F("%03d"), DayofYear); + break; + } + case 'd': //Day of the month as a decimal number [01..31] + m_snprintf(buf, sizeof(buf), _F("%02d"), Day); + break; + case 'e': //Day of the month as a decimal number [ 1,31] + m_snprintf(buf, sizeof(buf), _F("% 2d"), Day); + break; + // Day of week (Not implemented: Ow, Ou) + case 'w': //Weekday as a decimal number with Sunday as 0 [0..6] + m_snprintf(buf, sizeof(buf), _F("%d"), DayofWeek); + break; + case 'a': //Abbreviated weekday name, e.g. Fri + m_snprintf(buf, 4, _F("%s"), CStringArray(flashDayNames)[DayofWeek]); + break; + case 'A': //Full weekday name, e.g. Friday + m_snprintf(buf, sizeof(buf), _F("%s"), (CStringArray(flashDayNames)[DayofWeek])); + break; + case 'u': //Weekday as a decimal number, where Monday is 1 (ISO 8601 format) [1..7] + m_snprintf(buf, sizeof(buf), _F("%d"), (DayofWeek == 0) ? 7 : DayofWeek); + break; + //Time (not implemented: OH, OI, OM, OS) + case 'H': //Hour as a decimal number, 24 hour clock [00..23] + m_snprintf(buf, sizeof(buf), _F("%02d"), Hour); + break; + case 'I': //Hour as a decimal number, 12 hour clock [0..12] + m_snprintf(buf, sizeof(buf), _F("%02d"), Hour ? ((Hour > 12) ? Hour - 12 : Hour) : 12); + break; + case 'M': //Minute as a decimal number [00..59] + m_snprintf(buf, sizeof(buf), _F("%02d"), Minute); + break; + case 'S': //Second as a decimal number [00..61] + m_snprintf(buf, sizeof(buf), _F("%02d"), Second); + break; + // Other (not implemented: Ec, Ex, EX, z, Z) + case 'c': //Locale preferred date and time format, e.g. Tue Dec 11 08:48:32 2018 + m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE_TIME).c_str()); + break; + case 'D': //US date (MM/DD/YY) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%m/%d/%y").c_str()); + break; + case 'F': //ISO 8601 date format (YYYY-mm-dd) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%Y-%m-%d").c_str()); + break; + case 'r': //12-hour clock time (hh:MM:SS AM) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%I:%M:%S %p").c_str()); + break; + case 'R': //Short time (HH:MM) + m_snprintf(buf, sizeof(buf), _F("%02d:%02d"), Hour, Minute); + break; + case 'T': //ISO 8601 time format (HH:MM:SS) + m_snprintf(buf, sizeof(buf), _F("%s"), format("%H:%M:%S").c_str()); + break; + case 'p': //Meridiem [AM,PM] + m_snprintf(buf, sizeof(buf), _F("%s"), (Hour < 12) ? "AM" : "PM"); + break; + case '%': //Literal percent (%). The full conversion specification must be %% + m_snprintf(buf, sizeof(buf), _F("%s"), "%"); + break; + case 'n': //Newline character (\n) + m_snprintf(buf, sizeof(buf), _F("%s"), "\n"); + break; + case 't': //Horizontal tab (\t) + m_snprintf(buf, sizeof(buf), _F("%s"), "\t"); + break; + default: //Silently ignore % and process next character + --pos; } sReturn += buf; ++pos; //Skip format charachter } - } - else - { + } else { //Normal character sReturn += sFormat[pos]; } @@ -388,21 +381,19 @@ String DateTime::format(String sFormat) void DateTime::calcDayOfYear() { DayofYear = 0; - for(unsigned int i = 0; i < Month; ++i) - { - switch(i) - { - case 8: //Sep - case 3: //Apr - case 5: //Jun - case 10: //Nov - DayofYear += 30; - break; - case 1: //Feb - DayofYear += LEAP_YEAR(Year)?29:28; - break; - default: - DayofYear += 31; + for(unsigned int i = 0; i < Month; ++i) { + switch(i) { + case 8: //Sep + case 3: //Apr + case 5: //Jun + case 10: //Nov + DayofYear += 30; + break; + case 1: //Feb + DayofYear += LEAP_YEAR(Year) ? 29 : 28; + break; + default: + DayofYear += 31; } } DayofYear += Day; diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h old mode 100755 new mode 100644 index 1b364447bf..5cba9213ab --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -22,13 +22,13 @@ /*==============================================================================*/ /* Useful Constants */ -#define SECS_PER_MIN (60UL) +#define SECS_PER_MIN (60UL) #define SECS_PER_HOUR (3600UL) -#define SECS_PER_DAY (SECS_PER_HOUR * 24L) +#define SECS_PER_DAY (SECS_PER_HOUR * 24L) #define DAYS_PER_WEEK (7L) #define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK) #define SECS_PER_YEAR (SECS_PER_WEEK * 52L) -#define SECS_YR_2000 (946681200UL) +#define SECS_YR_2000 (946681200UL) /* Useful Macros for getting elapsed time */ /** Get just seconds part of given Unix time */ @@ -36,24 +36,27 @@ /** Get just minutes part of given Unix time */ #define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) /** Get just hours part of given Unix time */ -#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) +#define numberOfHours(_time_) ((_time_ % SECS_PER_DAY) / SECS_PER_HOUR) /** Get day of week from given Unix time */ -#define dayOfWeek(_time_) (( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK) // 0 = Sunday +#define dayOfWeek(_time_) ((_time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK) // 0 = Sunday /** Get elapsed days since 1970-01-01 from given Unix time */ -#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 +#define elapsedDays(_time_) (_time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 /** Get quantity of seconds since midnight from given Unix time */ -#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight +#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight /** Get Unix time of midnight at start of day from given Unix time */ -#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day +#define previousMidnight(_time_) ((_time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day /** Get Unix time of midnight at end of day from given just Unix time */ -#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day +#define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day /** Get quantity of seconds since midnight at start of previous Sunday from given Unix time */ -#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY) ) +#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY)) /** Define default locale settings as en_GB:en */ #ifndef LOCALE -#define LOCALE en_GB:en -#define LOCALE_MONTH_NAMES "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" +#define LOCALE \ + en_GB: \ + en +#define LOCALE_MONTH_NAMES \ + "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" #define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" #define LOCALE_DATE "%d/%m/%Y" #define LOCALE_TIME "%H:%M:%S" @@ -66,13 +69,13 @@ /** @brief Days of week */ typedef enum { - dtSunday, ///< Sunday - dtMonday, ///< Monday - dtTuesday, ///< Tuesday - dtWednesday, ///< Wednesday - dtThursday, ///< Thursday - dtFriday, ///< Friday - dtSaturday ///< Saturday + dtSunday, ///< Sunday + dtMonday, ///< Monday + dtTuesday, ///< Tuesday + dtWednesday, ///< Wednesday + dtThursday, ///< Thursday + dtFriday, ///< Friday + dtSaturday ///< Saturday } dtDays_t; /** @brief Date and time class @@ -100,9 +103,12 @@ class DateTime /** @brief Get current Unix time * @retval time_t Quantity of seconds since 00:00:00 1970-01-01 */ - operator time_t() { return toUnixTime(); } + operator time_t() + { + return toUnixTime(); + } - /** @brief Set time using Unix timestamp + /** @brief Set time using Unix timestamp * @param time Unix time to set object time to */ void setTime(time_t time); @@ -131,7 +137,7 @@ class DateTime * @note GMT suffix is optional and is always assumed / ignored * @deprecated Use 'fromHttpDate' instead */ - bool parseHttpDate(const String& httpDate) __attribute__ ((deprecated)) + bool parseHttpDate(const String& httpDate) __attribute__((deprecated)) { return fromHttpDate(httpDate); } @@ -195,7 +201,8 @@ class DateTime * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. * @note All of the return values are optional, specify nullptr if not required */ - static void fromUnixTime(time_t timep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear); + static void fromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday, + int8_t* pmonth, int16_t* pyear); // functions to convert to and from time components (hrs, secs, days, years etc) to time_t /** @brief Convert from Unix time to individual time components @@ -215,9 +222,10 @@ class DateTime * @note All of the return values are optional, specify nullptr if not required * @deprecated Use 'fromUnixTime' instead */ - static void convertFromUnixTime(time_t tImep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear) __attribute__ ((deprecated)) + static void convertFromUnixTime(time_t tImep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, + int8_t* pwday, int8_t* pmonth, int16_t* pyear) __attribute__((deprecated)) { - return fromUnixTime( tImep, psec, pmin, phour, pday, pwday, pmonth, pyear); + return fromUnixTime(tImep, psec, pmin, phour, pday, pwday, pmonth, pyear); } /** @brief Convert from individual time components to Unix time @@ -247,7 +255,8 @@ class DateTime * @note Unix time does not account for leap seconds. To convert Unix time to UTC requires reference to a leap second table. * @deprecated Use 'toUnixTime' instead */ - static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) __attribute__ ((deprecated)) + static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year) + __attribute__((deprecated)) { return toUnixTime(sec, min, hour, day, month, year); } @@ -300,17 +309,16 @@ class DateTime void calcDayOfYear(); public: - int8_t Hour = 0; ///< Hour (0-23) - int8_t Minute = 0; ///< Minute (0-59) - int8_t Second = 0; ///< Second (0-59) + int8_t Hour = 0; ///< Hour (0-23) + int8_t Minute = 0; ///< Minute (0-59) + int8_t Second = 0; ///< Second (0-59) int16_t Milliseconds = 0; ///< Milliseconds (0-999) - int8_t Day = 0; ///< Day of month (1-31) - int8_t DayofWeek = 0; ///< Day of week (0-6 Sunday is day 0) - int16_t DayofYear = 0; ///< Day of year (0-365) - int8_t Month = 0; ///< Month (0-11 Jan is month 0) - int16_t Year = 0; ///< Full Year number + int8_t Day = 0; ///< Day of month (1-31) + int8_t DayofWeek = 0; ///< Day of week (0-6 Sunday is day 0) + int16_t DayofYear = 0; ///< Day of year (0-365) + int8_t Month = 0; ///< Month (0-11 Jan is month 0) + int16_t Year = 0; ///< Full Year number }; /** @} */ #endif /* DateTime_h */ - diff --git a/Sming/SmingCore/Digital.h b/Sming/SmingCore/Digital.h index 01e0cce14a..4c4eef8c60 100644 --- a/Sming/SmingCore/Digital.h +++ b/Sming/SmingCore/Digital.h @@ -69,6 +69,6 @@ inline uint16_t analogRead(uint16_t pin) return -1; // Not supported } - /** @} */ +/** @} */ #endif diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index 1e2312d04b..571deaa29c 100755 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -14,7 +14,7 @@ #define _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ #include "HttpConnectionBase.h" -#include +#include #include "Data/ObjectQueue.h" /** @defgroup HTTP client connection diff --git a/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp b/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp index 7fe319e4e8..2a90246cbc 100644 --- a/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp +++ b/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp @@ -44,7 +44,7 @@ void HttpConnectionBase::init(http_parser_type type) #endif parserSettings.on_url = staticOnPath; - // Data callbacks: on_url, (common) on_header_field, on_header_value, on_body; +// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body; #ifndef COMPACT_MODE parserSettings.on_status = staticOnStatus; #endif diff --git a/Sming/SmingCore/Network/MqttClient.h b/Sming/SmingCore/Network/MqttClient.h index 201d246efd..635a5440cc 100644 --- a/Sming/SmingCore/Network/MqttClient.h +++ b/Sming/SmingCore/Network/MqttClient.h @@ -309,7 +309,7 @@ class MqttClient : protected TcpClient // client flags uint8_t flags = 0; - /* 7 8 6 5 4 3 2 1 0 +/* 7 8 6 5 4 3 2 1 0 * | * --- set when connected ... */ diff --git a/Sming/SmingCore/Network/NtpClient.h b/Sming/SmingCore/Network/NtpClient.h index 4dac5aebde..3d3b295555 100755 --- a/Sming/SmingCore/Network/NtpClient.h +++ b/Sming/SmingCore/Network/NtpClient.h @@ -18,7 +18,7 @@ #include "UdpConnection.h" #include "Platform/System.h" #include "Timer.h" -#include +#include #include "Delegate.h" #define NTP_PORT 123 diff --git a/Sming/SmingCore/SPI.cpp b/Sming/SmingCore/SPI.cpp index b0b8af9e35..804ed16c68 100644 --- a/Sming/SmingCore/SPI.cpp +++ b/Sming/SmingCore/SPI.cpp @@ -119,8 +119,9 @@ uint32 SPIClass::transfer32(uint32 data, uint8 bits) regvalue |= SPI_USR_MOSI | SPI_DOUTDIN | SPI_CK_I_EDGE; WRITE_PERI_REG(SPI_USER(SPI_NO), regvalue); - WRITE_PERI_REG(SPI_USER1(SPI_NO), (((bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | - (((bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); + WRITE_PERI_REG(SPI_USER1(SPI_NO), + (((bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | + (((bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); // copy data to W0 if(READ_PERI_REG(SPI_USER(SPI_NO)) & SPI_WR_BYTE_ORDER) { @@ -214,8 +215,9 @@ void SPIClass::transfer(uint8* buffer, size_t numberBytes) WRITE_PERI_REG(SPI_USER(SPI_NO), regvalue); // setup bit lenght - WRITE_PERI_REG(SPI_USER1(SPI_NO), (((num_bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | - (((num_bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); + WRITE_PERI_REG(SPI_USER1(SPI_NO), + (((num_bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | + (((num_bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); // copy the registers starting from last index position memcpy((void*)SPI_W0(SPI_NO), &buffer[bufIndx], bufLength); @@ -355,10 +357,10 @@ void SPIClass::setClock(uint8 prediv, uint8 cntdiv) // go full speed = SYSTEMCLOCK WRITE_PERI_REG(SPI_CLOCK(SPI_NO), SPI_CLK_EQU_SYSCLK); } else { - WRITE_PERI_REG(SPI_CLOCK(SPI_NO), (((prediv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) | - (((cntdiv - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) | - (((cntdiv >> 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) | - ((0 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); + WRITE_PERI_REG(SPI_CLOCK(SPI_NO), + (((prediv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) | + (((cntdiv - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) | + (((cntdiv >> 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) | ((0 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); } } diff --git a/Sming/SmingCore/SmingCore.h b/Sming/SmingCore/SmingCore.h index 1c43047cbe..b2a5cc2dd6 100644 --- a/Sming/SmingCore/SmingCore.h +++ b/Sming/SmingCore/SmingCore.h @@ -55,7 +55,7 @@ #include "Data/Stream/FileStream.h" #include "Data/Stream/TemplateFileStream.h" -#include "DateTime/DateTime.h" +#include "DateTime.h" #include "../Services/FATFS/ff.h" #include "../Services/Yeelight/YeelightBulb.h" diff --git a/Sming/SmingCore/SystemClock.h b/Sming/SmingCore/SystemClock.h index ed57983d73..d79869d0d8 100755 --- a/Sming/SmingCore/SystemClock.h +++ b/Sming/SmingCore/SystemClock.h @@ -13,7 +13,7 @@ #ifndef SMINGCORE_SYSTEMCLOCK_H_ #define SMINGCORE_SYSTEMCLOCK_H_ -#include +#include #include "WString.h" /** @addtogroup constants diff --git a/Sming/SmingCore/Wire.cpp b/Sming/SmingCore/Wire.cpp index 3cfcee67b1..b04c587fae 100644 --- a/Sming/SmingCore/Wire.cpp +++ b/Sming/SmingCore/Wire.cpp @@ -271,7 +271,7 @@ void TwoWire::onRequest(void (*function)(void)) //user_onRequest = function; } - // Preinstantiate Objects ////////////////////////////////////////////////////// +// Preinstantiate Objects ////////////////////////////////////////////////////// #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE) TwoWire Wire; diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp old mode 100755 new mode 100644 index 2a482b8f2f..9456654061 --- a/samples/Basic_DateTime/app/application.cpp +++ b/samples/Basic_DateTime/app/application.cpp @@ -29,7 +29,7 @@ void showTime(time_t timestamp) //Week Serial.println(dt.format("%%U Week number with the first Sunday as the first day of week one (00-53): %U")); //NYI Serial.println(dt.format("%%W Week number with the first Monday as the first day of week one (00-53): %W")); //NYI - Serial.println(dt.format("%%V ISO 8601 week number (01-53): %V")); //NYI + Serial.println(dt.format("%%V ISO 8601 week number (01-53): %V")); //NYI //Day Serial.println(dt.format("%%j Day of the year (001-366): %j")); Serial.println(dt.format("%%d Day of the month, zero-padded (01-31)%: %d")); @@ -70,46 +70,43 @@ void showTime(time_t timestamp) Serial.println(dt.toShortDateString()); Serial.print("toShortTimeString: "); Serial.println(dt.toShortTimeString()); - } void onRx(Stream& source, char arrivedChar, unsigned short availableCharsCount) { - switch(arrivedChar) - { - case '\n': - Serial.println(); - Serial.println(); - Serial.print("****Showing DateTime formating options for Unix timestamp: "); - Serial.println(g_timestamp); - showTime(g_timestamp); - Serial.print("Enter Unix timestamp: "); - g_timestamp = 0; - g_tsLength = 0; - break; - case '0' ... '9': - g_timestamp *= 10; - g_timestamp += arrivedChar - '0'; - ++g_tsLength; - Serial.print(arrivedChar); - break; - case '\b': - if(g_tsLength) - { - Serial.print('\b'); - Serial.print(" "); - Serial.print('\b'); - --g_tsLength; - g_timestamp /= 10; - } - break; - case 27: - g_timestamp = 0; - g_tsLength = 0; - Serial.print('\r'); - Serial.print(" "); - Serial.print('\r'); - Serial.print("Enter Unix timestamp: "); + switch(arrivedChar) { + case '\n': + Serial.println(); + Serial.println(); + Serial.print("****Showing DateTime formating options for Unix timestamp: "); + Serial.println(g_timestamp); + showTime(g_timestamp); + Serial.print("Enter Unix timestamp: "); + g_timestamp = 0; + g_tsLength = 0; + break; + case '0' ... '9': + g_timestamp *= 10; + g_timestamp += arrivedChar - '0'; + ++g_tsLength; + Serial.print(arrivedChar); + break; + case '\b': + if(g_tsLength) { + Serial.print('\b'); + Serial.print(" "); + Serial.print('\b'); + --g_tsLength; + g_timestamp /= 10; + } + break; + case 27: + g_timestamp = 0; + g_tsLength = 0; + Serial.print('\r'); + Serial.print(" "); + Serial.print('\r'); + Serial.print("Enter Unix timestamp: "); } } diff --git a/samples/Basic_Delegates/app/application.cpp b/samples/Basic_Delegates/app/application.cpp index 5526ad6312..6569afd68f 100644 --- a/samples/Basic_Delegates/app/application.cpp +++ b/samples/Basic_Delegates/app/application.cpp @@ -53,8 +53,8 @@ class Task taskTimer .initializeMs( taskInterval, - [foo] // capture just foo by value (Note it would be bad to pass by reference as foo would be out of scope when the lamda function runs later) - () // No parameters to the callback + [foo] // capture just foo by value (Note it would be bad to pass by reference as foo would be out of scope when the lamda function runs later) + () // No parameters to the callback -> void // Returns nothing { if(foo == 123) { diff --git a/samples/Basic_ProgMem/app/TestProgmem.cpp b/samples/Basic_ProgMem/app/TestProgmem.cpp index 6ffede9d3c..9790fdd136 100644 --- a/samples/Basic_ProgMem/app/TestProgmem.cpp +++ b/samples/Basic_ProgMem/app/TestProgmem.cpp @@ -123,7 +123,7 @@ void testFSTR(Print& out) } out.println("}"); - // Test equality operators +// Test equality operators #define TEST(_test) out.printf(_F("%s: %s\n"), (_test) ? _F("PASS") : _F("FAIL"), _F(#_test)); TEST(demoFSTR1 == demoFSTR2) TEST(demoFSTR1 != _FLOAD(demoPSTR1)) @@ -137,8 +137,7 @@ void testFSTR(Print& out) static DEFINE_FSTR(fstr2, "Test string #2"); static FSTR_TABLE(table) = { - FSTR_PTR(fstr1), - FSTR_PTR(fstr2), + FSTR_PTR(fstr1), FSTR_PTR(fstr2), }; // Table entries may be accessed directly as they are word-aligned diff --git a/samples/SDCard/app/application.cpp b/samples/SDCard/app/application.cpp index b5cfd128b2..90c61c1833 100644 --- a/samples/SDCard/app/application.cpp +++ b/samples/SDCard/app/application.cpp @@ -106,7 +106,7 @@ void stat_file(char* fname) } FRESULT ls(const char* path /* Start node to be scanned (also used as work area) */ -) + ) { FRESULT res; FILINFO fno; From 0d2bef11c4c3fa6f875a359fc7a0acd323bde5e1 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Tue, 11 Dec 2018 13:40:02 +0000 Subject: [PATCH 07/16] Updates comments --- Sming/SmingCore/DateTime.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) mode change 100644 => 100755 Sming/SmingCore/DateTime.cpp diff --git a/Sming/SmingCore/DateTime.cpp b/Sming/SmingCore/DateTime.cpp old mode 100644 new mode 100755 index 9e8e4a5d3f..f33788c9cd --- a/Sming/SmingCore/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -247,9 +247,8 @@ time_t DateTime::toUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int String DateTime::format(String sFormat) { - //!@todo Add localisation (maybe via pre-compiler conditions) String sReturn; - char buf[64]; //!@todo Check size of buffer is optimal + char buf[64]; //64 characters should be sufficient for most locale for(unsigned int pos = 0; pos < sFormat.length(); ++pos) { if(sFormat[pos] == '%') { @@ -292,7 +291,6 @@ String DateTime::format(String sFormat) m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE).c_str()); break; case 'X': //Locale preferred time format - //!@todo Implement locale m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_TIME).c_str()); break; // Day of year/month (Not implemented: Od, Oe) From bbf91292977a5c33458ec72f1a18fcc50f547704 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Tue, 11 Dec 2018 21:59:11 +0000 Subject: [PATCH 08/16] Fixes previous commit errors. Implements %U week number. --- Sming/SmingCore/DateTime.cpp | 9 +++- Sming/SmingCore/DateTime.h | 6 +-- Sming/SmingCore/Locale.h | 48 +++++++++++++++++++ .../SmingCore/Network/Http/HttpConnection.cpp | 0 .../Network/Http/HttpConnectionBase.cpp | 0 Sming/SmingCore/Network/NtpClient.h | 2 +- Sming/SmingCore/SystemClock.h | 2 +- samples/Basic_DateTime/.cproject | 4 +- samples/Basic_DateTime/.project | 2 +- samples/Basic_DateTime/app/application.cpp | 30 ++++++------ samples/SDCard/app/application.cpp | 0 11 files changed, 78 insertions(+), 25 deletions(-) mode change 100644 => 100755 Sming/SmingCore/DateTime.h create mode 100755 Sming/SmingCore/Locale.h mode change 100755 => 100644 Sming/SmingCore/Network/Http/HttpConnection.cpp mode change 100644 => 100755 Sming/SmingCore/Network/Http/HttpConnectionBase.cpp mode change 100644 => 100755 samples/Basic_DateTime/app/application.cpp mode change 100644 => 100755 samples/SDCard/app/application.cpp diff --git a/Sming/SmingCore/DateTime.cpp b/Sming/SmingCore/DateTime.cpp index f33788c9cd..800c5fe66c 100755 --- a/Sming/SmingCore/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -279,8 +279,15 @@ String DateTime::format(String sFormat) break; //Week (not implemented: OU, OW, V, OV) case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] - //!@todo Implement week number (Sunday as first day of week) + { + int16_t sunday = DayofYear - DayofWeek; + int8_t firstSunday = sunday % 7; + if(firstSunday < 0) + firstSunday += 7; + int16_t weekNumber = (sunday + 7 - firstSunday) / 7; + m_snprintf(buf, sizeof(buf), _F("%03d"), weekNumber); break; + } case 'V': //ISO 8601 week number (01-53) //!@todo Implement ISO 8601 week number break; diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h old mode 100644 new mode 100755 index 5cba9213ab..d62b09283d --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -52,9 +52,7 @@ /** Define default locale settings as en_GB:en */ #ifndef LOCALE -#define LOCALE \ - en_GB: \ - en +#define LOCALE LOCALE_EN_GB #define LOCALE_MONTH_NAMES \ "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" #define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" @@ -293,7 +291,7 @@ class DateTime * | %%t | Horizontal tab | | * | %%T | Time in 24-hour notation (HH:MM:SS) | | * | %%u | Day of the week as a decimal (range 1 to 7, Monday is 1) | | - * | (%%U) | Week number as a decimal number (range 00 to 53, first Sunday as the first day of week 01) | | + * | %%U | Week number as a decimal number (range 00 to 53, first Sunday as the first day of week 01) | | * | (%%V) | ISO 8601 week number as a decimal number (range 01 to 53, where week 1 is the first week including a Thursday) | | * | %%w | Day of the week as a decimal (range 0 to 6, Sunday is 0) | | * | (%%W) | Week number as a decimal number (range 00 to 53, first Monday as the first day of week 01) | | diff --git a/Sming/SmingCore/Locale.h b/Sming/SmingCore/Locale.h new file mode 100755 index 0000000000..dddd21b23e --- /dev/null +++ b/Sming/SmingCore/Locale.h @@ -0,0 +1,48 @@ +#ifndef LOCALE_H_INCLUDED +#define LOCALE_H_INCLUDED + +//Define unique values for each locale (try to use ISD codes if appropriate) +#define LOCALE_EN_GB 44 +#define LOCALE_EN_AU 61 +#define LOCALE_EN_US 1 +#define LOCALE_DE_DE 49 +#define LOCALE_FR_FR 33 + +//Default values (en-gb) +#define LOCALE_MONTH_NAMES \ + "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" +#define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" +#define LOCALE_DATE "%d/%m/%Y" +#define LOCALE_TIME "%H:%M:%S" +#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" + +#ifndef LOCALE +#define LOCALE LOCALE_EN_GB +#endif // LOCALE + +//Austailia +#ifdef LOCALE LOCALE_EN_AU +#endif // LOCALE_EN_AU + +//USA +#ifdef LOCALE LOCALE_EN_US +#define LOCALE_DATE "%m/%d/%Y" +#endif // LOCALE_EN_US + +//Germany +#ifdef LOCALE LOCALE_DE_DE +#define LOCALE_MONTH_NAMES \ + "Januar\0Februar\0M�rz\0April\0Mai\0Juni\0Juli\0August\0September\0Oktober\0November\0Dezember" +#define LOCALE_DAY_NAMES "Sonntag\0Montag\0Dienstag\0Mittwoch\0Donnerstag\0Freitag\0Samstag" +#define LOCALE_DATE "%d.%m.%Y" +#endif // LOCALE_DE_DE + +//France +#ifdef LOCALE LOCALE_FR_FR +#define LOCALE_MONTH_NAMES \ + "janvier\0f�vrier\0mars\0avril\0mai\0juin\0juillet\0ao�t\0septembre\0octobre\0novembre\0d�cembre" +#define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" +#define LOCALE_DATE "%d-%m-%Y" +#endif // LOCALE_FR_FR + +#endif // LOCALE_H_INCLUDED diff --git a/Sming/SmingCore/Network/Http/HttpConnection.cpp b/Sming/SmingCore/Network/Http/HttpConnection.cpp old mode 100755 new mode 100644 diff --git a/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp b/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp old mode 100644 new mode 100755 diff --git a/Sming/SmingCore/Network/NtpClient.h b/Sming/SmingCore/Network/NtpClient.h index 3d3b295555..e6dd627732 100755 --- a/Sming/SmingCore/Network/NtpClient.h +++ b/Sming/SmingCore/Network/NtpClient.h @@ -18,7 +18,7 @@ #include "UdpConnection.h" #include "Platform/System.h" #include "Timer.h" -#include +#include "DateTime.h" #include "Delegate.h" #define NTP_PORT 123 diff --git a/Sming/SmingCore/SystemClock.h b/Sming/SmingCore/SystemClock.h index d79869d0d8..23a4981236 100755 --- a/Sming/SmingCore/SystemClock.h +++ b/Sming/SmingCore/SystemClock.h @@ -13,7 +13,7 @@ #ifndef SMINGCORE_SYSTEMCLOCK_H_ #define SMINGCORE_SYSTEMCLOCK_H_ -#include +#include "DateTime.h" #include "WString.h" /** @addtogroup constants diff --git a/samples/Basic_DateTime/.cproject b/samples/Basic_DateTime/.cproject index 67c056d24e..ed202cb727 100644 --- a/samples/Basic_DateTime/.cproject +++ b/samples/Basic_DateTime/.cproject @@ -64,12 +64,12 @@ - + - + diff --git a/samples/Basic_DateTime/.project b/samples/Basic_DateTime/.project index 045ddeccff..c3d5ab9a58 100644 --- a/samples/Basic_DateTime/.project +++ b/samples/Basic_DateTime/.project @@ -1,6 +1,6 @@ - Basic_Blink + Basic_DateTime SmingFramework diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp old mode 100644 new mode 100755 index 9456654061..4663226b67 --- a/samples/Basic_DateTime/app/application.cpp +++ b/samples/Basic_DateTime/app/application.cpp @@ -7,8 +7,8 @@ #include #include -time_t g_timestamp = 0; -size_t g_tsLength = 0; +time_t timestamp = 0; +size_t tsLength = 0; void showTime(time_t timestamp) { @@ -42,7 +42,7 @@ void showTime(time_t timestamp) Serial.println(dt.format("%%p Meridiem (AM|PM): %p")); Serial.println(dt.format("%%H Hour in 24h format (00-23): %H")); Serial.println(dt.format("%%h Hour in 12h format (01-12): %I")); - //Mnute + //Minute Serial.println(dt.format("%%M Minute (00-59): %M")); //Second Serial.println(dt.format("%%S Second (00-61): %S")); @@ -79,30 +79,30 @@ void onRx(Stream& source, char arrivedChar, unsigned short availableCharsCount) Serial.println(); Serial.println(); Serial.print("****Showing DateTime formating options for Unix timestamp: "); - Serial.println(g_timestamp); - showTime(g_timestamp); + Serial.println(timestamp); + showTime(timestamp); Serial.print("Enter Unix timestamp: "); - g_timestamp = 0; - g_tsLength = 0; + timestamp = 0; + tsLength = 0; break; case '0' ... '9': - g_timestamp *= 10; - g_timestamp += arrivedChar - '0'; - ++g_tsLength; + timestamp *= 10; + timestamp += arrivedChar - '0'; + ++tsLength; Serial.print(arrivedChar); break; case '\b': - if(g_tsLength) { + if(tsLength) { Serial.print('\b'); Serial.print(" "); Serial.print('\b'); - --g_tsLength; - g_timestamp /= 10; + --tsLength; + timestamp /= 10; } break; case 27: - g_timestamp = 0; - g_tsLength = 0; + timestamp = 0; + tsLength = 0; Serial.print('\r'); Serial.print(" "); Serial.print('\r'); diff --git a/samples/SDCard/app/application.cpp b/samples/SDCard/app/application.cpp old mode 100644 new mode 100755 From 0e35a3a31c63aab47ef8d85438a2ce0bbd4b54c8 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Tue, 11 Dec 2018 22:14:52 +0000 Subject: [PATCH 09/16] Revert erroneous changes --- Sming/SmingCore/Network/Http/HttpConnection.h | 0 .../Network/Http/HttpConnectionBase.cpp | 2 +- Sming/SmingCore/Network/MqttClient.h | 2 +- Sming/SmingCore/SPI.cpp | 18 ++++++++---------- Sming/SmingCore/Wire.cpp | 2 +- samples/Basic_Delegates/app/application.cpp | 4 ++-- samples/Basic_ProgMem/app/TestProgmem.cpp | 5 +++-- 7 files changed, 16 insertions(+), 17 deletions(-) mode change 100755 => 100644 Sming/SmingCore/Network/Http/HttpConnection.h mode change 100755 => 100644 Sming/SmingCore/Network/Http/HttpConnectionBase.cpp diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h old mode 100755 new mode 100644 diff --git a/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp b/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp old mode 100755 new mode 100644 index 2a90246cbc..7fe319e4e8 --- a/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp +++ b/Sming/SmingCore/Network/Http/HttpConnectionBase.cpp @@ -44,7 +44,7 @@ void HttpConnectionBase::init(http_parser_type type) #endif parserSettings.on_url = staticOnPath; -// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body; + // Data callbacks: on_url, (common) on_header_field, on_header_value, on_body; #ifndef COMPACT_MODE parserSettings.on_status = staticOnStatus; #endif diff --git a/Sming/SmingCore/Network/MqttClient.h b/Sming/SmingCore/Network/MqttClient.h index 635a5440cc..201d246efd 100644 --- a/Sming/SmingCore/Network/MqttClient.h +++ b/Sming/SmingCore/Network/MqttClient.h @@ -309,7 +309,7 @@ class MqttClient : protected TcpClient // client flags uint8_t flags = 0; -/* 7 8 6 5 4 3 2 1 0 + /* 7 8 6 5 4 3 2 1 0 * | * --- set when connected ... */ diff --git a/Sming/SmingCore/SPI.cpp b/Sming/SmingCore/SPI.cpp index 804ed16c68..b0b8af9e35 100644 --- a/Sming/SmingCore/SPI.cpp +++ b/Sming/SmingCore/SPI.cpp @@ -119,9 +119,8 @@ uint32 SPIClass::transfer32(uint32 data, uint8 bits) regvalue |= SPI_USR_MOSI | SPI_DOUTDIN | SPI_CK_I_EDGE; WRITE_PERI_REG(SPI_USER(SPI_NO), regvalue); - WRITE_PERI_REG(SPI_USER1(SPI_NO), - (((bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | - (((bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); + WRITE_PERI_REG(SPI_USER1(SPI_NO), (((bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | + (((bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); // copy data to W0 if(READ_PERI_REG(SPI_USER(SPI_NO)) & SPI_WR_BYTE_ORDER) { @@ -215,9 +214,8 @@ void SPIClass::transfer(uint8* buffer, size_t numberBytes) WRITE_PERI_REG(SPI_USER(SPI_NO), regvalue); // setup bit lenght - WRITE_PERI_REG(SPI_USER1(SPI_NO), - (((num_bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | - (((num_bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); + WRITE_PERI_REG(SPI_USER1(SPI_NO), (((num_bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S) | + (((num_bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S)); // copy the registers starting from last index position memcpy((void*)SPI_W0(SPI_NO), &buffer[bufIndx], bufLength); @@ -357,10 +355,10 @@ void SPIClass::setClock(uint8 prediv, uint8 cntdiv) // go full speed = SYSTEMCLOCK WRITE_PERI_REG(SPI_CLOCK(SPI_NO), SPI_CLK_EQU_SYSCLK); } else { - WRITE_PERI_REG(SPI_CLOCK(SPI_NO), - (((prediv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) | - (((cntdiv - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) | - (((cntdiv >> 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) | ((0 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); + WRITE_PERI_REG(SPI_CLOCK(SPI_NO), (((prediv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) | + (((cntdiv - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) | + (((cntdiv >> 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) | + ((0 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)); } } diff --git a/Sming/SmingCore/Wire.cpp b/Sming/SmingCore/Wire.cpp index b04c587fae..3cfcee67b1 100644 --- a/Sming/SmingCore/Wire.cpp +++ b/Sming/SmingCore/Wire.cpp @@ -271,7 +271,7 @@ void TwoWire::onRequest(void (*function)(void)) //user_onRequest = function; } -// Preinstantiate Objects ////////////////////////////////////////////////////// + // Preinstantiate Objects ////////////////////////////////////////////////////// #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE) TwoWire Wire; diff --git a/samples/Basic_Delegates/app/application.cpp b/samples/Basic_Delegates/app/application.cpp index 6569afd68f..5526ad6312 100644 --- a/samples/Basic_Delegates/app/application.cpp +++ b/samples/Basic_Delegates/app/application.cpp @@ -53,8 +53,8 @@ class Task taskTimer .initializeMs( taskInterval, - [foo] // capture just foo by value (Note it would be bad to pass by reference as foo would be out of scope when the lamda function runs later) - () // No parameters to the callback + [foo] // capture just foo by value (Note it would be bad to pass by reference as foo would be out of scope when the lamda function runs later) + () // No parameters to the callback -> void // Returns nothing { if(foo == 123) { diff --git a/samples/Basic_ProgMem/app/TestProgmem.cpp b/samples/Basic_ProgMem/app/TestProgmem.cpp index 9790fdd136..6ffede9d3c 100644 --- a/samples/Basic_ProgMem/app/TestProgmem.cpp +++ b/samples/Basic_ProgMem/app/TestProgmem.cpp @@ -123,7 +123,7 @@ void testFSTR(Print& out) } out.println("}"); -// Test equality operators + // Test equality operators #define TEST(_test) out.printf(_F("%s: %s\n"), (_test) ? _F("PASS") : _F("FAIL"), _F(#_test)); TEST(demoFSTR1 == demoFSTR2) TEST(demoFSTR1 != _FLOAD(demoPSTR1)) @@ -137,7 +137,8 @@ void testFSTR(Print& out) static DEFINE_FSTR(fstr2, "Test string #2"); static FSTR_TABLE(table) = { - FSTR_PTR(fstr1), FSTR_PTR(fstr2), + FSTR_PTR(fstr1), + FSTR_PTR(fstr2), }; // Table entries may be accessed directly as they are word-aligned From 9c4b30a9f0b40f0d766309fe4e683c4b5929b743 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Wed, 12 Dec 2018 07:39:22 +0000 Subject: [PATCH 10/16] Reverts erroneously modified files --- Sming/SmingCore/Digital.h | 2 +- Sming/SmingCore/Network/Http/HttpConnection.h | 2 +- samples/SDCard/app/application.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sming/SmingCore/Digital.h b/Sming/SmingCore/Digital.h index 4c4eef8c60..01e0cce14a 100644 --- a/Sming/SmingCore/Digital.h +++ b/Sming/SmingCore/Digital.h @@ -69,6 +69,6 @@ inline uint16_t analogRead(uint16_t pin) return -1; // Not supported } -/** @} */ + /** @} */ #endif diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index 571deaa29c..f0c89cd822 100644 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -14,7 +14,7 @@ #define _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ #include "HttpConnectionBase.h" -#include +#include "DateTime.h" #include "Data/ObjectQueue.h" /** @defgroup HTTP client connection diff --git a/samples/SDCard/app/application.cpp b/samples/SDCard/app/application.cpp index 90c61c1833..b5cfd128b2 100755 --- a/samples/SDCard/app/application.cpp +++ b/samples/SDCard/app/application.cpp @@ -106,7 +106,7 @@ void stat_file(char* fname) } FRESULT ls(const char* path /* Start node to be scanned (also used as work area) */ - ) +) { FRESULT res; FILINFO fno; From 73d6c4ab1db5f39ebbd8078ca7e4bbfa6c3ff888 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Wed, 12 Dec 2018 08:18:33 +0000 Subject: [PATCH 11/16] Use Locale.h for localisation --- Sming/SmingCore/DateTime.h | 12 +----- Sming/SmingCore/Locale.h | 83 ++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h index d62b09283d..4fefdbd8df 100755 --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -19,6 +19,7 @@ #include #include "WString.h" +#include "Locale.h" /*==============================================================================*/ /* Useful Constants */ @@ -50,17 +51,6 @@ /** Get quantity of seconds since midnight at start of previous Sunday from given Unix time */ #define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY)) -/** Define default locale settings as en_GB:en */ -#ifndef LOCALE -#define LOCALE LOCALE_EN_GB -#define LOCALE_MONTH_NAMES \ - "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" -#define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" -#define LOCALE_DATE "%d/%m/%Y" -#define LOCALE_TIME "%H:%M:%S" -#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" -#endif // LOCALE - // todo add date math macros /*============================================================================*/ diff --git a/Sming/SmingCore/Locale.h b/Sming/SmingCore/Locale.h index dddd21b23e..0b990989cb 100755 --- a/Sming/SmingCore/Locale.h +++ b/Sming/SmingCore/Locale.h @@ -1,48 +1,63 @@ +/** Localization is defined within Locale.h +* Each locale has a unique ID (usually its international dial code, e.g. GB=44 +* The default locale is GB and the default values are those used by GB. +* To add a new locale: +* #define LOCALE_xx_yy zz (where xx_yy is the locale identifier and zz is the IDC) +* Override any variation from GB settings within a "#elifdef LOCALE_xx_yy zz" block +* Default settings are at end of file +*/ #ifndef LOCALE_H_INCLUDED #define LOCALE_H_INCLUDED //Define unique values for each locale (try to use ISD codes if appropriate) -#define LOCALE_EN_GB 44 -#define LOCALE_EN_AU 61 #define LOCALE_EN_US 1 -#define LOCALE_DE_DE 49 #define LOCALE_FR_FR 33 - -//Default values (en-gb) -#define LOCALE_MONTH_NAMES \ - "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" -#define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" -#define LOCALE_DATE "%d/%m/%Y" -#define LOCALE_TIME "%H:%M:%S" -#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" +#define LOCALE_EN_GB 44 +#define LOCALE_DE_DE 49 +#define LOCALE_EN_AU 61 #ifndef LOCALE -#define LOCALE LOCALE_EN_GB + #define LOCALE LOCALE_EN_GB #endif // LOCALE -//Austailia -#ifdef LOCALE LOCALE_EN_AU -#endif // LOCALE_EN_AU - -//USA -#ifdef LOCALE LOCALE_EN_US -#define LOCALE_DATE "%m/%d/%Y" -#endif // LOCALE_EN_US - -//Germany -#ifdef LOCALE LOCALE_DE_DE -#define LOCALE_MONTH_NAMES \ +#if LOCALE == LOCALE_EN_US + #define LOCALE_DATE "%m/%d/%Y" +#elif LOCALE == LOCALE_FR_FR + #define LOCALE_MONTH_NAMES \ + "janvier\0f�vrier\0mars\0avril\0mai\0juin\0juillet\0ao�t\0septembre\0octobre\0novembre\0d�cembre" + #define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" + #define LOCALE_DATE "%d-%m-%Y" +#elif LOCALE == LOCALE_DE_DE + #define LOCALE_MONTH_NAMES \ "Januar\0Februar\0M�rz\0April\0Mai\0Juni\0Juli\0August\0September\0Oktober\0November\0Dezember" -#define LOCALE_DAY_NAMES "Sonntag\0Montag\0Dienstag\0Mittwoch\0Donnerstag\0Freitag\0Samstag" -#define LOCALE_DATE "%d.%m.%Y" -#endif // LOCALE_DE_DE + #define LOCALE_DAY_NAMES "Sonntag\0Montag\0Dienstag\0Mittwoch\0Donnerstag\0Freitag\0Samstag" + #define LOCALE_DATE "%d.%m.%Y" +#elif LOCALE == LOCALE_EN_AU + // Austrailia is same as GB +#endif // LOCALE -//France -#ifdef LOCALE LOCALE_FR_FR -#define LOCALE_MONTH_NAMES \ - "janvier\0f�vrier\0mars\0avril\0mai\0juin\0juillet\0ao�t\0septembre\0octobre\0novembre\0d�cembre" -#define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" -#define LOCALE_DATE "%d-%m-%Y" -#endif // LOCALE_FR_FR + +// Defaults (GB) +#ifndef LOCALE_MONTH_NAMES + //Sting array with full month names, starting with January, separated by '\0' + #define LOCALE_MONTH_NAMES \ + "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" +#endif // LOCALE_MONTH_NAMES +#ifndef LOCALE_DAY_NAMES + //Sting array with full day names, starting with Sunday, separated by '\0' + #define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" +#endif // LOCALE_DAY_NAMES +#ifndef LOCALE_DATE + //String with short date format (see DateTime::format for options - this implements %x format) + #define LOCALE_DATE "%d/%m/%Y" +#endif // LOCALE_DATE +#ifndef LOCALE_TIME + //String with time format (see DateTime::format for options - this implements %X format) + #define LOCALE_TIME "%H:%M:%S" +#endif // LOCALE_TIME +#ifndef LOCALE_DATE_TIME + //String with time and date format (see DateTime::format for options - this implements %c format) + #define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" +#endif // LOCALE_DATE_TIME #endif // LOCALE_H_INCLUDED From beecf46308d3f15277dc12f83afd9b3a21bbe866 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Sat, 15 Dec 2018 00:00:21 +0000 Subject: [PATCH 12/16] Implements week number (crued but probably good enough). Corrects coding style. Renames locale header to avoid conflict with locale.h on case insensitive file systems. --- Sming/SmingCore/DateTime.cpp | 25 ++++++------ Sming/SmingCore/DateTime.h | 5 ++- Sming/SmingCore/{Locale.h => SmingLocale.h} | 42 +++++++++++---------- 3 files changed, 39 insertions(+), 33 deletions(-) rename Sming/SmingCore/{Locale.h => SmingLocale.h} (51%) mode change 100755 => 100644 diff --git a/Sming/SmingCore/DateTime.cpp b/Sming/SmingCore/DateTime.cpp index 800c5fe66c..df76f6d690 100755 --- a/Sming/SmingCore/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -277,22 +277,16 @@ String DateTime::format(String sFormat) case 'm': //Month as a decimal number [01..12] m_snprintf(buf, sizeof(buf), _F("%02d"), Month + 1); break; - //Week (not implemented: OU, OW, V, OV) + //Week (not implemented: OU, OW, OV) case 'U': //Week of the year as a decimal number (Sunday is the first day of the week) [00..53] - { - int16_t sunday = DayofYear - DayofWeek; - int8_t firstSunday = sunday % 7; - if(firstSunday < 0) - firstSunday += 7; - int16_t weekNumber = (sunday + 7 - firstSunday) / 7; - m_snprintf(buf, sizeof(buf), _F("%03d"), weekNumber); + m_snprintf(buf, sizeof(buf), _F("%02d"), calcWeek(0)); break; - } case 'V': //ISO 8601 week number (01-53) - //!@todo Implement ISO 8601 week number + //!@todo Calculation of ISO 8601 week number is crude and frankly wrong but does anyone care? + m_snprintf(buf, sizeof(buf), _F("%02d"), calcWeek(1) + 1); break; case 'W': //Week of the year as a decimal number (Monday is the first day of the week) [00..53] - //!@todo Implement week number (Monday as first day of week) + m_snprintf(buf, sizeof(buf), _F("%02d"), calcWeek(1)); break; case 'x': //Locale preferred date format m_snprintf(buf, sizeof(buf), _F("%s"), format(LOCALE_DATE).c_str()); @@ -403,3 +397,12 @@ void DateTime::calcDayOfYear() } DayofYear += Day; } + +uint8_t DateTime::calcWeek(uint8_t firstDay) +{ + int16_t startOfWeek = DayofYear - DayofWeek + firstDay; + int8_t firstDayofWeek = startOfWeek % 7; + if(firstDayofWeek < 0) + firstDayofWeek += 7; + return (startOfWeek + 7 - firstDayofWeek) / 7; +} diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h index 4fefdbd8df..45cd606c4d 100755 --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -19,7 +19,7 @@ #include #include "WString.h" -#include "Locale.h" +#include "SmingLocale.h" /*==============================================================================*/ /* Useful Constants */ @@ -294,7 +294,8 @@ class DateTime String format(String format); private: - void calcDayOfYear(); + void calcDayOfYear(); // Helper function calculates day of year + uint8_t calcWeek(uint8_t firstDay); //Helper function calculates week number based on firstDay of week public: int8_t Hour = 0; ///< Hour (0-23) diff --git a/Sming/SmingCore/Locale.h b/Sming/SmingCore/SmingLocale.h old mode 100755 new mode 100644 similarity index 51% rename from Sming/SmingCore/Locale.h rename to Sming/SmingCore/SmingLocale.h index 0b990989cb..2d639853b2 --- a/Sming/SmingCore/Locale.h +++ b/Sming/SmingCore/SmingLocale.h @@ -17,47 +17,49 @@ #define LOCALE_EN_AU 61 #ifndef LOCALE - #define LOCALE LOCALE_EN_GB +#define LOCALE LOCALE_EN_GB #endif // LOCALE #if LOCALE == LOCALE_EN_US - #define LOCALE_DATE "%m/%d/%Y" +#define LOCALE_DATE "%m/%d/%Y" + #elif LOCALE == LOCALE_FR_FR - #define LOCALE_MONTH_NAMES \ +#define LOCALE_MONTH_NAMES \ "janvier\0f�vrier\0mars\0avril\0mai\0juin\0juillet\0ao�t\0septembre\0octobre\0novembre\0d�cembre" - #define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" - #define LOCALE_DATE "%d-%m-%Y" +#define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" +#define LOCALE_DATE "%d-%m-%Y" + #elif LOCALE == LOCALE_DE_DE - #define LOCALE_MONTH_NAMES \ +#define LOCALE_MONTH_NAMES \ "Januar\0Februar\0M�rz\0April\0Mai\0Juni\0Juli\0August\0September\0Oktober\0November\0Dezember" - #define LOCALE_DAY_NAMES "Sonntag\0Montag\0Dienstag\0Mittwoch\0Donnerstag\0Freitag\0Samstag" - #define LOCALE_DATE "%d.%m.%Y" +#define LOCALE_DAY_NAMES "Sonntag\0Montag\0Dienstag\0Mittwoch\0Donnerstag\0Freitag\0Samstag" +#define LOCALE_DATE "%d.%m.%Y" + #elif LOCALE == LOCALE_EN_AU - // Austrailia is same as GB +// Austrailia is same as GB #endif // LOCALE - // Defaults (GB) #ifndef LOCALE_MONTH_NAMES - //Sting array with full month names, starting with January, separated by '\0' - #define LOCALE_MONTH_NAMES \ +//Sting array with full month names, starting with January, separated by '\0' +#define LOCALE_MONTH_NAMES \ "January\0February\0March\0April\0May\0June\0July\0August\0September\0October\0November\0December" #endif // LOCALE_MONTH_NAMES #ifndef LOCALE_DAY_NAMES - //Sting array with full day names, starting with Sunday, separated by '\0' - #define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" +//Sting array with full day names, starting with Sunday, separated by '\0' +#define LOCALE_DAY_NAMES "Sunday\0Monday\0Tuesday\0Wednesday\0Thursday\0Friday\0Saturday" #endif // LOCALE_DAY_NAMES #ifndef LOCALE_DATE - //String with short date format (see DateTime::format for options - this implements %x format) - #define LOCALE_DATE "%d/%m/%Y" +//String with short date format (see DateTime::format for options - this implements %x format) +#define LOCALE_DATE "%d/%m/%Y" #endif // LOCALE_DATE #ifndef LOCALE_TIME - //String with time format (see DateTime::format for options - this implements %X format) - #define LOCALE_TIME "%H:%M:%S" +//String with time format (see DateTime::format for options - this implements %X format) +#define LOCALE_TIME "%H:%M:%S" #endif // LOCALE_TIME #ifndef LOCALE_DATE_TIME - //String with time and date format (see DateTime::format for options - this implements %c format) - #define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" +//String with time and date format (see DateTime::format for options - this implements %c format) +#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" #endif // LOCALE_DATE_TIME #endif // LOCALE_H_INCLUDED From 3011217f1af675d34c9ee9b40d4ad6f78c927f59 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Sat, 15 Dec 2018 00:23:01 +0000 Subject: [PATCH 13/16] Fix coding style - this is getting boring! --- Sming/SmingCore/DateTime.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 Sming/SmingCore/DateTime.h diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h old mode 100755 new mode 100644 index 45cd606c4d..4bc4695471 --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -294,7 +294,7 @@ class DateTime String format(String format); private: - void calcDayOfYear(); // Helper function calculates day of year + void calcDayOfYear(); // Helper function calculates day of year uint8_t calcWeek(uint8_t firstDay); //Helper function calculates week number based on firstDay of week public: From a565286753ab02fdcd9438a582db5e5543c5da5e Mon Sep 17 00:00:00 2001 From: riban-bw Date: Sat, 15 Dec 2018 10:33:03 +0000 Subject: [PATCH 14/16] Fixes fromHttpDate for RFC 850. Update API documentation. Corrects some locale errors. Update sample to match library. --- Sming/SmingCore/DateTime.cpp | 11 +++++------ Sming/SmingCore/DateTime.h | 19 ++++++++++--------- Sming/SmingCore/SmingLocale.h | 6 +++--- samples/Basic_DateTime/app/application.cpp | 12 ++++++------ 4 files changed, 24 insertions(+), 24 deletions(-) mode change 100644 => 100755 Sming/SmingCore/DateTime.h mode change 100644 => 100755 Sming/SmingCore/SmingLocale.h diff --git a/Sming/SmingCore/DateTime.cpp b/Sming/SmingCore/DateTime.cpp index df76f6d690..bf7ddc579e 100755 --- a/Sming/SmingCore/DateTime.cpp +++ b/Sming/SmingCore/DateTime.cpp @@ -13,9 +13,6 @@ #define LEAP_YEAR(year) ((year % 4) == 0) -/* - * Used to parse HTTP date strings - see fromHttpDate() - */ static DEFINE_FSTR(flashMonthNames, LOCALE_MONTH_NAMES); static DEFINE_FSTR(flashDayNames, LOCALE_DAY_NAMES); @@ -66,9 +63,10 @@ bool DateTime::isNull() bool DateTime::fromHttpDate(const String& httpDate) { - if(httpDate.length() < 29) + int first = httpDate.indexOf(','); + if(first < 0 || httpDate.length() - first < 20) return false; - + first += 2; // Skip ", " auto ptr = httpDate.c_str(); // Parse and return a decimal number and update ptr to the first non-numeric character after it @@ -81,7 +79,8 @@ bool DateTime::fromHttpDate(const String& httpDate) } if(DayofWeek > 6) return false; // Invalid day of week - ptr += 5; + + ptr += first; Day = parseNumber(); if(*ptr == '\0') diff --git a/Sming/SmingCore/DateTime.h b/Sming/SmingCore/DateTime.h old mode 100644 new mode 100755 index 4bc4695471..cab6d704e6 --- a/Sming/SmingCore/DateTime.h +++ b/Sming/SmingCore/DateTime.h @@ -249,17 +249,18 @@ class DateTime return toUnixTime(sec, min, hour, day, month, year); } - /** @brief Create string with strftime style formatting + /** @brief Create string formatted with time and date placeholders * @param format String including date and time formatting * @retval String Formatted string * @note Uses strftime style formatting, e.g. format("Today is %a, %d %b %Y") returns "Today is Mon, 10 Dec 2018" - * @note Localisation may be implemented in libsming at compile time - * @note Formatting parameters (braced param not yet implemented): + * @note Localisation may be implemented in libsming at compile time by setting LOCALE, e.g. LOCALE=LOCALE_DE_DE + * @note Default localisation is EN_GB + * @note Formatting parameters * | Param | Description | Locale | * | :----:| :---------- | :----: | * | %%a | Abbreviated weekday name| * | * | %%A | Full weekday name | * | - * | %%b | Abbreviate month name | * | + * | %%b | Abbreviated month name | * | * | %%B | Full month name | * | * | %%c | Locale preferred date and time format | * | * | %%C | Century number (2 digits) | | @@ -269,7 +270,7 @@ class DateTime * | %%F | ISO 8601 date format (YYYY-mm-dd) | | * | %%h | Equivalent to %%b | * | * | %%H | Hour as a decimal number using a 24-hour clock (range 00 to 23) | | - * | %%I | Hour as a decimal number using a 12-hour clock (range 00 to 12 | | + * | %%I | Hour as a decimal number using a 12-hour clock (range 00 to 12) | | * | %%j | Day of the year as a decimal number (range 001 to 366) | | * | %%m | Month as a decimal number (range 01 to 12) | | * | %%M | Minute as a decimal number (range 00 to 59) | | @@ -281,14 +282,14 @@ class DateTime * | %%t | Horizontal tab | | * | %%T | Time in 24-hour notation (HH:MM:SS) | | * | %%u | Day of the week as a decimal (range 1 to 7, Monday is 1) | | - * | %%U | Week number as a decimal number (range 00 to 53, first Sunday as the first day of week 01) | | - * | (%%V) | ISO 8601 week number as a decimal number (range 01 to 53, where week 1 is the first week including a Thursday) | | + * | %%U | Week number as a decimal number (range 00 to 53, first Sunday as the first day of week 01) | | + * | %%V | ISO 8601 week number as a decimal number (range 01 to 53, where week 1 is the first week including a Thursday) | | * | %%w | Day of the week as a decimal (range 0 to 6, Sunday is 0) | | - * | (%%W) | Week number as a decimal number (range 00 to 53, first Monday as the first day of week 01) | | + * | %%W | Week number as a decimal number (range 00 to 53, first Monday as the first day of week 01) | | * | %%x | Locale preferred date representation | * | * | %%X | Locale preferred time representation | * | * | %%y | Year as a decimal number without a century (range 00 to 99) | | - * | %%Y | The year as a decimal number (range 1970 to ...) | | + * | %%Y | Year as a decimal number (range 1970 to ...) | | * | %% | Percent sign | | */ String format(String format); diff --git a/Sming/SmingCore/SmingLocale.h b/Sming/SmingCore/SmingLocale.h old mode 100644 new mode 100755 index 2d639853b2..0efabf1ae6 --- a/Sming/SmingCore/SmingLocale.h +++ b/Sming/SmingCore/SmingLocale.h @@ -1,4 +1,4 @@ -/** Localization is defined within Locale.h +/** Localization is defined within SmingLocale.h * Each locale has a unique ID (usually its international dial code, e.g. GB=44 * The default locale is GB and the default values are those used by GB. * To add a new locale: @@ -22,12 +22,12 @@ #if LOCALE == LOCALE_EN_US #define LOCALE_DATE "%m/%d/%Y" +#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" #elif LOCALE == LOCALE_FR_FR #define LOCALE_MONTH_NAMES \ "janvier\0f�vrier\0mars\0avril\0mai\0juin\0juillet\0ao�t\0septembre\0octobre\0novembre\0d�cembre" #define LOCALE_DAY_NAMES "dimanche\0lundi\0mardi\0mercredi\0jeudi\0vendredi\0samedi" -#define LOCALE_DATE "%d-%m-%Y" #elif LOCALE == LOCALE_DE_DE #define LOCALE_MONTH_NAMES \ @@ -59,7 +59,7 @@ #endif // LOCALE_TIME #ifndef LOCALE_DATE_TIME //String with time and date format (see DateTime::format for options - this implements %c format) -#define LOCALE_DATE_TIME "%a %b %d %H:%M:%S %Y" +#define LOCALE_DATE_TIME "%a %d %b %Y %X" #endif // LOCALE_DATE_TIME #endif // LOCALE_H_INCLUDED diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp index 4663226b67..ddc13b42c3 100755 --- a/samples/Basic_DateTime/app/application.cpp +++ b/samples/Basic_DateTime/app/application.cpp @@ -49,12 +49,12 @@ void showTime(time_t timestamp) //Formatted strings Serial.println(dt.format("%%R 24-hour time (HH:MM): %R")); Serial.println(dt.format("%%r 12-hour time (hh:MM:SS AM): %r")); - Serial.println(dt.format("%%c Date and time (ddd mmm DD HH:MM:SS YYYY): %c")); - Serial.println(dt.format("%%D Short date (MM/DD/YY): %D")); - Serial.println(dt.format("%%F Short date (YYYY-MM-DD): %F")); - Serial.println(dt.format("%%T ISO 8601 time format (HH:MM:SS): %T")); - Serial.println(dt.format("%%x Local date (DD/MM/YYYY): %x")); - Serial.println(dt.format("%%X Local time (HH:MM:SS): %X")); + Serial.println(dt.format("%%c Locale date and time: %c")); + Serial.println(dt.format("%%D US short date (MM/DD/YY): %D")); + Serial.println(dt.format("%%F ISO 8601 date (YYYY-MM-DD): %F")); + Serial.println(dt.format("%%T ISO 8601 time (HH:MM:SS): %T")); + Serial.println(dt.format("%%x Locale date: %x")); + Serial.println(dt.format("%%X Locale time: %X")); //HTTP date Serial.print("toHTTPDate: "); Serial.println(dt.toHTTPDate()); From dc3e411fe298a5cb96682f18e7e65718343ad357 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Sat, 15 Dec 2018 11:03:09 +0000 Subject: [PATCH 15/16] Adds Locale to Makefile --- Sming/Makefile | 83 ++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 39 deletions(-) mode change 100644 => 100755 Sming/Makefile diff --git a/Sming/Makefile b/Sming/Makefile old mode 100644 new mode 100755 index 77b5dcdd59..fbcfdf7843 --- a/Sming/Makefile +++ b/Sming/Makefile @@ -17,13 +17,13 @@ ## SMING_HOME sets the path where Sming framework is located. ## Windows: -# SMING_HOME = c:/tools/sming/Sming +# SMING_HOME = c:/tools/sming/Sming # MacOS / Linux # SMING_HOME = /opt/esp-open-sdk ## COM port parameter is reqruied to flash firmware correctly. -## Windows: +## Windows: # COM_PORT = COM3 # MacOS / Linux: @@ -87,7 +87,7 @@ else # Default ESP_HOME. Can be overriden. ESP_HOME ?= /opt/esp-open-sdk - include Makefile-macos.mk + include Makefile-macos.mk else ifeq ($(UNAME),Linux) # Linux Detected UNAME := Linux @@ -140,7 +140,7 @@ COM_SPEED_SERIAL ?= $(COM_SPEED) # By default `debugf` does not print file name and line number. If you want this enabled set the directive below to 1 DEBUG_PRINT_FILENAME_AND_LINE ?= 0 -# Default debug verbose level is INFO, where DEBUG=3 INFO=2 WARNING=1 ERROR=0 +# Default debug verbose level is INFO, where DEBUG=3 INFO=2 WARNING=1 ERROR=0 DEBUG_VERBOSE_LEVEL ?= 2 # Disable CommandExecutor functionality if not used and save some ROM and RAM @@ -178,9 +178,9 @@ MODULES = system system/helpers Wiring appinit \ EXTRA_INCDIR = include system/include Wiring Libraries SmingCore $(SDK_BASE)/../include # Place a file that should exist in a submodule that is fetched separately -# => rboot +# => rboot THIRD_PARTY_DATA = third-party/rboot/Makefile -EXTRA_INCDIR += third-party/rboot third-party/rboot/appcode +EXTRA_INCDIR += third-party/rboot third-party/rboot/appcode # => spiffs THIRD_PARTY_DATA += third-party/spiffs/makefile @@ -202,6 +202,11 @@ THIRD_PARTY_DATA += third-party/mqtt-codec/Makefile EXTRA_INCDIR += third-party/mqtt-codec/src CUSTOM_TARGETS += $(USER_LIBDIR)/libmqttc.a +# => LOCALE +ifdef LOCALE + CFLAGS += -DLOCALE=$(LOCALE) +endif + # => SDK SDK_INTERNAL = 0 ifneq (,$(findstring third-party/ESP8266_NONOS_SDK, $(SDK_BASE))) @@ -231,7 +236,7 @@ endif LIBLWIP = lwip ENABLE_CUSTOM_LWIP ?= 1 ENABLE_ESPCONN ?= 0 -ifeq ($(ENABLE_CUSTOM_LWIP), 0) +ifeq ($(ENABLE_CUSTOM_LWIP), 0) EXTRA_INCDIR += system/esp-lwip endif ifeq ($(ENABLE_CUSTOM_LWIP), 1) @@ -271,10 +276,10 @@ endif MFORCE32 := $(shell $(CC) --help=target | grep mforce-l32) -CFLAGS_COMMON = -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -finline-functions -fdata-sections -ffunction-sections +CFLAGS_COMMON = -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -finline-functions -fdata-sections -ffunction-sections # compiler flags using during compilation of source files. Add '-pg' for debugging CFLAGS += -Wall -Wundef -Wpointer-arith -Wno-comment $(CFLAGS_COMMON) \ - -D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF -DARDUINO=106 -DCOM_SPEED_SERIAL=$(COM_SPEED_SERIAL) -DENABLE_CMD_EXECUTOR=$(ENABLE_CMD_EXECUTOR) -DESP8266=1 -DSMING_INCLUDED=1 + -D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF -DARDUINO=106 -DCOM_SPEED_SERIAL=$(COM_SPEED_SERIAL) -DENABLE_CMD_EXECUTOR=$(ENABLE_CMD_EXECUTOR) -DESP8266=1 -DSMING_INCLUDED=1 ifneq ($(STRICT),1) CFLAGS += -Werror -Wno-sign-compare -Wno-parentheses -Wno-unused-variable -Wno-unused-but-set-variable -Wno-strict-aliasing -Wno-return-type -Wno-maybe-uninitialized endif @@ -288,11 +293,11 @@ else CFLAGS += -Os -g endif ifneq ($(MFORCE32),) - # Your compiler supports the -mforce-l32 flag which means that + # Your compiler supports the -mforce-l32 flag which means that # constants can be stored in flash (program) memory instead of SRAM. # See: https://www.arduino.cc/en/Reference/PROGMEM CFLAGS += -DPROGMEM_L32="__attribute__((aligned(4))) __attribute__((section(\".irom.text\")))" -mforce-l32 -else +else CFLAGS += -DPROGMEM_L32="" endif @@ -310,7 +315,7 @@ CXXFLAGS += -Wno-reorder endif # linker flags used to generate the main object file -LDFLAGS = -nostdlib -u call_user_start -Wl,-static -Wl,--gc-sections -Wl,-wrap,system_restart_local +LDFLAGS = -nostdlib -u call_user_start -Wl,-static -Wl,--gc-sections -Wl,-wrap,system_restart_local # linker script used for the above linkier step LD_PATH = compiler/ld/ @@ -326,14 +331,14 @@ ifeq ($(ENABLE_SSL),1) THIRD_PARTY_DATA += third-party/axtls-8266/Makefile LIBS += axtls MODULES += third-party/axtls-8266/compat third-party/axtls-8266/replacements - EXTRA_INCDIR += third-party/axtls-8266 axtls-8266/ssl third-party/axtls-8266/crypto + EXTRA_INCDIR += third-party/axtls-8266 axtls-8266/ssl third-party/axtls-8266/crypto AXTLS_FLAGS = -DLWIP_RAW=1 -DENABLE_SSL=1 - ifeq ($(SSL_DEBUG),1) # + ifeq ($(SSL_DEBUG),1) # AXTLS_FLAGS += -DSSL_DEBUG=1 -DDEBUG_TLS_MEM=1 -DAXL_DEBUG=1 endif CUSTOM_TARGETS += $(USER_LIBDIR)/libaxtls.a # EXTRA_CFLAGS_AXTLS = -I../../system/include -I../../Wiring - CFLAGS += $(AXTLS_FLAGS) + CFLAGS += $(AXTLS_FLAGS) CXXFLAGS += $(AXTLS_FLAGS) endif @@ -350,7 +355,7 @@ SDK_INCDIR := $(addprefix -I$(SDK_BASE)/,$(SDK_INCDIR)) AS_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.s)) ifeq ($(ENABLE_GDB), 1) - AS_SRC += $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S)) + AS_SRC += $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S)) endif C_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c)) CXX_SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp)) @@ -397,11 +402,11 @@ define compile-objects $1/%.o: %.s $(vecho) "AS $$<" $(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@ -ifeq ($(ENABLE_GDB), 1) +ifeq ($(ENABLE_GDB), 1) $1/%.o: %.S $(vecho) "AS $$<" $(Q) $(AS) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@ -endif +endif $1/%.o: %.c $1/%.c.d $(vecho) "CC $$<" $(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@ @@ -412,7 +417,7 @@ $1/%.c.d: %.c $(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -MM -MT $1/$$*.o $$< -o $$@ $1/%.cpp.d: %.cpp $(Q) $(CXX) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CXXFLAGS) -MM -MT $1/$$*.o $$< -o $$@ - + .PRECIOUS: $1/%.c.d $1/%.cpp.d endef @@ -420,11 +425,11 @@ endef all: checkdirs $(APP_AR) tools -ifeq ($(ENABLE_CUSTOM_PWM), 1) +ifeq ($(ENABLE_CUSTOM_PWM), 1) $(USER_LIBDIR)/libpwm_open.a: third-party/pwm/pwm.c $(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $< -o $(dir $<)/pwm.o $(Q) $(AR) rcs $@ $(dir $<)/pwm.o -endif +endif ifeq ($(ENABLE_SSL), 1) $(USER_LIBDIR)/libaxtls.a: @@ -434,7 +439,7 @@ endif ifeq ($(ENABLE_CUSTOM_HEAP), 1) $(USER_LIBDIR)/libmainmm.a: $(addprefix $(SDK_LIBDIR)/,libmain.a) $(vecho) "Enabling custom heap implementation" - $(Q) cp $^ $@ + $(Q) cp $^ $@ $(Q) $(AR) -d $@ mem_manager.o endif @@ -462,7 +467,7 @@ ifeq ($(ENABLE_SSL), 1) else $(vecho) "(!) SSL support is not enabled. To enable it type: 'make clean; make ENABLE_SSL=1'" endif - $(Q) cp -r $(APP_AR) $(USER_LIBDIR)/$(LIBSMING).a + $(Q) cp -r $(APP_AR) $(USER_LIBDIR)/$(LIBSMING).a $(vecho) "Done" checkdirs: $(ARDUINO_LIBRARIES) $(THIRD_PARTY_DATA) reload $(BUILD_DIR) $(FW_BASE) $(CUSTOM_TARGETS) @@ -482,7 +487,7 @@ clean: $(Q) rm -rf $(FW_BASE) ifeq ($(ENABLE_SSL), 1) $(Q) -$(MAKE) --no-print-directory -C third-party/axtls-8266 clean -e V=$(V) BIN_DIR="$(SMING_HOME)/$(USER_LIBDIR)" CFLAGS_EXTRA="$(EXTRA_CFLAGS_AXTLS)" -endif +endif ifeq ($(ENABLE_CUSTOM_LWIP), 1) $(Q) -$(MAKE) -C third-party/esp-open-lwip/ -f Makefile.open ENABLE_ESPCONN=$(ENABLE_ESPCONN) SDK_BASE="$(SDK_BASE)" USER_LIBDIR="$(SMING_HOME)/$(USER_LIBDIR)/" CFLAGS_EXTRA="$(EXTRA_CFLAGS_LWIP)" clean endif @@ -490,11 +495,11 @@ ifeq ($(ENABLE_CUSTOM_LWIP), 2) $(Q) -$(MAKE) -C third-party/lwip2/ -f Makefile.sming ENABLE_ESPCONN=$(ENABLE_ESPCONN) SDK_BASE="$(SDK_BASE)" USER_LIBDIR="$(SMING_HOME)/$(USER_LIBDIR)/" CFLAGS_EXTRA="$(EXTRA_CFLAGS_LWIP)" clean endif -test: Basic_Blink Basic_rBoot Basic_Ssl Basic_HwPWM +test: Basic_Blink Basic_rBoot Basic_Ssl Basic_HwPWM -docs/wiki/Home.md: +docs/wiki/Home.md: $(vecho) "No Wiki submodule found. Using git to fetch it..." - $(Q) $(GIT) submodule update --init ../docs/wiki + $(Q) $(GIT) submodule update --init ../docs/wiki docs/api/error.log: ifndef DOXYGEN @@ -509,32 +514,32 @@ docs/api/sming/index.html: docs/api/error.log $(vecho) "Undocumented parameters: $(shell grep "The following parameters of .* are not documented" $(SMING_HOME)/../docs/api/error.log | wc -l) " $(vecho) "Open in your browser: file://$(SMING_HOME)/../$@" -wiki: docs/wiki/Home.md -api: docs/api/sming/index.html +wiki: docs/wiki/Home.md +api: docs/api/sming/index.html docs: wiki api -third-party/%: +third-party/%: $(vecho) "Fetching $(dir $@) ..." $(Q) $(GIT) submodule update --init --recursive $(dir $@) $(Q) touch $(patsubst third-party/%/,third-party/.patches/%.patch, $(dir $@)) $(Q) -cd $(dir $@); $(GIT) apply -v $(patsubst third-party/%/,$(SMING_HOME)/third-party/.patches/%.patch, $(dir $@)) --ignore-whitespace --whitespace=nowarn # if the new submodule brings source code files that need to be compiled inside Sming -# then we need somehow to be able to "see" these new files. +# then we need somehow to be able to "see" these new files. # For now we solve this by "reloading" the makefile after fetching a module. RELOAD_MKFILE=1 -Libraries/%: +Libraries/%: $(vecho) "Fetching Arduino Library $(dir $@) ..." $(Q) $(GIT) submodule update --init --recursive $(dir $@) $(Q) touch $(patsubst Libraries/%/,Libraries/.patches/%.patch, $(dir $@)) $(Q) -cd $(dir $@); $(GIT) apply -v $(patsubst Libraries/%/,$(SMING_HOME)/Libraries/.patches/%.patch, $(dir $@)) --ignore-whitespace --whitespace=nowarn # if the new submodule brings source code files that need to be compiled inside Sming -# then we need somehow to be able to "see" these new files. +# then we need somehow to be able to "see" these new files. # For now we solve this by "reloading" the makefile after fetching a module. RELOAD_MKFILE=1 -reload: +reload: $(Q) if [ $(RELOAD_MKFILE) -eq 1 ]; then \ $(MAKE) -C $(SMING_HOME) $(MAKECMDGOALS) RELOAD_MKFILE=0; \ fi @@ -548,16 +553,16 @@ samples-clean: $(Q) for dir in $(SAMPLES_DIRS); do \ $(MAKE) -C $(SMING_HOME)/../samples/$$dir clean; \ done - + third-party-clean: $(Q) for dir in $(THIRD_PARTY_DIRS); do \ rm -rf third-party/$$dir; \ done - $(Q) $(GIT) checkout third-party - + $(Q) $(GIT) checkout third-party + libraries-clean: $(Q) rm -rf Libraries - $(Q) $(GIT) checkout Libraries + $(Q) $(GIT) checkout Libraries tools: third-party/spiffs/makefile $(TOOLS_DIRS) @@ -573,7 +578,7 @@ tools-clean: if [ ! -d $(SMING_HOME)/../tools/$$dir ]; then continue; fi; \ $(MAKE) --no-print-directory -C $(SMING_HOME)/../tools/$$dir clean V=$(V); \ done - + dist-clean: clean samples-clean third-party-clean libraries-clean tools-clean $(Q) for file in $(shell ls $(USER_LIBDIR)/lib*.a ); do \ rm $$file; \ From 0790a2d5035f8a7202fcf31a05e86cafc20aad04 Mon Sep 17 00:00:00 2001 From: riban-bw Date: Sun, 16 Dec 2018 08:33:35 +0000 Subject: [PATCH 16/16] Fixes file permissions --- Sming/Makefile | 0 Sming/SmingCore/Network/NtpClient.h | 0 Sming/SmingCore/SystemClock.h | 0 samples/Basic_DateTime/app/application.cpp | 0 samples/SDCard/app/application.cpp | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 Sming/Makefile mode change 100755 => 100644 Sming/SmingCore/Network/NtpClient.h mode change 100755 => 100644 Sming/SmingCore/SystemClock.h mode change 100755 => 100644 samples/Basic_DateTime/app/application.cpp mode change 100755 => 100644 samples/SDCard/app/application.cpp diff --git a/Sming/Makefile b/Sming/Makefile old mode 100755 new mode 100644 diff --git a/Sming/SmingCore/Network/NtpClient.h b/Sming/SmingCore/Network/NtpClient.h old mode 100755 new mode 100644 diff --git a/Sming/SmingCore/SystemClock.h b/Sming/SmingCore/SystemClock.h old mode 100755 new mode 100644 diff --git a/samples/Basic_DateTime/app/application.cpp b/samples/Basic_DateTime/app/application.cpp old mode 100755 new mode 100644 diff --git a/samples/SDCard/app/application.cpp b/samples/SDCard/app/application.cpp old mode 100755 new mode 100644