ESPixelStick Firmware
Firmware for the ESPixelStick
Loading...
Searching...
No Matches
SaferStringConversion.hpp
Go to the documentation of this file.
1#pragma once
2/*
3* ESPixelStick.h
4*
5* Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
6* Copyright (c) 2016, 2022 Shelby Merrick
7* http://www.forkineye.com
8*
9* This program is provided free for you to use in any way that you wish,
10* subject to the laws and regulations where you are using it. Due diligence
11* is strongly suggested before using this code. Please give credit where due.
12*
13* The Author makes no warranty of any kind, express or implied, with regard
14* to this program or the documentation contained in this document. The
15* Author shall not be liable in any event for incidental or consequential
16* damages in connection with, or arising out of, the furnishing, performance
17* or use of these programs.
18*
19*/
20
21#include "ESPixelStick.h"
22#include "backported.h"
23
24// The following template functions' first parameter is defined
25// as a **reference** to an array of characters. The size of
26// the array is the template parameter. This allows the function
27// to statically assert that the passed-in buffer is large enough
28// to always succeed. Else, a compilation will occur.
29//
30// Allowing the compiler to deduce the template parameters has
31// multiple benefits:
32// * Users can ignore that the function is a template
33// * Code remains easy to read
34// * Compiler doesn't deduce wrong size
35// * Even if a user manually enters a larger template size,
36// the compiler will report an error, such as:
37// error: invalid initialization of reference of type 'char (&)[15]'
38// from expression of type 'char [12]'
39
40
41
42// Safer RGB to HTML string (e.g., "#FF8833") conversion function
43//
44// Example use:
45// char foo[8];
46// ESP_ERROR_CHECK(saferRgbToHtmlColorString(foo, led.r, led.g, led.b));
47//
48template <size_t N>
49inline esp_err_t saferRgbToHtmlColorString(char (&output)[N], uint8_t r, uint8_t g, uint8_t b) {
50 esp_err_t result = ESP_FAIL;
51
52 // Including the trailing null, this string requires eight (8) characters.
53 //
54 // The output is formatted as "#RRGGBB", where RR, GG, and BB are two hex digits
55 // for the red, green, and blue components, respectively.
56 static_assert(N >= 8);
57 static_assert(sizeof(int) <= sizeof(size_t)); // casting non-negative int to size_t is safe
58 int wouldHaveWrittenChars = snprintf(output, N, "#%02x%02x%02x", r, g, b);
59 if (likely((wouldHaveWrittenChars > 0) && (((size_t)wouldHaveWrittenChars) < N))) {
60 result = ESP_OK;
61 } else {
62 // TODO: assert((wouldHaveWrittenChars > 0) && (wouldHaveWrittenChars < N));
63 }
64 return result;
65}
66// Safer seconds to "Minutes:Seconds" string conversion function
67//
68// Example use:
69// char foo[12];
70// ESP_ERROR_CHECK(saferSecondsToFormattedMinutesAndSecondsString(foo, seconds));
71//
72template <size_t N>
73inline esp_err_t saferSecondsToFormattedMinutesAndSecondsString(char (&output)[N], uint32_t seconds) {
74 esp_err_t result = ESP_FAIL;
75
76 // Including the trailing null, the string may require up to twelve (12) characters.
77 //
78 // The output is formatted as "{minutes}:{seconds}".
79 // uint32_t seconds is in range [0..4294967295].
80 // therefore, minutes is in range [0..71582788] (eight characters).
81 // seconds is always exactly two characters.
82 static_assert(N >= 12);
83 static_assert(sizeof(int) <= sizeof(size_t)); // casting non-negative int to size_t is safe
84 uint32_t m = seconds / 60u;
85 uint8_t s = seconds % 60u;
86 int wouldHaveWrittenChars = snprintf(output, N, "%u:%02u", m, s);
87 if (likely((wouldHaveWrittenChars > 0) && (((size_t)wouldHaveWrittenChars) < N))) {
88 result = ESP_OK;
89 } else {
90 // TODO: assert((wouldHaveWrittenChars > 0) && (wouldHaveWrittenChars < N));
91 }
92 return result;
93}
esp_err_t saferRgbToHtmlColorString(char(&output)[N], uint8_t r, uint8_t g, uint8_t b)
Definition SaferStringConversion.hpp:49
esp_err_t saferSecondsToFormattedMinutesAndSecondsString(char(&output)[N], uint32_t seconds)
Definition SaferStringConversion.hpp:73