|
| 1 | +/* |
| 2 | + _________.___________ ____ __.__ __ |
| 3 | + / _____/| \______ \ | |/ _|__| ____ | | __ |
| 4 | + \_____ \ | || | \| < | |/ ___\| |/ / |
| 5 | + / \| || ` \ | \| \ \___| < |
| 6 | + /_______ /|___/_______ /____|__ \__|\___ >__|_ \ |
| 7 | + \/ \/ \/ \/ \/ |
| 8 | + |
| 9 | + LED_Helpers.h |
| 10 | +
|
| 11 | + SIDKick - SID-replacement with SID, Sound Expander and MIDI Emulation based on Teensy 4.1 |
| 12 | + (using reSID by Dag Lem and FMOPL by Jarek Burczynski, Tatsuyuki Satoh, Marco van den Heuvel, and Acho A. Tang) |
| 13 | + Copyright (c) 2019-2021 Carsten Dachsbacher <[email protected]> |
| 14 | +
|
| 15 | + Logo created with http://patorjk.com/software/taag/ |
| 16 | + |
| 17 | + This program is free software: you can redistribute it and/or modify |
| 18 | + it under the terms of the GNU General Public License as published by |
| 19 | + the Free Software Foundation, either version 3 of the License, or |
| 20 | + (at your option) any later version. |
| 21 | +
|
| 22 | + This program is distributed in the hope that it will be useful, |
| 23 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | + GNU General Public License for more details. |
| 26 | + |
| 27 | + You should have received a copy of the GNU General Public License |
| 28 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 29 | +*/ |
| 30 | + |
| 31 | +extern uint32_t NUM_LEDS; |
| 32 | + |
| 33 | +extern uint32_t ledWriteRGB[32]; |
| 34 | + |
| 35 | +extern uint8_t ledBits2Write; |
| 36 | +extern uint8_t ledColors2Write; |
| 37 | +extern int32_t ledWriteStatus; |
| 38 | +extern uint8_t ledColorsBufOfs; |
| 39 | + |
| 40 | +int32_t nLEDs = 0; |
| 41 | +int32_t nLEDsConfigTool = 0; |
| 42 | + |
| 43 | +static uint8_t ledVisMode = 0; |
| 44 | +static float last_tL = 0.0f; |
| 45 | +static float last_tR = 0.0f; |
| 46 | +static uint32_t vuSumL = 0; |
| 47 | +static uint32_t vuSumR = 0; |
| 48 | +static float vuAvg = 0.0f; |
| 49 | +static uint32_t vuMeter = 0; |
| 50 | +static uint32_t vu_nValues = 0; |
| 51 | +static uint32_t nSamplesCollected = 64*8; |
| 52 | +static float scaleSamples = 1.0f; |
| 53 | +static float scaleSamplesConfigTool = 1.0f; |
| 54 | +static int32_t nLEDsL = 0; |
| 55 | +static int32_t nLEDsR = 0; |
| 56 | +static uint32_t colorCycle = 0; |
| 57 | +static uint32_t ledColorCycleSpeed = 0; |
| 58 | +uint8_t collectStatistics = 1; |
| 59 | + |
| 60 | +static uint32_t col0 = 0; |
| 61 | +static uint32_t col1 = 0; |
| 62 | +static uint32_t hsv0 = 0; |
| 63 | +static uint32_t hsv1 = 0; |
| 64 | +static uint32_t col0_cycle[ 128 ], col1_cycle[ 128 ]; |
| 65 | + |
| 66 | +static uint32_t delayLED = 0; |
| 67 | + |
| 68 | +#define H_( hsv ) (((uint8_t*)&hsv)[2]) |
| 69 | +#define S_( hsv ) (((uint8_t*)&hsv)[1]) |
| 70 | +#define V_( hsv ) (((uint8_t*)&hsv)[0]) |
| 71 | + |
| 72 | +// unusual format of WS2818 |
| 73 | +#define G_( rgb ) (((uint8_t*)&rgb)[2]) |
| 74 | +#define R_( rgb ) (((uint8_t*)&rgb)[1]) |
| 75 | +#define B_( rgb ) (((uint8_t*)&rgb)[0]) |
| 76 | + |
| 77 | +#define LRG( r, g ) (((r)<<16)|g) |
| 78 | +//#define LR_( rg ) (((rg)>>16)&255) |
| 79 | +//#define LG_( rg ) ((rg)&255) |
| 80 | + |
| 81 | +#define RGB( r, g, b ) (((g)<<16)|((r)<<8)|b) |
| 82 | +#define HSV( h, s, v ) (((h)<<16)|((s)<<8)|v) |
| 83 | + |
| 84 | +__attribute__( ( always_inline ) ) inline uint32_t SCALE( const uint32_t a, const int32_t t ) |
| 85 | +{ |
| 86 | + int32_t c; uint8_t *p = (uint8_t *)&c; |
| 87 | + p[ 1 ] = (R_(a) * t) >> 8; |
| 88 | + p[ 2 ] = (G_(a) * t) >> 8; |
| 89 | + p[ 0 ] = (B_(a) * t) >> 8; |
| 90 | + return c; |
| 91 | +} |
| 92 | + |
| 93 | +__attribute__( ( always_inline ) ) inline uint32_t LERP( uint32_t a, uint32_t b, int32_t t ) |
| 94 | +{ |
| 95 | + int32_t it = 255 - t; |
| 96 | + int32_t c; uint8_t *p = (uint8_t *)&c; |
| 97 | + p[ 1 ] = (R_(a) * t + R_(b) * it) >> 8; |
| 98 | + p[ 2 ] = (G_(a) * t + G_(b) * it) >> 8; |
| 99 | + p[ 0 ] = (B_(a) * t + B_(b) * it) >> 8; |
| 100 | + return c; |
| 101 | +} |
| 102 | + |
| 103 | +__attribute__( ( always_inline ) ) inline uint32_t ADD_CLAMP( const uint32_t a, const uint32_t b ) |
| 104 | +{ |
| 105 | + uint32_t result; |
| 106 | + asm volatile ( "uqadd8 %0, %1, %2" : "=r" (result) : "r" (a), "r" (b) ); |
| 107 | + return(result); |
| 108 | +} |
| 109 | + |
| 110 | +__attribute__( ( always_inline ) ) inline uint32_t HSV2RGB(uint32_t hsv) |
| 111 | +{ |
| 112 | + int32_t region, remainder, p, q, t; |
| 113 | + |
| 114 | + if (S_(hsv) == 0) |
| 115 | + return RGB( V_( hsv ), V_( hsv ), V_( hsv ) ); |
| 116 | + |
| 117 | + region = H_( hsv ) / 43; |
| 118 | + remainder = ( H_( hsv ) - (region * 43)) * 6; |
| 119 | + |
| 120 | + p = (V_( hsv ) * (255 - S_( hsv ))) / 256; |
| 121 | + q = (V_( hsv ) * (255*256 - ((S_( hsv ) * remainder)))) / 65536; |
| 122 | + t = (V_( hsv ) * (255*256 - ((S_( hsv ) * (255 - remainder))))) / 65536; |
| 123 | + |
| 124 | + switch (region) |
| 125 | + { |
| 126 | + case 0: |
| 127 | + return RGB( V_( hsv ), t, p ); |
| 128 | + case 1: |
| 129 | + return RGB( q, V_( hsv ), p ); |
| 130 | + case 2: |
| 131 | + return RGB( p, V_( hsv ), t ); |
| 132 | + case 3: |
| 133 | + return RGB( p, q, V_( hsv ) ); |
| 134 | + case 4: |
| 135 | + return RGB( t, p, V_( hsv ) ); |
| 136 | + default: |
| 137 | + return RGB( V_( hsv ), p, q ); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +__attribute__( ( always_inline ) ) inline uint32_t RGB2HSV( uint32_t rgb ) |
| 142 | +{ |
| 143 | + int32_t rgbMin, rgbMax, h, s, v; |
| 144 | + |
| 145 | + rgbMax = rgbMin = R_(rgb); |
| 146 | + if ( G_(rgb) > rgbMax ) rgbMax = G_(rgb); |
| 147 | + if ( G_(rgb) < rgbMin ) rgbMin = G_(rgb); |
| 148 | + if ( B_(rgb) > rgbMax ) rgbMax = B_(rgb); |
| 149 | + if ( B_(rgb) < rgbMin ) rgbMin = B_(rgb); |
| 150 | + v = rgbMax; |
| 151 | + |
| 152 | + if ( v == 0 ) |
| 153 | + return 0; |
| 154 | + |
| 155 | + s = 255 * ((int32_t)(rgbMax - rgbMin)) / v; |
| 156 | + |
| 157 | + int32_t diff = rgbMax - rgbMin; |
| 158 | + |
| 159 | + if ( s == 0 ) |
| 160 | + return HSV( 0, 0, v ); |
| 161 | + |
| 162 | + if (rgbMax == R_(rgb)) |
| 163 | + h = 0 + 43 * ((int32_t)G_(rgb) - (int32_t)B_(rgb)) / diff; else |
| 164 | + if (rgbMax == G_(rgb)) |
| 165 | + h = 85 + 43 * ((int32_t)B_(rgb) - (int32_t)R_(rgb)) / diff; else |
| 166 | + h = 171 + 43 * ((int32_t)R_(rgb) - (int32_t)G_(rgb)) / diff; |
| 167 | + |
| 168 | + h &= 255; |
| 169 | + return HSV( h, s, v ); |
| 170 | +} |
| 171 | + |
| 172 | +// |
| 173 | +// https://github.com/ekmett/approximate/blob/master/cbits/fast.c |
| 174 | +// |
| 175 | +__attribute__( ( always_inline ) ) inline float powf_fast(float a, float b) { |
| 176 | + union { float d; int x; } u = { a }; |
| 177 | + u.x = (int)(b * (u.x - 1064866805) + 1064866805); |
| 178 | + return u.d; |
| 179 | +} |
| 180 | + |
| 181 | +__attribute__( ( always_inline ) ) inline float powf_fast_lb(float a, float b) { |
| 182 | + union { float d; int x; } u = { a }; |
| 183 | + u.x = (int)(b * (u.x - 1065353217) + 1064631197); |
| 184 | + return u.d; |
| 185 | +} |
| 186 | + |
| 187 | +__attribute__( ( always_inline ) ) inline float powf_fast_ub(float a, float b) { |
| 188 | + union { float d; int x; } u = { a }; |
| 189 | + u.x = (int)(b * (u.x - 1064631197) + 1065353217); |
| 190 | + return u.d; |
| 191 | +} |
0 commit comments