From 2e2d165c628a17a3dda597f6ff2e756dd413de6f Mon Sep 17 00:00:00 2001 From: Lurkars Date: Wed, 30 Dec 2020 23:04:08 +0100 Subject: [PATCH] clean leds --- bikewheel.html | 17 ++++++++--------- led_bikewheel.ino | 16 +++++++++++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/bikewheel.html b/bikewheel.html index 3f2d926..4913390 100644 --- a/bikewheel.html +++ b/bikewheel.html @@ -39,7 +39,7 @@ let canvas_out_elem = document.getElementById("canvas_out"); let canvas_out2_elem = document.getElementById("canvas_out2"); - + // helper for printing number as hex value let intToHex = function (color, int16) { let text = color.toString(16); @@ -64,8 +64,8 @@ let canvas_context = canvas_elem.getContext("2d"); canvas_context.drawImage(image, 0, 0); - const num_segments = num_segments_elem.value; - const num_leds = num_leds_elem.value; + const num_segments = parseInt(num_segments_elem.value); + const num_leds = parseInt(num_leds_elem.value); const led_offset = Math.floor(led_offset_elem.value); const offset_x = image.width / 2; @@ -76,7 +76,7 @@ let canvas_out_context = canvas_out_elem.getContext("2d"); canvas_out_elem.width = image.width; canvas_out_elem.height = image.height; - let out_data = canvas_out_context.createImageData(image.width, image.height); + let out_data = canvas_out_context.createImageData(canvas_out_elem.width, canvas_out_elem.height); let canvas_out2_context = canvas_out2_elem.getContext("2d"); @@ -88,17 +88,16 @@ for (let led = 0; led < num_leds; led++) { for (let seg = 0; seg < num_segments; seg++) { - let x = offset_x + (led_offset + led) * Math.cos((seg * (1 / num_segments) * 360) * (Math.PI / 180)); - let y = offset_y + (led_offset + led) * Math.sin((seg * (1 / num_segments) * 360) * (Math.PI / 180)); + let x = Math.floor(offset_x + (led_offset + led) * Math.cos((seg * (1 / num_segments) * 360) * (Math.PI / 180))); + let y = Math.floor(offset_y + (led_offset + led) * Math.sin((seg * (1 / num_segments) * 360) * (Math.PI / 180))); if (!data[seg]) { data[seg] = new Array(); } - // data[seg][led] = Math.floor(x) + ":" + Math.floor(y); - data[seg][led] = canvas_context.getImageData(Math.floor(x), Math.floor(y), 1, 1).data; + data[seg][led] = canvas_context.getImageData(x, y, 1, 1).data; for (let i = 0; i < 3; i++) { data[seg][led][i] = data[seg][led][i] - (data[seg][led][i] % 32); } - let index = 4 * (Math.floor(x) + Math.floor(y) * image.width); + let index = 4 * (x + y * canvas_out_elem.width); out_data.data[index] = data[seg][led][0]; out_data.data[index + 1] = data[seg][led][1]; out_data.data[index + 2] = data[seg][led][2]; diff --git a/led_bikewheel.ino b/led_bikewheel.ino index 3504008..964ff7a 100644 --- a/led_bikewheel.ino +++ b/led_bikewheel.ino @@ -1,7 +1,7 @@ // Copyright 2020 Lukas Haubaum // // Licensed under MIT license -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,8 +31,8 @@ #define LED_COUNT 26 // how many LEDs on one stripe #define LED_STRIPES 2 // how many LEDs stripes on wheel -int strip_matrix_offset[LED_STRIPES] = {0, 0}; // given offset in segments for stripe -bool strip_matrix_invert[LED_STRIPES] = {false, true}; // set LEDs in revert order for stripe +int strip_matrix_offset[LED_STRIPES] = {0, 0}; // given offset in segments for stripe +bool strip_matrix_invert[LED_STRIPES] = {false, true}; // set LEDs in revert order for stripe // \CONFIGURE // LED stripes @@ -44,8 +44,8 @@ Adafruit_NeoPixel ledStrip(LED_COUNT *LED_STRIPES, LED_PIN, NEO_GRB + NEO_KHZ800 #define LED_DATA_PIN 3 // data pin for LED strip #define LED_CLOCK_PIN 2 // clock pin for LED strip APA102 ledStrip; -#define BRIGHTNESS 1 // brightness for LED strip [0-31] -rgb_color color_buffer[LED_COUNT * LED_STRIPES]; // color buffer to write to LED stripe +#define BRIGHTNESS 1 // brightness for LED strip [0-31] +rgb_color color_buffer[LED_COUNT * LED_STRIPES]; // color buffer to write to LED stripe #endif float passed = 0; @@ -67,6 +67,12 @@ void setup() ledStrip.begin(); ledStrip.show(); ledStrip.setBrightness(BRIGHTNESS); +#else + for (int i = 0; i < LED_COUNT * 4; i++) // loop over all LEDs + { + color_buffer[i] = rgb_color(0, 0, 0); // set to black + } + ledStrip.write(color_buffer, LED_COUNT * LED_STRIPES, 0); // write buffer to stripes #endif }