1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// project description: I have an esp8266 that is receiving a string
// representing a color pattern of rgb values with a time for transitioning
// to the next color.
// example format: "0,0,0,1000;255,0,0,1000;"
// that is two values with 1000ms transition between each.
// I am parsing the incoming data so that, after it has been parsed I can loop
// over the values and update the gpio pins.
// My issue: I am having trouble properly saving the variables in a dynamic
// length array and creating a pointer to that array.
// The pointer to the array has the same memory addresses but not the same
// values (at least for the first index that I am printing)
// my approach: parse the incoming string so that I save each tick in the
// pattern; "255,0,0,1000" in an object, and each of those objects in a dynamic
// length array that I can point to in the main loop
// goal: I need to be able to save the incoming string in a format that I can
// loop over, without blocking potential future incoming values (if I want
// to run a new color pattern that is received over wifi)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// ----------------------------------------------------------------------------
// CLASS COLOR
// ----------------------------------------------------------------------------
#ifndef CLASS_COLOR
#define CLASS_COLOR
struct color {
int red {};
int green {};
int blue {};
int transition_time {};
color() = default;
color(int red_arg, int green_arg, int blue_arg, int transition_time_arg);
color(std::string s);
// friend:
friend std::istream& operator>>(std::istream& is, color& rhs);
friend std::ostream& operator<<(std::ostream& os, const color& rhs);
};
color::color(int red_arg, int green_arg, int blue_arg, int transition_time_arg)
: red { red_arg }
, green { green_arg }
, blue { blue_arg }
, transition_time { transition_time_arg }
{
}
color::color(std::string s)
{
std::istringstream iss { s };
iss >> red >> green >> blue >> transition_time;
}
std::istream& operator>>(std::istream& is, color& rhs)
{
is >> rhs.red >> rhs.green >> rhs.blue >> rhs.transition_time;
return is;
}
std::ostream& operator<<(std::ostream& os, const color& rhs)
{
os << "red: " << rhs.red << " - green: " << rhs.green
<< " - blue: " << rhs.blue << " - time: " << rhs.transition_time;
return os;
}
#endif // CLASS_COLOR
// ----------------------------------------------------------------------------
// STANDALONE FUNCTION PROTOTYPES
// ----------------------------------------------------------------------------
std::istringstream receiveColors(/* type from ? */);
std::string extractSingleColor(std::istringstream& iss);
std::vector<color> fillVectorColorFromStream(std::istringstream& iss);
template <typename T>
void
printVec(const std::vector<T>& v,
char after_element = ' ',
char end_of_elements = '\0');
// ----------------------------------------------------------------------------
// MAIN()
// ----------------------------------------------------------------------------
int main()
{
auto iss { receiveColors(/* */) };
std::vector<color> v { fillVectorColorFromStream(iss) };
printVec(v, '\n');
}
std::istringstream receiveColors(/* type from ? */)
{
// Create an istringstream from your source data (file, ... ):
std::istringstream iss { "0,0,0,1000;25,25,25,1000;150,150,150,1000" };
return iss;
}
std::string extractSingleColor(std::istringstream& iss)
{
std::string s;
std::getline(iss, s, ';');
return s;
}
std::vector<color> fillVectorColorFromStream(std::istringstream& iss)
{
std::vector<color> v;
// 's' needs an inital value, otherwise the loop doesn’t start:
for ( std::string s { extractSingleColor(iss) };
!s.empty();
s = extractSingleColor(iss) )
{
v.push_back( color(s) );
}
return v;
}
template <typename T>
void
printVec(const std::vector<T>& v, char after_element, char end_of_elements)
{
for(const auto& e : v) {
std::cout << e << after_element;
}
if('\0' != end_of_elements) {
std::cout << end_of_elements;
};
}
|