|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Erik Bosman <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without restriction, |
| 7 | + * including without limitation the rights to use, copy, modify, |
| 8 | + * merge, publish, distribute, sublicense, and/or sell copies of the |
| 9 | + * Software, and to permit persons to whom the Software is furnished to |
| 10 | + * do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + * (http://opensource.org/licenses/mit-license.html) |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +#include <stdint.h> |
| 29 | + |
| 30 | +#include <libopencmsis/core_cm3.h> |
| 31 | + |
| 32 | +#include <libopencm3/stm32/rcc.h> |
| 33 | +#include <libopencm3/stm32/gpio.h> |
| 34 | + |
| 35 | +#include "ws2812_new.h" |
| 36 | + |
| 37 | +static void enable_sys_tick(uint32_t ticks) |
| 38 | +{ |
| 39 | + STK_RVR = ticks; |
| 40 | + STK_CVR = 0; |
| 41 | + STK_CSR = STK_CSR_ENABLE|STK_CSR_TICKINT; |
| 42 | +} |
| 43 | + |
| 44 | +volatile uint32_t tick=0; |
| 45 | +void SysTick_Handler(void) |
| 46 | +{ |
| 47 | + ws2812_write(); |
| 48 | + tick+=1; |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | +from math import tau, cos |
| 53 | +max_val=0xff00 |
| 54 | +gamma=2.6 |
| 55 | +
|
| 56 | +size = 256 |
| 57 | +wave = [ ( 1 - cos(x*tau/size) )/2 for x in range(size) ] |
| 58 | +
|
| 59 | +def clamp(x): |
| 60 | + return min(max_val, max(0, x)) |
| 61 | +
|
| 62 | +table = [ clamp(int(max_val * x**gamma)) for x in wave ] |
| 63 | +
|
| 64 | +for i in range(0, size, 8): |
| 65 | + print ( '\t'+' '.join('0x{:04x},'.format(x) for x in table[i:i+8]) ) |
| 66 | +
|
| 67 | +*/ |
| 68 | +uint16_t wave[256] = |
| 69 | +{ |
| 70 | + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
| 71 | + 0x0000, 0x0000, 0x0001, 0x0001, 0x0003, 0x0004, 0x0006, 0x0009, |
| 72 | + 0x000d, 0x0012, 0x0018, 0x0020, 0x0029, 0x0035, 0x0043, 0x0054, |
| 73 | + 0x0069, 0x0081, 0x009d, 0x00bd, 0x00e3, 0x010f, 0x0141, 0x0179, |
| 74 | + 0x01ba, 0x0202, 0x0253, 0x02ae, 0x0312, 0x0382, 0x03fe, 0x0486, |
| 75 | + 0x051b, 0x05be, 0x0670, 0x0732, 0x0804, 0x08e7, 0x09dc, 0x0ae4, |
| 76 | + 0x0c00, 0x0d2f, 0x0e74, 0x0fce, 0x113e, 0x12c6, 0x1465, 0x161c, |
| 77 | + 0x17ec, 0x19d4, 0x1bd7, 0x1df3, 0x202a, 0x227b, 0x24e6, 0x276d, |
| 78 | + 0x2a0f, 0x2ccb, 0x2fa3, 0x3295, 0x35a2, 0x38c9, 0x3c0a, 0x3f65, |
| 79 | + 0x42d9, 0x4666, 0x4a0a, 0x4dc5, 0x5197, 0x557e, 0x597a, 0x5d89, |
| 80 | + 0x61aa, 0x65dc, 0x6a1f, 0x6e6f, 0x72ce, 0x7737, 0x7bab, 0x8028, |
| 81 | + 0x84ab, 0x8934, 0x8dc0, 0x924f, 0x96dd, 0x9b69, 0x9ff2, 0xa475, |
| 82 | + 0xa8f1, 0xad63, 0xb1ca, 0xb623, 0xba6d, 0xbea6, 0xc2cb, 0xc6dc, |
| 83 | + 0xcad5, 0xceb5, 0xd27b, 0xd624, 0xd9af, 0xdd19, 0xe062, 0xe387, |
| 84 | + 0xe687, 0xe960, 0xec12, 0xee9a, 0xf0f8, 0xf32a, 0xf52f, 0xf705, |
| 85 | + 0xf8ad, 0xfa25, 0xfb6d, 0xfc83, 0xfd68, 0xfe1a, 0xfe99, 0xfee6, |
| 86 | + 0xff00, 0xfee6, 0xfe99, 0xfe1a, 0xfd68, 0xfc83, 0xfb6d, 0xfa25, |
| 87 | + 0xf8ad, 0xf705, 0xf52f, 0xf32a, 0xf0f8, 0xee9a, 0xec12, 0xe960, |
| 88 | + 0xe687, 0xe387, 0xe062, 0xdd19, 0xd9af, 0xd624, 0xd27b, 0xceb5, |
| 89 | + 0xcad5, 0xc6dc, 0xc2cb, 0xbea6, 0xba6d, 0xb623, 0xb1ca, 0xad63, |
| 90 | + 0xa8f1, 0xa475, 0x9ff2, 0x9b69, 0x96dd, 0x924f, 0x8dc0, 0x8934, |
| 91 | + 0x84ab, 0x8028, 0x7bab, 0x7737, 0x72ce, 0x6e6f, 0x6a1f, 0x65dc, |
| 92 | + 0x61aa, 0x5d89, 0x597a, 0x557e, 0x5197, 0x4dc5, 0x4a0a, 0x4666, |
| 93 | + 0x42d9, 0x3f65, 0x3c0a, 0x38c9, 0x35a2, 0x3295, 0x2fa3, 0x2ccb, |
| 94 | + 0x2a0f, 0x276d, 0x24e6, 0x227b, 0x202a, 0x1df3, 0x1bd7, 0x19d4, |
| 95 | + 0x17ec, 0x161c, 0x1465, 0x12c6, 0x113e, 0x0fce, 0x0e74, 0x0d2f, |
| 96 | + 0x0c00, 0x0ae4, 0x09dc, 0x08e7, 0x0804, 0x0732, 0x0670, 0x05be, |
| 97 | + 0x051b, 0x0486, 0x03fe, 0x0382, 0x0312, 0x02ae, 0x0253, 0x0202, |
| 98 | + 0x01ba, 0x0179, 0x0141, 0x010f, 0x00e3, 0x00bd, 0x009d, 0x0081, |
| 99 | + 0x0069, 0x0054, 0x0043, 0x0035, 0x0029, 0x0020, 0x0018, 0x0012, |
| 100 | + 0x000d, 0x0009, 0x0006, 0x0004, 0x0003, 0x0001, 0x0001, 0x0000, |
| 101 | + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
| 102 | +}; |
| 103 | + |
| 104 | +static void prepare_next_frame(frame_t *f) |
| 105 | +{ |
| 106 | + |
| 107 | +static uint8_t n = 0; |
| 108 | +n+=1; |
| 109 | + |
| 110 | + int i; |
| 111 | + for (i=0; i<N_VALUES; i+=3) |
| 112 | + { |
| 113 | + f->data[i] = wave[ (n)&0xff ]>>3; |
| 114 | + f->data[i+1] = wave[ (n+85)&0xff ]>>3; |
| 115 | + f->data[i+2] = wave[ (n+170)&0xff ]>>3; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +int main(void) |
| 120 | +{ |
| 121 | + rcc_clock_setup_in_hsi_out_48mhz(); |
| 122 | + rcc_periph_clock_enable(RCC_GPIOA); |
| 123 | + gpio_mode_setup(PORT_LED_DATA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MASK_LED_DATA); |
| 124 | + |
| 125 | + //enable_sys_tick(SYSTICK_PERIOD); |
| 126 | + enable_sys_tick(F_SYS_TICK_CLK/1000); |
| 127 | + ws2812_init(); |
| 128 | + |
| 129 | + uint32_t t_last=tick; |
| 130 | + |
| 131 | + frame_t *f = NULL; |
| 132 | + |
| 133 | + for(;;) |
| 134 | + { |
| 135 | + f = ws2812_get_frame(); |
| 136 | + |
| 137 | + if (f != NULL && (tick-t_last > 50) ) |
| 138 | + { |
| 139 | + prepare_next_frame(f); |
| 140 | + ws2812_swap_frame(); |
| 141 | + t_last += 50; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments