mirror of
https://github.com/Makuna/NeoPixelBus.git
synced 2025-08-07 21:04:27 +02:00
Updated Spiral Topography (markdown)
@@ -27,11 +27,32 @@ uint16_t Map(int16_t ring, int16_t pixel) const
|
||||
### Mapping Implementation
|
||||
To implement the map method, the ring argument and pixel must be translated into the final index value. The first part is to calculate the start of the ring; this can be accomplished by a lookup table using the data above. Then we just need to add the pixel to offset to the correct one.
|
||||
```
|
||||
const uint16_t Rings[] = {0, 1, 7, 19, 35, 59, 119};
|
||||
const uint16_t Rings[] = {0, 1, 7, 19, 35, 59};
|
||||
|
||||
uint16_t Map(int16_t ring, int16_t pixel) const {
|
||||
uint16_t index = Rings[ring] + pixel;
|
||||
return index;
|
||||
}
|
||||
```
|
||||
This can be improved by checking boundary cases like if you call it with ring 0 but state pixel #32 which doesn't make sense but it will return a value.
|
||||
This can be improved by checking boundary cases. As it sits if you call it with ring 0 but state pixel #32 which doesn't make sense; it will return a value that is possibly out of scope.
|
||||
|
||||
Below I have added the argument checking code. The ring table has been expanded to include an extra ring valueas it provides us with the information to be able to validate the arguments.
|
||||
|
||||
```
|
||||
const uint16_t Rings[] = {0, 1, 7, 19, 35, 59, 119}; // note, included the start of another ring
|
||||
|
||||
uint16_t Map(uint16_t ring, uint16_t pixel) const {
|
||||
if (ring >= (sizeof(Rings)/sizeof(Rings[0]) - 1)) { // minus one as the Rings includes the extra value
|
||||
return 0; // invalid ring argument
|
||||
}
|
||||
if (pixel >= Rings[ring + 1]) { // using the extra value for range testing
|
||||
return 0; // invalid pixel argument
|
||||
}
|
||||
uint16_t index = Rings[ring] + pixel;
|
||||
return index;
|
||||
}
|
||||
```
|
||||
NOTE: The code above `(sizeof(Rings)/sizeof(Rings[0])` is a way of having the compiler calculate the count of elements in the Rings array without us hardcoding anything. Often this is exposed as `_countof()` in many code based but it was not included in the Arduino headers for some reason.
|
||||
|
||||
### Making the object usable for different collections of rings
|
||||
The above code hard codes the array of values. We can make this a configuration argument to the constructor of our mapping class and thus reuse the code for different projects.
|
||||
|
Reference in New Issue
Block a user