-
Notifications
You must be signed in to change notification settings - Fork 9
/
PICxel.cpp
1176 lines (908 loc) · 34.4 KB
/
PICxel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* PICxel.cpp - PIC32 Neopixel Library */
/* */
/* A simple to use library for addressable LEDs like the WS2812 for */
/* the PIC32 line of microcontrollers. */
/* */
/* tested supported boards: */
/* - Digilent UNO32 */
/* - Digilent UC32 */
/* */
/* This library is protected under the GNU GPL v3.0 license */
/* http://www.gnu.org/licenses/ */
/* */
/* 06/05/2017 Brian Schmalz Added support for all chipKIT boards, as */
/* well as simplifying the main GRBrefreshLEDs()*/
/* function. */
/* Tested on 40, 48, 80 and 200 MHz boards */
/************************************************************************/
#include "PICxel.h"
/************************************************************************/
/* Construction for the PICxel class */
/************************************************************************/
PICxel::PICxel(uint16_t num, uint8_t pin, color_mode_t colorMode) : numberOfLEDs(num),
pin(pin), colorArray(NULL), portSet(portOutputRegister(digitalPinToPort(pin)) + 2),
portClr(portOutputRegister(digitalPinToPort(pin)) + 1),
pinMask(digitalPinToBitMask(pin)), brightness(255), colorMode(colorMode){
if(colorMode == GRB){
numberOfBytes = 3*num;
//uint8_t colorArray[3*num];
colorArray = (uint8_t*)calloc(numberOfBytes, sizeof(uint8_t));
}
else{
numberOfBytes = 4*num;
//uint8_t colorArray[4*num];
colorArray = (uint8_t*)calloc(numberOfBytes, sizeof(uint8_t));
}
for(int i=0;i<numberOfBytes; i++)
colorArray[i] = 0;
}
PICxel::PICxel(uint16_t num, uint8_t pin, color_mode_t colorMode, memory_mode_t memory_mode) :
numberOfLEDs(num), pin(pin), colorArray(NULL),
portSet(portOutputRegister(digitalPinToPort(pin)) + 2),
portClr(portOutputRegister(digitalPinToPort(pin)) + 1),
pinMask(digitalPinToBitMask(pin)), brightness(255), colorMode(colorMode){
if(colorMode == GRB && memory_mode == alloc){
numberOfBytes = 3*num;
colorArray = (uint8_t*)calloc(numberOfBytes, sizeof(uint8_t));
}
else if(colorMode == HSV && memory_mode == alloc){
numberOfBytes = 4*num;
colorArray = (uint8_t*)calloc(numberOfBytes, sizeof(uint8_t));
}
else if(colorMode == GRB && memory_mode == noalloc){
numberOfBytes = 3*num;
}
else{ //(colorMode == GRB && memory_mode == noalloc)
numberOfBytes = 4*num;
}
}
void PICxel::setArrayPointer(uint8_t* colorPtr){
colorArray = colorPtr;
}
/************************************************************************/
/* Destructor for the PICxel class */
/************************************************************************/
PICxel::~PICxel(){
}
/************************************************************************/
/* Sets the pinMode for selected pin */
/************************************************************************/
void PICxel::begin(){
//let MPIDE handle the tri-state buffer and
//assign analog inputs as needed
pinMode(pin, OUTPUT);
//clear the pin
*portClr = pinMask;
}
/************************************************************************/
/* Clears the color array in either GRB or HSV mode */
/************************************************************************/
void PICxel::clear(){
uint8_t* arrayPtr = colorArray;
if(colorMode == GRB)
for(int i=0; i < numberOfLEDs*3; i++)
arrayPtr[i] = 0;
else
for(int i = 0; i < numberOfLEDs*4; i++)
arrayPtr[i] = 0;
}
/************************************************************************/
/* Clears the LED in the colorArray */
/************************************************************************/
void PICxel::clear(uint8_t num){
if(colorMode == GRB){
colorArray[(num*3)+0] = 0;
colorArray[(num*3)+1] = 0;
colorArray[(num*3)+2] = 0;
}
else{
colorArray[(num*4)+0] = 0;
colorArray[(num*4)+1] = 0;
colorArray[(num*4)+2] = 0;
colorArray[(num*4)+3] = 0;
}
}
/************************************************************************/
/* sets the class brightness */
/************************************************************************/
void PICxel::setBrightness(uint8_t b){
brightness = b;
}
/************************************************************************/
/* Modifies the color matrix with the colors presented in 8-bit values */
/* for green, red and blue. The values are then scaled by the */
/* brightness value and placed into the location of number */
/************************************************************************/
void PICxel::GRBsetLEDColor(uint16_t number, uint8_t green, uint8_t red, uint8_t blue){
if(number < numberOfLEDs){
red = ((red*brightness) >> 8);
green = ((green*brightness) >> 8);
blue = ((blue*brightness) >> 8);
uint8_t *arrayPtr = &colorArray[number*3];
arrayPtr[0] = green;
arrayPtr[1] = red;
arrayPtr[2] = blue;
}
}
/************************************************************************/
/* Modifies the color matrix with the colors presented one 32-bit */
/* value. color is a 32-bit unsigned int that is organized into four */
/* bytes: */
/* bits[31 - 24][23 - 16][15 - 8][7 - 0] */
/* ( blank )( red )(green )(blue ) */
/* */
/* Each color is scaled by the brightness value and stored in the */
/* color array. */
/************************************************************************/
void PICxel::GRBsetLEDColor(uint16_t number, uint32_t color){
if(number < numberOfLEDs){
uint32_t red = (uint8_t)(color >> 8);
uint32_t green = (uint8_t)(color >> 16);
uint32_t blue = (uint8_t)(color);
red = (red * brightness) >> 8;
green = (green * brightness) >> 8;
blue = (blue * brightness) >> 8;
uint8_t *arrayPtr = &colorArray[number*3];
arrayPtr[0] = green;
arrayPtr[1] = red;
arrayPtr[2] = blue;
}
}
/************************************************************************/
/* Modifies the color matrix with the passed values hue, sat and val. */
/* */
/* Hue is a 16-bit value that ranges from 0-1535 representing the */
/* color wheel. 255 values per 60 degrees of the color wheel. sat */
/* val are 8-bit values that control saturation and value. If */
/* saturation is 0 then the LED will be off. */
/************************************************************************/
void PICxel::HSVsetLEDColor(uint16_t number, uint16_t hue, uint8_t sat, uint8_t val){
if(number < numberOfLEDs){
uint8_t *arrayPtr = &colorArray[number*4];
arrayPtr[0] = hue;
arrayPtr[1] = hue >> 8;
arrayPtr[2] = sat;
arrayPtr[3] = val;
}
}
/************************************************************************/
/* Modifies the color matrix with the colors presented one 32-bit */
/* value. color is a 32-bit unsigned int that is organized into four */
/* bytes: */
/* bits[ 31 - 24 ][ 23 - 16 ][15 - 8][7 - 0] */
/* (upper hue)(lower hue)( sat )( val ) */
/* */
/* Hue is a 16-bit value that ranges from 0-1535 representing the */
/* color wheel. 255 values per 60 degrees of the color wheel. sat */
/* val are 8-bit values that control saturation and value. If */
/* saturation is 0 then the LED will be off. */
/************************************************************************/
void PICxel::HSVsetLEDColor(uint16_t number, uint32_t color){
if(number < numberOfLEDs){
uint8_t *arrayPtr = &colorArray[number*4];
arrayPtr[0] = color;
arrayPtr[1] = color >> 8;
arrayPtr[2] = color >> 16;
arrayPtr[3] = color >> 24;
}
}
/************************************************************************/
/* Refreshed the LED strip with either GRBrefreshLEDs() or */
/* HSVrefreshLEDs() dependent on which color mode to use */
/************************************************************************/
void PICxel::refreshLEDs(void){
if(colorMode == GRB)
GRBrefreshLEDs();
else
HSVrefreshLEDs();
}
/************************************************************************/
/* Generate the datastream to refresh the LEDs using the RGB color */
/* mode. This function utilizes MIPS assembly no-op commands to */
/* ensure that the specific timing is met. */
/* */
/* For each bit in each byte, a pair of high and low delays is used. */
/* */
/* There are preprocessor defined macros to control the variable */
/* number of no-ops required to meeting timing as easily as possible. */
/* preprocessor defined macros will allow for easier porting to other */
/* ChipKIT boards. */
/************************************************************************/
void PICxel::GRBrefreshLEDs(void){
uint32_t interruptBits;
uint8_t* colorArrayPtr = colorArray;
uint8_t bitSelect;
/* Disable interrupts, but save current bits so we can restore them later */
interruptBits = disableInterrupts();
for(int j = 0; j < numberOfBytes; j++)
{
bitSelect = 0x80;
while(bitSelect)
{
if(*colorArrayPtr & bitSelect)
{
*portSet = pinMask;
GRB_delay_T1H();
*portClr = pinMask;
GRB_delay_T1L();
}
else
{
*portSet = pinMask;
GRB_delay_T0H();
*portClr = pinMask;
GRB_delay_T0L();
}
bitSelect = (bitSelect >> 1);
}
colorArrayPtr++;
}
/* Restore the interrupts now */
restoreInterrupts(interruptBits);
}
/************************************************************************/
/* Generate the data stream to refresh the LEDs using the HSV color */
/* mode. This function utilizes MIPS assembly to perform the */
/* HSV to GRB conversion for the next LED while sending data for the */
/* current LED. */
/* */
/* The overall structure is simple but is difficult to read with the */
/* HSV to GRB conversion inline. In each frame of the bitstream there */
/* are three components that allow for easier manipulation: */
/* - high sector - the first 350ns of the bitframe that is high */
/* regardless of the data bit being a 1 or 0 */
/* - variable sector - the second 350ns of the bitframe that is high */
/* if a 1 and low if a zero */
/* - low sector - third and final 550ns of the bitframe that is low */
/* regardless of the data bit being a 1 or 0 */
/* */
/* There are preprocessor defined macros to control the variable */
/* number of no-ops required to meeting timing as easily as possible. */
/* preprocessor defined macros will allow for easier porting to other */
/* ChipKIT boards. */
/************************************************************************/
void PICxel::HSVrefreshLEDs(void){
//do not allow bitstream to be interrupted
noInterrupts();
asm volatile(
"lw $s0, %4 \n\t" //load address of color_ptr
//compute first color
"j computeFirstColor \n\t"
"colorComputed: \n\t"
//loop through all LEDs
"forLoop: \n\t"
"move $t0, $t1 \n\t" //move the next_color to current_color
"move $t1, $zero \n\t" //clear next_color
"addi $s0, $s0, 4 \n\t"
"lw $t2, ($s0) \n\t"
"move $t6, $zero \n\t" //clear t6 for a byte iterator
"color_loop: \n\t"
////////////////////
//Bit 0
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
"andi $t3, $t2, 0xFFFF \n\t" //get hue
"srl $t4, $t2, 16 \n\t" //get saturation
"andi $t4, $t4, 0xFF \n\t"
"addi $t4, $t4, 1 \n\t"
"srl $t5, $t2, 24 \n\t" //get value
"andi $t5, $t5, 0xFF \n\t"
"addi $t5, $t5, 1 \n\t"
"multu $t5, $t4 \n\t" //chroma = sat * val
"mflo $t4 \n\t"
"srl $t4, $t4, 8 \n\t"
"subu $t5, $t5, $t4 \n\t" //m_num = value - chroma
//delay for the end of the first section
HSV_bit_0_delay_0 //preprocessor macro
"andi $t7, $t0, 0b10000000 \n\t" //bitmask out the value
"bne $t7, $zero, bit0IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit0IsHigh: \n\t"
//delay for the end of the second section
HSV_bit_0_delay_1 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if(hue < 256)
"li $t7, 256 \n\t" //load hue
"sub $t7, $t3, $t7 \n\t" //store hue - 256
"bgez $t7, didNotMakeHue0 \n\t" //if hue>256 branch
//hue < 256
"multu $t3, $t4 \n\t" //set green
"mflo $t1 \n\t"
"srl $t1, $t1, 8 \n\t"
"add $t1, $t1, $t5 \n\t"
"addu $t7, $t4, $t5 \n\t" //set red
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 8 \n\t"
"andi $t7, $t7, 0xFF00 \n\t"
"or $t1, $t1, $t7 \n\t"
"andi $t7, $t5, 0xFF \n\t"
"sll $t7, $t5, 16 \n\t" //set blue
"or $t1, $t7, $t1 \n\t"
//delay for the end of the third section when the hue has been made
HSV_bit_0_delay_2 //preprocessor macro
"j madeHue0 \n\t"
"didNotMakeHue0: \n\t"
//delay for the end of the third section when the hue has not been made
HSV_bit_0_delay_3 //preprocessor macro
"madeHue0: \n\t"
////////////////////
//Bit 1
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b01000000 \n\t" //bitmask out the value
"bne $t7, $zero, bit1IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit1IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if(next_color == 0) then continue
"bne $t1, $zero, huePrevMade1 \n\t"
//if(hue < 512)
"li $t7, 512 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, didNotMakeHue1 \n\t"
//hue < 512
"addu $t7, $t4, $t5 \n\t" //set green
"addi $t7, $t7, -1 \n\t"
"andi $t1, $t7, 0xFF \n\t"
"li $t7, 511 \n\t" //set red
"subu $t7, $t7, $t3 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"and $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t7, $t1 \n\t"
"sll $t7, $t5, 16 \n\t" //set blue
"or $t1, $t7, $t1 \n\t"
//delay for the end of the third section when hue is made
HSV_bit_1_delay_0
"j madeHue1 \n\t"
"huePrevMade1: \n\t"
//delay for the end of the third section hue was made earlier
HSV_bit_1_delay_1
"j madeHue1 \n\t"
"didNotMakeHue1: \n\t"
//delay for the end of the third section hue has not been made
HSV_bit_1_delay_2
"madeHue1: \n\t"
////////////////////
//Bit 2
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b00100000 \n\t" //bitmask out the value
"bne $t7, $zero, bit2IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit2IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"nop\n"
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if(next_color == 0) then continue
"bne $t1, $zero, huePrevMade2 \n\t"
//if(hue < 768)
"li $t7, 768 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, didNotMakeHue2 \n\t"
//hue < 768
"addu $t7, $t4, $t5 \n\t" //set green
"addi $t7, $t7, -1 \n\t"
"andi $t1, $t7, 0xFF \n\t"
"sll $t7, $t5, 8 \n\t" //set red
"or $t1, $t7, $t1 \n\t"
"li $t7, 512 \n\t" //set blue
"subu $t7, $t3, $t7 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"andi $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
//delay for the end of the third section when hue is made
HSV_bit_2_delay_0 //preprocessor macro
"j madeHue2 \n\t"
"huePrevMade2: \n\t"
//delay for the end of the third section hue was made earlier
HSV_bit_2_delay_1 //preprocessor macro
"j madeHue2 \n\t"
"didNotMakeHue2: \n\t"
//delay for the end of the third section when hue is not made
HSV_bit_2_delay_2 //preprocessor macro
"madeHue2: \n\t"
////////////////////
//Bit 3
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b00010000 \n\t" //
"bne $t7, $zero, bit3IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit3IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if(next_color == 0) then continue
"bne $t1, $zero, huePrevMade3 \n\t"
//if(hue < 1024)
"li $t7, 1024 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, didNotMakeHue3 \n\t"
//hue < 1024
"li $t7, 1023 \n\t" //set green
"subu $t7, $t7, $t3 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t1, $t7, $t5 \n\t"
//"and $t7, $t7, 0xFF \n\t"
"sll $t7, $t5, 8 \n\t" //set red
"or $t1, $t7, $t1 \n\t"
"addu $t7, $t4, $t5 \n\t" //set blue
"addi $t7, $t7, -1 \n\t"
"andi $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
//delay for the end of the third section when hue is made
HSV_bit_3_delay_0
"j madeHue3 \n\t"
"huePrevMade3: \n\t"
//delay for the end of the third section hue was made earlier
HSV_bit_3_delay_1
"j madeHue3 \n\t"
"didNotMakeHue3: \n\t"
//delay for the end of the third section when hue is not made
HSV_bit_3_delay_2
"madeHue3: \n\t"
////////////////////
//Bit 4
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b00001000 \n\t" //
"bne $t7, $zero, bit4IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit4IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if next_color is not zero then branch
"bne $t1, $zero, huePrevMade4 \n\t"
//if(hue < 1280)
"li $t7, 1280 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, didNotMakeHue4 \n\t"
//hue < 1280
"andi $t1, $t5, 0xFF \n\t" //set green
"li $t7, 1024 \n\t" //set red
"subu $t7, $t3, $t7 \n\t"
"multu $t7, $t4 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
"addu $t7, $t4, $t5 \n\t" //set blue
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
//delay for the end of the third section when hue is made
HSV_bit_4_delay_0 //preprocessor macro
"j madeHue4 \n\t"
"huePrevMade4: \n\t"
//delay for the end of the third section hue was made earlier
HSV_bit_4_delay_1 //preprocessor macro
"j madeHue4 \n\t"
"didNotMakeHue4: \n\t"
//delay for the end of the third section when hue is not made
HSV_bit_4_delay_2
"madeHue4: \n\t"
////////////////////
//Bit 5
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b0000100 \n\t" //
"bne $t7, $zero, bit5IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit5IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"nop \n"
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//if next_color is not zero then branch
"bne $t1, $zero, huePrevMade5 \n\t"
//if(hue < 1536)
"li $t7, 1536 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, didNotMakeHue5 \n\t"
//hue < 1536
"andi $t1, $t5, 0xFF \n\t" //set green
"addu $t7, $t4, $t5 \n\t" //set red
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
"li $t7, 1535 \n\t" //set blue
"subu $t7, $t7, $t3 \n\t"
"multu $t7, $t4 \n\t"
"mflo $t7 \n\t"
"andi $t7, $t7, 0xFF00 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
//delay for the end of the third section when hue is made
HSV_bit_5_delay_0 //preprocessor macro
"j madeHue5 \n\t"
"huePrevMade5: \n\t"
//delay for the end of the third section hue was made earlier
HSV_bit_5_delay_1 //preprocessor macro
"j madeHue5 \n\t"
"didNotMakeHue5: \n\t"
//delay for the end of the third section when hue is not made
HSV_bit_5_delay_2 //preprocessor macro
"madeHue5: \n\t"
////////////////////
//Bit 6
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b00000010 \n\t" //
"bne $t7, $zero, bit6IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit6IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"srl $t7, $t2, 24 \n\t" //get value
"andi $t7, $t7, 0xFF \n\t"
"bne $t7, $zero, zero_val_check_1 \n\t"
"move $t1, $zero \n\t"
"zero_val_check_1: \n\t"
//delay for the end of the second section
HSV_universal_delay_3 //preprocessor macro
////////////////////
//Bit 7
////////////////////
"lw $t7, %3 \n\t" //set pin
"sw %0, ($t7) \n\t"
//delay for the end of the first section
HSV_universal_delay_1 //preprocessor macro
"andi $t7, $t0, 0b00000001 \n\t" //
"bne $t7, $zero, bit7IsHigh \n\t" //branch if bit is high, branch over set low
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
"bit7IsHigh: \n\t"
//delay for the end of the second section
HSV_universal_delay_2 //preprocessor macro
"lw $t7, %2 \n\t" //clear pin
"sw %0, ($t7) \n\t"
//delay for the end of the second section
HSV_universal_delay_3 //preprocessor macro
//shift the data to the next color byte
"srl $t0, $t0, 8 \n\t"
"addi $t6, $t6, 1 \n\t" //iterate t6
"li $t7, 3 \n\t" //load
"bne $t7, $t6, color_loop \n\t"
///////////////////////////////
//end of data stream
"addi %1, %1, -1 \n\t" //decrement the led
"bgt %1, $zero, forLoop \n\t"
//end of forLoop:
"j HSVassemblyEnd \n\t"
"computeFirstColor: \n\t"
//color computation
"lw $t2, ($s0) \n\t" //dereference color_ptr address
"move $t1, $zero \n\t" //clear next color
"andi $t3, $t2, 0xFFFF \n\t" //get hue
"srl $t4, $t2, 16 \n\t" //get saturation
"andi $t4, $t4, 0xFF \n\t"
"addi $t4, $t4, 1 \n\t"
"srl $t5, $t2, 24 \n\t" //get value
"andi $t5, $t5, 0xFF \n\t"
"addi $t5, $t5, 1 \n\t"
"multu $t5, $t4 \n\t" //chroma = sat * val
"mflo $t4 \n\t"
"srl $t4, $t4, 8 \n\t"
"subu $t5, $t5, $t4 \n\t" //m_num = value - chroma
//if(hue < 256)
"li $t7, 256 \n\t" //load hue
"sub $t7, $t3, $t7 \n\t" //store hue - 256
"bgez $t7, comp_hue1 \n\t" //if hue>256 branch
//hue < 256
"multu $t3, $t4 \n\t" //set green
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"add $t1, $t7, $t5 \n\t"
"addu $t7, $t4, $t5 \n\t" //set red
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 8 \n\t"
"andi $t7, $t7, 0xFF00 \n\t"
"or $t1, $t1, $t7 \n\t"
"andi $t7, $t5, 0xFF \n\t"
"sll $t7, $t5, 16 \n\t" //set blue
"or $t1, $t7, $t1 \n\t"
"j comp_hue_end \n\t" //jump to end
"comp_hue1: \n\t"
//if(hue < 512)
"li $t7, 512 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, comp_hue2 \n\t"
//hue < 512
"addu $t7, $t4, $t5 \n\t" //set green
"addi $t7, $t7, -1 \n\t"
"andi $t1, $t7, 0xFF \n\t"
"li $t7, 511 \n\t" //set red
"subu $t7, $t7, $t3 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"and $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t7, $t1 \n\t"
"sll $t7, $t5, 16 \n\t" //set blue
"or $t1, $t7, $t1 \n\t"
"j comp_hue_end \n\t" //jump to end
"comp_hue2: \n\t"
//if(hue < 768)
"li $t7, 768 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, comp_hue3 \n\t"
//hue < 768
"addu $t7, $t4, $t5 \n\t" //set green
"addi $t7, $t7, -1 \n\t"
"andi $t1, $t7, 0xFF \n\t"
"sll $t7, $t5, 8 \n\t" //set red
"or $t1, $t7, $t1 \n\t"
"li $t7, 512 \n\t" //set blue
"subu $t7, $t3, $t7 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"andi $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
"j comp_hue_end \n\t" //jump to end
"comp_hue3: \n\t"
//if(hue < 1024)
"li $t7, 1024 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, comp_hue4 \n\t"
//hue < 1024
"li $t7, 1023 \n\t" //set green
"subu $t7, $t7, $t3 \n\t"
"multu $t4, $t7 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t1, $t7, $t5 \n\t"
//"and $t7, $t7, 0xFF \n\t"
"sll $t7, $t5, 8 \n\t" //set red
"or $t1, $t7, $t1 \n\t"
"addu $t7, $t4, $t5 \n\t" //set blue
"addi $t7, $t7, -1 \n\t"
"andi $t7, $t7, 0xFF \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
"j comp_hue_end \n\t" //jump to end
"comp_hue4: \n\t"
//if(hue < 1280)
"li $t7, 1280 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, comp_hue5 \n\t"
//hue < 1280
"andi $t1, $t5, 0xFF \n\t" //set green
"li $t7, 1024 \n\t" //set red
"subu $t7, $t3, $t7 \n\t"
"multu $t7, $t4 \n\t"
"mflo $t7 \n\t"
"srl $t7, $t7, 8 \n\t"
"addu $t7, $t7, $t5 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
"addu $t7, $t4, $t5 \n\t" //set blue
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 16 \n\t"
"or $t1, $t7, $t1 \n\t"
"j comp_hue_end \n\t" //jump to end
"comp_hue5: \n\t"
//if(hue < 1536)
"li $t7, 1536 \n\t"
"sub $t7, $t3, $t7 \n\t"
"bgez $t7, comp_hue6 \n\t"
//hue < 1536
"andi $t1, $t5, 0xFF \n\t" //set green
"addu $t7, $t4, $t5 \n\t" //set red
"addi $t7, $t7, -1 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
"li $t7, 1535 \n\t" //set blue
"subu $t7, $t7, $t3 \n\t"
"multu $t7, $t4 \n\t"
"mflo $t7 \n\t"
"andi $t7, $t7, 0xFF00 \n\t"
"sll $t7, $t7, 8 \n\t"
"or $t1, $t1, $t7 \n\t"
"comp_hue6: \n\t"
"comp_hue_end: \n\t"
"srl $t7, $t2, 24 \n\t" //get value
"andi $t7, $t7, 0xFF \n\t"
"bne $t7, $zero, zero_val_check \n\t"
"move $t1, $zero \n\t"
"zero_val_check: \n\t"
"j colorComputed \n\t"
//end of assembly
"HSVassemblyEnd: \n\t"
: //output
: "r"(pinMask), "r"(numberOfLEDs), "m"(portClr), "m"(portSet), "m"(colorArray) //input
: "%s0" //clobber-list
);
//bitstream done, enable interrupts