From 9e0a963355b123cc29440c23259395b337ef6d91 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Sat, 4 Apr 2015 04:33:29 -0400 Subject: [PATCH] very close. Just cleanup to go. --- embeddednf.h | 2 +- embeddedout.c | 147 +++++++++++++++++++++++++++++++++++++++++++------- embeddedout.h | 4 +- 3 files changed, 130 insertions(+), 23 deletions(-) diff --git a/embeddednf.h b/embeddednf.h index 6841ccb..bb64f7b 100644 --- a/embeddednf.h +++ b/embeddednf.h @@ -32,7 +32,7 @@ #define MAX_JUMP_DISTANCE 5 -#define AMP_1_NERFING_BITS 5 +#define AMP_1_NERFING_BITS 4 #define AMP_2_NERFING_BITS 3 //This is the amplitude, coming from folded_bins. If the value is below this diff --git a/embeddedout.c b/embeddedout.c index 6146214..d2406f3 100644 --- a/embeddedout.c +++ b/embeddedout.c @@ -3,6 +3,11 @@ //uint8_t ledArray[NUM_LIN_LEDS]; //Points to which notes correspond to these LEDs uint8_t ledOut[NUM_LIN_LEDS*3]; +uint16_t ledSpin; +uint16_t ledAmpOut[NUM_LIN_LEDS]; +uint8_t ledFreqOut[NUM_LIN_LEDS]; +uint8_t ledFreqOutOld[NUM_LIN_LEDS]; + void UpdateLinearLEDs() { //Source material: @@ -17,39 +22,64 @@ void UpdateLinearLEDs() //Color them according to value in note_peak_amps2. uint8_t i; - uint16_t j; + int8_t k; + uint16_t j, l; uint32_t total_size_all_notes = 0; int32_t porpamps[MAXNOTES]; //LEDs for each corresponding note. uint8_t sorted_note_map[MAXNOTES]; //mapping from which note into the array of notes from the rest of the system. - uint16_t local_peak_amps[MAXNOTES]; - uint16_t local_peak_amps2[MAXNOTES]; - uint8_t local_note_freq[MAXNOTES]; - uint8_t sorted_map_count = 0; for( i = 0; i < MAXNOTES; i++ ) { uint16_t ist = note_peak_amps[i]; - if( note_peak_freqs[i] == 255 || ist <= NERF_NOTE_SIZE_VALUE ) + uint8_t nff = note_peak_freqs[i]; + if( nff == 255 || ist <= NERF_NOTE_SIZE_VALUE ) { - local_peak_amps[i] = 0; continue; } for( j = 0; j < sorted_map_count; j++ ) { - //TODO SORT ME + if( note_peak_freqs[ sorted_note_map[j] ] > nff ) + { + break; + } } + for( k = sorted_map_count; k > j; k-- ) + { + sorted_note_map[k] = sorted_note_map[k-1]; + } + + sorted_note_map[j] = i; sorted_map_count++; } - for( i = 0; i < MAXNOTES; i++ ) +#if 0 + for( i = 0; i < sorted_map_count; i++ ) { - uint16_t ist = note_peak_amps[i]; + printf( "%d: %d: %d /", sorted_note_map[i], note_peak_freqs[sorted_note_map[i]], note_peak_amps[sorted_note_map[i]] ); + } + printf( "\n" ); +#endif + + uint16_t local_peak_amps[MAXNOTES]; + uint16_t local_peak_amps2[MAXNOTES]; + uint8_t local_peak_freq[MAXNOTES]; + + for( i = 0; i < sorted_map_count; i++ ) + { + local_peak_amps[i] = note_peak_amps[sorted_note_map[i]]; + local_peak_amps2[i] = note_peak_amps2[sorted_note_map[i]]; + local_peak_freq[i] = note_peak_freqs[sorted_note_map[i]]; + } + + for( i = 0; i < sorted_map_count; i++ ) + { + uint16_t ist = local_peak_amps[i]; porpamps[i] = 0; - if( note_peak_freqs[i] == 255 || ist <= NERF_NOTE_SIZE_VALUE ) + if( ist <= NERF_NOTE_SIZE_VALUE ) { local_peak_amps[i] = 0; continue; @@ -70,30 +100,107 @@ void UpdateLinearLEDs() uint32_t porportional = (uint32_t)(NUM_LIN_LEDS<<8)/((uint32_t)total_size_all_notes); uint16_t total_accounted_leds = 0; - for( i = 0; i < MAXNOTES; i++ ) + for( i = 0; i < sorted_map_count; i++ ) { - if( note_peak_freqs[i] == 255 ) continue; porpamps[i] = (local_peak_amps[i] * porportional) >> 8; total_accounted_leds += porpamps[i]; } int16_t total_unaccounted_leds = NUM_LIN_LEDS - total_accounted_leds; - for( i = 0; i < MAXNOTES && total_unaccounted_leds; i++ ) + for( i = 0; i < sorted_map_count && total_unaccounted_leds; i++ ) { - if( note_peak_freqs[i] == 255 ) continue; porpamps[i]++; total_unaccounted_leds--; } - //Now, we use porpamps to march through the LEDs, coloring them. + //Put the frequencies on a ring. j = 0; - for( i = 0; i < MAXNOTES; i++ ) + for( i = 0; i < sorted_map_count; i++ ) { while( porpamps[i] > 0 ) { - uint16_t amp = ((uint32_t)note_peak_amps2[i] * NOTE_FINAL_AMP) >> 8; + ledFreqOut[j] = local_peak_freq[i]; + ledAmpOut[j] = (local_peak_amps2[i]*NOTE_FINAL_AMP)>>8; + j++; + porpamps[i]--; + } + } + + //This part totally can't run on an embedded system. +#if LIN_WRAPAROUND + uint16_t midx = 0; + uint32_t mqty = 100000000; + for( j = 0; j < NUM_LIN_LEDS; j++ ) + { + uint32_t dqty; + uint16_t localj; + + dqty = 0; + localj = j; + for( l = 0; l < NUM_LIN_LEDS; l++ ) + { + int32_t d = (int32_t)ledFreqOut[localj] - (int32_t)ledFreqOutOld[l]; + if( d < 0 ) d *= -1; + if( d > (NOTERANGE>>1) ) { d = NOTERANGE - d + 1; } + dqty += ( d * d ); + + localj++; + if( localj == NUM_LIN_LEDS ) localj = 0; + } + + if( dqty < mqty ) + { + mqty = dqty; + midx = j; + } + } + + ledSpin = midx; + +#else + ledSpin = 0; +#endif + + j = ledSpin; + for( l = 0; l < NUM_LIN_LEDS; l++, j++ ) + { + if( j >= NUM_LIN_LEDS ) j = 0; + ledFreqOutOld[l] = ledFreqOut[j]; + + uint16_t amp = ledAmpOut[j]; + if( amp > 255 ) amp = 255; + uint32_t color = ECCtoHEX( ledFreqOut[j], 255, amp ); + ledOut[l*3+0] = ( color >> 0 ) & 0xff; + ledOut[l*3+1] = ( color >> 8 ) & 0xff; + ledOut[l*3+2] = ( color >>16 ) & 0xff; + } +/* j = ledSpin; + for( i = 0; i < sorted_map_count; i++ ) + { + while( porpamps[i] > 0 ) + { + uint16_t amp = ((uint32_t)local_peak_amps2[i] * NOTE_FINAL_AMP) >> 8; if( amp > 255 ) amp = 255; - uint32_t color = ECCtoHEX( note_peak_freqs[i], 255, amp ); + uint32_t color = ECCtoHEX( local_peak_freq[i], 255, amp ); + ledOut[j*3+0] = ( color >> 0 ) & 0xff; + ledOut[j*3+1] = ( color >> 8 ) & 0xff; + ledOut[j*3+2] = ( color >>16 ) & 0xff; + + j++; + if( j == NUM_LIN_LEDS ) j = 0; + porpamps[i]--; + } + }*/ + + //Now, we use porpamps to march through the LEDs, coloring them. +/* j = 0; + for( i = 0; i < sorted_map_count; i++ ) + { + while( porpamps[i] > 0 ) + { + uint16_t amp = ((uint32_t)local_peak_amps2[i] * NOTE_FINAL_AMP) >> 8; + if( amp > 255 ) amp = 255; + uint32_t color = ECCtoHEX( local_peak_freq[i], 255, amp ); ledOut[j*3+0] = ( color >> 0 ) & 0xff; ledOut[j*3+1] = ( color >> 8 ) & 0xff; ledOut[j*3+2] = ( color >>16 ) & 0xff; @@ -101,7 +208,7 @@ void UpdateLinearLEDs() j++; porpamps[i]--; } - } + }*/ } diff --git a/embeddedout.h b/embeddedout.h index 7d52369..9d68137 100644 --- a/embeddedout.h +++ b/embeddedout.h @@ -8,11 +8,11 @@ #define NOTE_FINAL_AMP 255 //Number from 0...255 //Controls, basically, the minimum size of the splotches. -#define NERF_NOTE_SIZE_VALUE 10 +#define NERF_NOTE_SIZE_VALUE 1 #define NUM_LIN_LEDS 296 -#define LIN_WRAPAROUND 0 //Whether the output lights wrap around. (TODO) +#define LIN_WRAPAROUND 1 //Whether the output lights wrap around. extern uint8_t ledArray[]; extern uint8_t ledOut[]; //[NUM_LIN_LEDS*3]