Saya Matsuura
/
drum
明石高専ロボ研 drum
neopixel.h@7:678c6b604ac7, 2020-11-12 (annotated)
- Committer:
- TanakaRobo
- Date:
- Thu Nov 12 16:44:10 2020 +0000
- Revision:
- 7:678c6b604ac7
- Parent:
- 6:a7894e6982ea
add lidar lite lib
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 | 6:a7894e6982ea | 4 | #include <vector> |
TanakaRobo | 0:ca84ed7518f5 | 5 | |
TanakaRobo | 0:ca84ed7518f5 | 6 | /* |
TanakaRobo | 0:ca84ed7518f5 | 7 | // Example |
TanakaRobo | 0:ca84ed7518f5 | 8 | |
TanakaRobo | 0:ca84ed7518f5 | 9 | NeoPixelOut npx(D12); |
TanakaRobo | 0:ca84ed7518f5 | 10 | |
TanakaRobo | 0:ca84ed7518f5 | 11 | int main() { |
TanakaRobo | 0:ca84ed7518f5 | 12 | wait(0.2); // wait for HSE to stabilize |
TanakaRobo | 0:ca84ed7518f5 | 13 | |
TanakaRobo | 0:ca84ed7518f5 | 14 | npx.global_scale = 1.0f; // Adjust brightness |
TanakaRobo | 0:ca84ed7518f5 | 15 | npx.normalize = true; // Equalize brightness to make r + g + b = 255 |
TanakaRobo | 0:ca84ed7518f5 | 16 | |
TanakaRobo | 0:ca84ed7518f5 | 17 | Pixel strip[6]; |
TanakaRobo | 0:ca84ed7518f5 | 18 | strip[0].hex = 0xFF0000; |
TanakaRobo | 0:ca84ed7518f5 | 19 | strip[1].hex = 0xFFFF00; |
TanakaRobo | 0:ca84ed7518f5 | 20 | strip[2].hex = 0x00FF00; |
TanakaRobo | 0:ca84ed7518f5 | 21 | strip[3].hex = 0x00FFFF; |
TanakaRobo | 0:ca84ed7518f5 | 22 | strip[4].hex = 0x0000FF; |
TanakaRobo | 0:ca84ed7518f5 | 23 | strip[5].hex = 0xFF00FF; |
TanakaRobo | 0:ca84ed7518f5 | 24 | |
TanakaRobo | 0:ca84ed7518f5 | 25 | npx.send(strip, 6); |
TanakaRobo | 0:ca84ed7518f5 | 26 | |
TanakaRobo | 0:ca84ed7518f5 | 27 | while(1); |
TanakaRobo | 0:ca84ed7518f5 | 28 | } |
TanakaRobo | 0:ca84ed7518f5 | 29 | */ |
TanakaRobo | 0:ca84ed7518f5 | 30 | |
TanakaRobo | 0:ca84ed7518f5 | 31 | |
TanakaRobo | 0:ca84ed7518f5 | 32 | |
TanakaRobo | 0:ca84ed7518f5 | 33 | /** |
TanakaRobo | 0:ca84ed7518f5 | 34 | * @brief Struct for easy manipulation of RGB colors. |
TanakaRobo | 0:ca84ed7518f5 | 35 | * |
TanakaRobo | 0:ca84ed7518f5 | 36 | * Set components in the xrgb.r (etc.) and you will get |
TanakaRobo | 0:ca84ed7518f5 | 37 | * the hex in xrgb.num. |
TanakaRobo | 0:ca84ed7518f5 | 38 | */ |
TanakaRobo | 0:ca84ed7518f5 | 39 | union Pixel { |
TanakaRobo | 0:ca84ed7518f5 | 40 | /** Struct for access to individual color components */ |
TanakaRobo | 0:ca84ed7518f5 | 41 | struct __attribute__((packed)) { |
TanakaRobo | 0:ca84ed7518f5 | 42 | uint8_t b; |
TanakaRobo | 0:ca84ed7518f5 | 43 | uint8_t g; |
TanakaRobo | 0:ca84ed7518f5 | 44 | uint8_t r; |
TanakaRobo | 0:ca84ed7518f5 | 45 | uint8_t a; // unused |
TanakaRobo | 0:ca84ed7518f5 | 46 | }; |
TanakaRobo | 0:ca84ed7518f5 | 47 | |
TanakaRobo | 0:ca84ed7518f5 | 48 | /** RGB color as a single uint32_t */ |
TanakaRobo | 0:ca84ed7518f5 | 49 | uint32_t hex; |
TanakaRobo | 0:ca84ed7518f5 | 50 | }; |
TanakaRobo | 0:ca84ed7518f5 | 51 | |
TanakaRobo | 0:ca84ed7518f5 | 52 | |
TanakaRobo | 0:ca84ed7518f5 | 53 | class NeoPixelOut : DigitalOut { |
TanakaRobo | 0:ca84ed7518f5 | 54 | private: |
TanakaRobo | 0:ca84ed7518f5 | 55 | void byte(uint32_t b); |
TanakaRobo | 6:a7894e6982ea | 56 | int board_; |
TanakaRobo | 6:a7894e6982ea | 57 | int num_pixels_; |
TanakaRobo | 6:a7894e6982ea | 58 | vector<Pixel> strip_; |
TanakaRobo | 0:ca84ed7518f5 | 59 | |
TanakaRobo | 0:ca84ed7518f5 | 60 | public: |
TanakaRobo | 0:ca84ed7518f5 | 61 | bool normalize; |
TanakaRobo | 0:ca84ed7518f5 | 62 | float global_scale; |
TanakaRobo | 0:ca84ed7518f5 | 63 | |
TanakaRobo | 6:a7894e6982ea | 64 | NeoPixelOut(PinName pin, int num = 0); |
TanakaRobo | 0:ca84ed7518f5 | 65 | |
TanakaRobo | 0:ca84ed7518f5 | 66 | void send(Pixel *colors, uint32_t count, bool flipwait=true); |
TanakaRobo | 6:a7894e6982ea | 67 | void changeNum(uint32_t num); |
TanakaRobo | 6:a7894e6982ea | 68 | void setBrightness(float brightness);//0~1 |
TanakaRobo | 6:a7894e6982ea | 69 | void setPixelColor(uint32_t i,uint32_t color); |
TanakaRobo | 6:a7894e6982ea | 70 | void show(); |
TanakaRobo | 6:a7894e6982ea | 71 | void off(bool flag = true); |
TanakaRobo | 6:a7894e6982ea | 72 | uint32_t color(uint32_t b,uint32_t g,uint32_t r); |
TanakaRobo | 6:a7894e6982ea | 73 | int numPixels(); |
TanakaRobo | 0:ca84ed7518f5 | 74 | |
TanakaRobo | 0:ca84ed7518f5 | 75 | /** Wait long enough to make the colors show up */ |
TanakaRobo | 0:ca84ed7518f5 | 76 | void flip(void); |
TanakaRobo | 0:ca84ed7518f5 | 77 | }; |
TanakaRobo | 0:ca84ed7518f5 | 78 | |
TanakaRobo | 0:ca84ed7518f5 | 79 | |
TanakaRobo | 0:ca84ed7518f5 | 80 | #endif /* NEOPIXEL_H */ |