![](/media/cache/profiles/P1030212_resized.JPG.50x50_q85.jpg)
明石高専ロボ研 mbedライブラリ
Dependents: MDD_L432KC USB2RS485 pathtracking odometry ... more
neopixel.cpp@0:ca84ed7518f5, 2020-01-02 (annotated)
- Committer:
- TanakaRobo
- Date:
- Thu Jan 02 09:30:06 2020 +0000
- Revision:
- 0:ca84ed7518f5
- Child:
- 5:a7894e6982ea
buffered serial before test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TanakaRobo | 0:ca84ed7518f5 | 1 | #include "mbed.h" |
TanakaRobo | 0:ca84ed7518f5 | 2 | #include "neopixel.h" |
TanakaRobo | 0:ca84ed7518f5 | 3 | |
TanakaRobo | 0:ca84ed7518f5 | 4 | const int wait_time[4][2] = { |
TanakaRobo | 0:ca84ed7518f5 | 5 | {6,14}, |
TanakaRobo | 0:ca84ed7518f5 | 6 | {2, 5}, |
TanakaRobo | 0:ca84ed7518f5 | 7 | {2, 6}, |
TanakaRobo | 0:ca84ed7518f5 | 8 | {5,11} |
TanakaRobo | 0:ca84ed7518f5 | 9 | }; |
TanakaRobo | 0:ca84ed7518f5 | 10 | |
TanakaRobo | 0:ca84ed7518f5 | 11 | NeoPixelOut::NeoPixelOut(PinName pin) : DigitalOut(pin) |
TanakaRobo | 0:ca84ed7518f5 | 12 | { |
TanakaRobo | 0:ca84ed7518f5 | 13 | normalize = false; |
TanakaRobo | 0:ca84ed7518f5 | 14 | global_scale = 1.0f; |
TanakaRobo | 0:ca84ed7518f5 | 15 | #ifdef __STM32L432xx_H |
TanakaRobo | 0:ca84ed7518f5 | 16 | boad_ = 0; |
TanakaRobo | 0:ca84ed7518f5 | 17 | #endif |
TanakaRobo | 0:ca84ed7518f5 | 18 | #ifdef __STM32F446xx_H |
TanakaRobo | 0:ca84ed7518f5 | 19 | boad_ = 1; |
TanakaRobo | 0:ca84ed7518f5 | 20 | #endif |
TanakaRobo | 0:ca84ed7518f5 | 21 | } |
TanakaRobo | 0:ca84ed7518f5 | 22 | |
TanakaRobo | 0:ca84ed7518f5 | 23 | // The timing should be approximately 800ns/300ns, 300ns/800ns |
TanakaRobo | 0:ca84ed7518f5 | 24 | void NeoPixelOut::byte(register uint32_t byte) |
TanakaRobo | 0:ca84ed7518f5 | 25 | { |
TanakaRobo | 0:ca84ed7518f5 | 26 | for (int i = 0; i < 8; i++) { |
TanakaRobo | 0:ca84ed7518f5 | 27 | gpio_write(&gpio, 1); |
TanakaRobo | 0:ca84ed7518f5 | 28 | |
TanakaRobo | 0:ca84ed7518f5 | 29 | // duty cycle determines bit value |
TanakaRobo | 0:ca84ed7518f5 | 30 | if (byte & 0x80) { |
TanakaRobo | 0:ca84ed7518f5 | 31 | // one |
TanakaRobo | 0:ca84ed7518f5 | 32 | for(int j = 0; j < wait_time[0][boad_]; j++) asm("NOP");//6 14 |
TanakaRobo | 0:ca84ed7518f5 | 33 | |
TanakaRobo | 0:ca84ed7518f5 | 34 | gpio_write(&gpio, 0); |
TanakaRobo | 0:ca84ed7518f5 | 35 | for(int j = 0; j < wait_time[1][boad_]; j++) asm("NOP");//2 5 |
TanakaRobo | 0:ca84ed7518f5 | 36 | } |
TanakaRobo | 0:ca84ed7518f5 | 37 | else { |
TanakaRobo | 0:ca84ed7518f5 | 38 | // zero |
TanakaRobo | 0:ca84ed7518f5 | 39 | for(int j = 0; j < wait_time[2][boad_]; j++) asm("NOP");//2 6 |
TanakaRobo | 0:ca84ed7518f5 | 40 | |
TanakaRobo | 0:ca84ed7518f5 | 41 | gpio_write(&gpio, 0); |
TanakaRobo | 0:ca84ed7518f5 | 42 | for(int j = 0; j < wait_time[3][boad_]; j++) asm("NOP");//5 11 |
TanakaRobo | 0:ca84ed7518f5 | 43 | } |
TanakaRobo | 0:ca84ed7518f5 | 44 | |
TanakaRobo | 0:ca84ed7518f5 | 45 | byte = byte << 1; // shift to next bit |
TanakaRobo | 0:ca84ed7518f5 | 46 | } |
TanakaRobo | 0:ca84ed7518f5 | 47 | |
TanakaRobo | 0:ca84ed7518f5 | 48 | } |
TanakaRobo | 0:ca84ed7518f5 | 49 | |
TanakaRobo | 0:ca84ed7518f5 | 50 | void NeoPixelOut::send(Pixel *colors, uint32_t count, bool flipwait) |
TanakaRobo | 0:ca84ed7518f5 | 51 | { |
TanakaRobo | 0:ca84ed7518f5 | 52 | // Disable interrupts in the critical section |
TanakaRobo | 0:ca84ed7518f5 | 53 | __disable_irq(); |
TanakaRobo | 0:ca84ed7518f5 | 54 | |
TanakaRobo | 0:ca84ed7518f5 | 55 | Pixel* rgb; |
TanakaRobo | 0:ca84ed7518f5 | 56 | float fr,fg,fb; |
TanakaRobo | 0:ca84ed7518f5 | 57 | for (int i = 0; i < count; i++) { |
TanakaRobo | 0:ca84ed7518f5 | 58 | rgb = colors++; |
TanakaRobo | 0:ca84ed7518f5 | 59 | fr = (int)rgb->r; |
TanakaRobo | 0:ca84ed7518f5 | 60 | fg = (int)rgb->g; |
TanakaRobo | 0:ca84ed7518f5 | 61 | fb = (int)rgb->b; |
TanakaRobo | 0:ca84ed7518f5 | 62 | |
TanakaRobo | 0:ca84ed7518f5 | 63 | if (normalize) { |
TanakaRobo | 0:ca84ed7518f5 | 64 | float scale = 255.0f/(fr+fg+fb); |
TanakaRobo | 0:ca84ed7518f5 | 65 | fr *= scale; |
TanakaRobo | 0:ca84ed7518f5 | 66 | fg *= scale; |
TanakaRobo | 0:ca84ed7518f5 | 67 | fb *= scale; |
TanakaRobo | 0:ca84ed7518f5 | 68 | } |
TanakaRobo | 0:ca84ed7518f5 | 69 | |
TanakaRobo | 0:ca84ed7518f5 | 70 | fr *= global_scale; |
TanakaRobo | 0:ca84ed7518f5 | 71 | fg *= global_scale; |
TanakaRobo | 0:ca84ed7518f5 | 72 | fb *= global_scale; |
TanakaRobo | 0:ca84ed7518f5 | 73 | |
TanakaRobo | 0:ca84ed7518f5 | 74 | if (fr > 255) fr = 255; |
TanakaRobo | 0:ca84ed7518f5 | 75 | if (fg > 255) fg = 255; |
TanakaRobo | 0:ca84ed7518f5 | 76 | if (fb > 255) fb = 255; |
TanakaRobo | 0:ca84ed7518f5 | 77 | if (fr < 0) fr = 0; |
TanakaRobo | 0:ca84ed7518f5 | 78 | if (fg < 0) fg = 0; |
TanakaRobo | 0:ca84ed7518f5 | 79 | if (fb < 0) fb = 0; |
TanakaRobo | 0:ca84ed7518f5 | 80 | |
TanakaRobo | 0:ca84ed7518f5 | 81 | // Black magic to fix distorted timing |
TanakaRobo | 0:ca84ed7518f5 | 82 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_DISABLE |
TanakaRobo | 0:ca84ed7518f5 | 83 | __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
TanakaRobo | 0:ca84ed7518f5 | 84 | #endif |
TanakaRobo | 0:ca84ed7518f5 | 85 | |
TanakaRobo | 0:ca84ed7518f5 | 86 | byte((int)fg); |
TanakaRobo | 0:ca84ed7518f5 | 87 | byte((int)fr); |
TanakaRobo | 0:ca84ed7518f5 | 88 | byte((int)fb); |
TanakaRobo | 0:ca84ed7518f5 | 89 | |
TanakaRobo | 0:ca84ed7518f5 | 90 | #ifdef __HAL_FLASH_INSTRUCTION_CACHE_ENABLE |
TanakaRobo | 0:ca84ed7518f5 | 91 | __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
TanakaRobo | 0:ca84ed7518f5 | 92 | #endif |
TanakaRobo | 0:ca84ed7518f5 | 93 | } |
TanakaRobo | 0:ca84ed7518f5 | 94 | |
TanakaRobo | 0:ca84ed7518f5 | 95 | __enable_irq(); |
TanakaRobo | 0:ca84ed7518f5 | 96 | |
TanakaRobo | 0:ca84ed7518f5 | 97 | if (flipwait) flip(); |
TanakaRobo | 0:ca84ed7518f5 | 98 | } |
TanakaRobo | 0:ca84ed7518f5 | 99 | |
TanakaRobo | 0:ca84ed7518f5 | 100 | |
TanakaRobo | 0:ca84ed7518f5 | 101 | void NeoPixelOut::flip(void) |
TanakaRobo | 0:ca84ed7518f5 | 102 | { |
TanakaRobo | 0:ca84ed7518f5 | 103 | wait_us(50); |
TanakaRobo | 0:ca84ed7518f5 | 104 | } |