Saya Matsuura
/
drum
明石高専ロボ研 drum
neopixel.h@0:ca84ed7518f5, 2020-01-02 (annotated)
- Committer:
- TanakaRobo
- Date:
- Thu Jan 02 09:30:06 2020 +0000
- Revision:
- 0:ca84ed7518f5
- Child:
- 5:68617f409aaf
buffered serial before test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TanakaRobo | 0:ca84ed7518f5 | 1 | #ifndef NEOPIXEL_H |
TanakaRobo | 0:ca84ed7518f5 | 2 | #define NEOPIXEL_H |
TanakaRobo | 0:ca84ed7518f5 | 3 | #include "mbed.h" |
TanakaRobo | 0:ca84ed7518f5 | 4 | |
TanakaRobo | 0:ca84ed7518f5 | 5 | /* |
TanakaRobo | 0:ca84ed7518f5 | 6 | // Example |
TanakaRobo | 0:ca84ed7518f5 | 7 | |
TanakaRobo | 0:ca84ed7518f5 | 8 | NeoPixelOut npx(D12); |
TanakaRobo | 0:ca84ed7518f5 | 9 | |
TanakaRobo | 0:ca84ed7518f5 | 10 | int main() { |
TanakaRobo | 0:ca84ed7518f5 | 11 | wait(0.2); // wait for HSE to stabilize |
TanakaRobo | 0:ca84ed7518f5 | 12 | |
TanakaRobo | 0:ca84ed7518f5 | 13 | npx.global_scale = 1.0f; // Adjust brightness |
TanakaRobo | 0:ca84ed7518f5 | 14 | npx.normalize = true; // Equalize brightness to make r + g + b = 255 |
TanakaRobo | 0:ca84ed7518f5 | 15 | |
TanakaRobo | 0:ca84ed7518f5 | 16 | Pixel strip[6]; |
TanakaRobo | 0:ca84ed7518f5 | 17 | strip[0].hex = 0xFF0000; |
TanakaRobo | 0:ca84ed7518f5 | 18 | strip[1].hex = 0xFFFF00; |
TanakaRobo | 0:ca84ed7518f5 | 19 | strip[2].hex = 0x00FF00; |
TanakaRobo | 0:ca84ed7518f5 | 20 | strip[3].hex = 0x00FFFF; |
TanakaRobo | 0:ca84ed7518f5 | 21 | strip[4].hex = 0x0000FF; |
TanakaRobo | 0:ca84ed7518f5 | 22 | strip[5].hex = 0xFF00FF; |
TanakaRobo | 0:ca84ed7518f5 | 23 | |
TanakaRobo | 0:ca84ed7518f5 | 24 | npx.send(strip, 6); |
TanakaRobo | 0:ca84ed7518f5 | 25 | |
TanakaRobo | 0:ca84ed7518f5 | 26 | while(1); |
TanakaRobo | 0:ca84ed7518f5 | 27 | } |
TanakaRobo | 0:ca84ed7518f5 | 28 | */ |
TanakaRobo | 0:ca84ed7518f5 | 29 | |
TanakaRobo | 0:ca84ed7518f5 | 30 | |
TanakaRobo | 0:ca84ed7518f5 | 31 | |
TanakaRobo | 0:ca84ed7518f5 | 32 | /** |
TanakaRobo | 0:ca84ed7518f5 | 33 | * @brief Struct for easy manipulation of RGB colors. |
TanakaRobo | 0:ca84ed7518f5 | 34 | * |
TanakaRobo | 0:ca84ed7518f5 | 35 | * Set components in the xrgb.r (etc.) and you will get |
TanakaRobo | 0:ca84ed7518f5 | 36 | * the hex in xrgb.num. |
TanakaRobo | 0:ca84ed7518f5 | 37 | */ |
TanakaRobo | 0:ca84ed7518f5 | 38 | union Pixel { |
TanakaRobo | 0:ca84ed7518f5 | 39 | /** Struct for access to individual color components */ |
TanakaRobo | 0:ca84ed7518f5 | 40 | struct __attribute__((packed)) { |
TanakaRobo | 0:ca84ed7518f5 | 41 | uint8_t b; |
TanakaRobo | 0:ca84ed7518f5 | 42 | uint8_t g; |
TanakaRobo | 0:ca84ed7518f5 | 43 | uint8_t r; |
TanakaRobo | 0:ca84ed7518f5 | 44 | uint8_t a; // unused |
TanakaRobo | 0:ca84ed7518f5 | 45 | }; |
TanakaRobo | 0:ca84ed7518f5 | 46 | |
TanakaRobo | 0:ca84ed7518f5 | 47 | /** RGB color as a single uint32_t */ |
TanakaRobo | 0:ca84ed7518f5 | 48 | uint32_t hex; |
TanakaRobo | 0:ca84ed7518f5 | 49 | }; |
TanakaRobo | 0:ca84ed7518f5 | 50 | |
TanakaRobo | 0:ca84ed7518f5 | 51 | |
TanakaRobo | 0:ca84ed7518f5 | 52 | class NeoPixelOut : DigitalOut { |
TanakaRobo | 0:ca84ed7518f5 | 53 | private: |
TanakaRobo | 0:ca84ed7518f5 | 54 | void byte(uint32_t b); |
TanakaRobo | 0:ca84ed7518f5 | 55 | int boad_; |
TanakaRobo | 0:ca84ed7518f5 | 56 | |
TanakaRobo | 0:ca84ed7518f5 | 57 | public: |
TanakaRobo | 0:ca84ed7518f5 | 58 | bool normalize; |
TanakaRobo | 0:ca84ed7518f5 | 59 | float global_scale; |
TanakaRobo | 0:ca84ed7518f5 | 60 | |
TanakaRobo | 0:ca84ed7518f5 | 61 | NeoPixelOut(PinName pin); |
TanakaRobo | 0:ca84ed7518f5 | 62 | |
TanakaRobo | 0:ca84ed7518f5 | 63 | void send(Pixel *colors, uint32_t count, bool flipwait=true); |
TanakaRobo | 0:ca84ed7518f5 | 64 | |
TanakaRobo | 0:ca84ed7518f5 | 65 | /** Wait long enough to make the colors show up */ |
TanakaRobo | 0:ca84ed7518f5 | 66 | void flip(void); |
TanakaRobo | 0:ca84ed7518f5 | 67 | }; |
TanakaRobo | 0:ca84ed7518f5 | 68 | |
TanakaRobo | 0:ca84ed7518f5 | 69 | |
TanakaRobo | 0:ca84ed7518f5 | 70 | #endif /* NEOPIXEL_H */ |