@@ -21,17 +21,76 @@ const VERSION_CMD = 0x20;
2121const WIDTH = 9 ;
2222const HEIGHT = 34 ;
2323
24- const PATTERNS = [
25- 'Custom' ,
26- 'Blank' ,
27- 'Full' ,
28- 'Checkerboard' ,
29- 'Double Checkerboard' ,
30- 'Every 2nd Row' ,
31- 'Every 3rd Row' ,
32- 'Every 2nd Col' ,
33- 'Every 3rd Col' ,
34- ] ;
24+ const double_words = [ '' , '' , 'Double ' , 'Triple ' , 'Quad ' ] ;
25+ const patterns = {
26+ Blank : {
27+ each : ( row , col ) => 1 ,
28+ } ,
29+ Full : {
30+ each : ( row , col ) => 0 ,
31+ } ,
32+ ...[ 1 , 2 , 3 , 4 ] . reduce ( ( o , n ) => {
33+ o [ `${ double_words [ n ] } Checkerboard` ] = {
34+ each : ( row , col ) => ( row % ( 2 * n ) < n )
35+ ? ( col + n ) % ( 2 * n ) < n
36+ : col % ( 2 * n ) < n ,
37+ } ;
38+ return o ;
39+ } , { } ) ,
40+ ...[ 1 , 2 , 3 , 4 ] . reduce ( ( o , n ) => {
41+ o [ `${ double_words [ n ] } Diagonal Lines` ] = {
42+ each : ( row , col ) => Math . floor ( ( row + col ) / n ) % 2 == 0 ,
43+ } ;
44+ return o ;
45+ } , { } ) ,
46+ 'Every 2nd Row' : {
47+ each : ( row , col ) => row % 2 !== 0 ,
48+ } ,
49+ 'Every 3rd Row' : {
50+ each : ( row , col ) => row % 3 !== 0 ,
51+ } ,
52+ 'Every 2nd Col' : {
53+ each : ( row , col ) => col % 2 !== 0 ,
54+ } ,
55+ 'Every 3rd Col' : {
56+ each : ( row , col ) => col % 3 !== 0 ,
57+ } ,
58+ 'Diamond Pattern' : {
59+ each : ( row , col ) => {
60+ const size = 5 ;
61+ const repeatHeight = 10 ; // Adjust this value to control vertical repetition
62+ const totalCols = WIDTH ; // Width of your LED matrix
63+
64+ const centerCol = Math . floor ( totalCols / 2 ) ;
65+ const adjustedRow = row % repeatHeight ;
66+ const centerRow = Math . floor ( repeatHeight / 2 ) ;
67+
68+ return Math . abs ( adjustedRow - centerRow ) + Math . abs ( col - centerCol ) < size ;
69+ } ,
70+ } ,
71+ 'Scatter 50%' : {
72+ fn : ( { matrix } ) => {
73+ // Start blank
74+ for ( let row = 0 ; row < HEIGHT ; row ++ )
75+ for ( let col = 0 ; col < WIDTH ; col ++ )
76+ matrix [ row ] [ col ] = 1 ;
77+
78+ // Place a fixed number of random dots (50%)
79+ const count = Math . floor ( ( WIDTH * HEIGHT ) / 2 ) ;
80+ let placed = 0 ;
81+ while ( placed < count ) {
82+ const row = Math . floor ( Math . random ( ) * HEIGHT ) ;
83+ const col = Math . floor ( Math . random ( ) * WIDTH ) ;
84+ if ( matrix [ row ] [ col ] === 1 ) {
85+ matrix [ row ] [ col ] = 0 ;
86+ placed ++ ;
87+ }
88+ }
89+ } ,
90+ } ,
91+ } ;
92+
93+ const PATTERNS = Object . keys ( patterns ) ;
3594
3695var matrix_left ;
3796var matrix_right ;
@@ -67,33 +126,39 @@ $(function() {
67126 }
68127} ) ;
69128
70- function drawPattern ( matrix , pattern , pos ) {
129+ const makeCell = ( { matrix } ) => ( { row, col } ) => {
130+ return {
131+ row,
132+ col,
133+ set ( v ) {
134+ matrix [ row ] [ col ] = v ;
135+ }
136+ }
137+ }
138+
139+ const patternIter = function * ( { matrix } ) {
71140 for ( let col = 0 ; col < WIDTH ; col ++ ) {
72- for ( let row = 0 ; row < HEIGHT ; row ++ ) {
73- if ( pattern == 'Blank' ) {
74- matrix [ row ] [ col ] = 1 ;
75- } else if ( pattern == 'Full' ) {
76- matrix [ row ] [ col ] = 0 ;
77- } else if ( pattern == 'Checkerboard' ) {
78- if ( row % 2 == 0 )
79- matrix [ row ] [ col ] = col % 2 == 0 ;
80- else
81- matrix [ row ] [ col ] = ( col + 1 ) % 2 == 0 ;
82- } else if ( pattern == 'Double Checkerboard' ) {
83- if ( row % 4 < 2 )
84- matrix [ row ] [ col ] = ( col + 2 ) % 4 < 2 ;
85- else
86- matrix [ row ] [ col ] = ( col ) % 4 < 2 ;
87- } else if ( pattern == 'Every 2nd Row' ) {
88- matrix [ row ] [ col ] = row % 2 != 0 ;
89- } else if ( pattern == 'Every 3rd Row' ) {
90- matrix [ row ] [ col ] = row % 3 != 0 ;
91- } else if ( pattern == 'Every 2nd Col' ) {
92- matrix [ row ] [ col ] = col % 2 != 0 ;
93- } else if ( pattern == 'Every 3rd Col' ) {
94- matrix [ row ] [ col ] = col % 3 != 0 ;
141+ for ( let row = 0 ; row < HEIGHT ; row ++ ) {
142+ yield makeCell ( { matrix } ) ( { row, col } ) ;
143+ }
144+ }
145+ }
146+
147+ function drawPattern ( matrix , pattern , pos ) {
148+ if ( patterns [ pattern ] ) {
149+ // Support patterns with an 'each' function defined.
150+ // These are patterns defined by a simple expression on (row, col).
151+ if ( patterns [ pattern ] . each ) {
152+ for ( const cell of patternIter ( { matrix } ) ) {
153+ cell . set ( patterns [ pattern ] . each ( cell . row , cell . col ) ) ;
95154 }
96155 }
156+ // Support patterns with an 'fn' function defined.
157+ // These are patterns which have their own iterators, which can be
158+ // useful for recursion or carrying state across iterations.
159+ else if ( patterns [ pattern ] . fn ) {
160+ patterns [ pattern ] . fn ( { matrix } ) ;
161+ }
97162 }
98163 updateMatrix ( matrix , pos ) ;
99164}
0 commit comments