Alyson Castiblanco
/
TETRISACT
ok
Fork of 00_01LAB_matrix8x8_fig_tetris1 by
main.cpp@5:819f043b99a5, 2018-09-08 (annotated)
- Committer:
- acastiblancoc
- Date:
- Sat Sep 08 03:46:46 2018 +0000
- Revision:
- 5:819f043b99a5
- Parent:
- 4:2bc5dfe058ef
- Child:
- 6:2f42f0b53611
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabeltranm | 0:61199d9d0aed | 1 | |
fabeltranm | 0:61199d9d0aed | 2 | #include "mbed.h" |
fabeltranm | 4:2bc5dfe058ef | 3 | #include "figuras.h" |
fabeltranm | 0:61199d9d0aed | 4 | |
fabeltranm | 0:61199d9d0aed | 5 | SPI deviceM(PB_15, PB_14, PB_13); |
fabeltranm | 0:61199d9d0aed | 6 | DigitalOut ssel (PB_12); |
fabeltranm | 4:2bc5dfe058ef | 7 | Serial command(USBTX, USBRX); |
fabeltranm | 0:61199d9d0aed | 8 | |
fabeltranm | 4:2bc5dfe058ef | 9 | #define DEBUG 1 |
acastiblancoc | 5:819f043b99a5 | 10 | #define VELOCITY 1000 // ms |
fabeltranm | 1:b732136485e0 | 11 | |
acastiblancoc | 5:819f043b99a5 | 12 | |
fabeltranm | 4:2bc5dfe058ef | 13 | |
fabeltranm | 4:2bc5dfe058ef | 14 | uint8_t matrix_act[8]={0,0,0,0,0,0,0,0}; |
acastiblancoc | 5:819f043b99a5 | 15 | uint8_t matrix_tmp[11]={0b00000000,0b00000000,0b00000000,0,0,0,0,0,0,0}; |
fabeltranm | 4:2bc5dfe058ef | 16 | |
fabeltranm | 0:61199d9d0aed | 17 | |
fabeltranm | 0:61199d9d0aed | 18 | void sendSPI(uint8_t d1, uint8_t d2) |
fabeltranm | 0:61199d9d0aed | 19 | { |
fabeltranm | 0:61199d9d0aed | 20 | deviceM.unlock(); |
fabeltranm | 0:61199d9d0aed | 21 | ssel=0; |
fabeltranm | 0:61199d9d0aed | 22 | deviceM.write(d1); |
fabeltranm | 0:61199d9d0aed | 23 | deviceM.write(d2); |
fabeltranm | 0:61199d9d0aed | 24 | ssel=1; |
fabeltranm | 0:61199d9d0aed | 25 | deviceM.lock(); |
fabeltranm | 0:61199d9d0aed | 26 | }; |
fabeltranm | 0:61199d9d0aed | 27 | |
fabeltranm | 0:61199d9d0aed | 28 | |
acastiblancoc | 5:819f043b99a5 | 29 | |
acastiblancoc | 5:819f043b99a5 | 30 | |
acastiblancoc | 5:819f043b99a5 | 31 | void debug_m(char *s , ... ){ |
acastiblancoc | 5:819f043b99a5 | 32 | #if DEBUG |
acastiblancoc | 5:819f043b99a5 | 33 | command.printf(s); |
acastiblancoc | 5:819f043b99a5 | 34 | #endif |
acastiblancoc | 5:819f043b99a5 | 35 | } |
acastiblancoc | 5:819f043b99a5 | 36 | |
fabeltranm | 0:61199d9d0aed | 37 | void printMatrix(uint8_t* vC) |
fabeltranm | 0:61199d9d0aed | 38 | |
fabeltranm | 0:61199d9d0aed | 39 | { |
fabeltranm | 0:61199d9d0aed | 40 | uint8_t i =0; |
fabeltranm | 0:61199d9d0aed | 41 | for (i=1;i<9;i++){ |
fabeltranm | 4:2bc5dfe058ef | 42 | sendSPI(i,*(vC+i-1)); |
fabeltranm | 0:61199d9d0aed | 43 | } |
fabeltranm | 0:61199d9d0aed | 44 | |
acastiblancoc | 5:819f043b99a5 | 45 | } |
fabeltranm | 2:414129d953dd | 46 | |
fabeltranm | 4:2bc5dfe058ef | 47 | |
acastiblancoc | 5:819f043b99a5 | 48 | void init_display(){ |
fabeltranm | 4:2bc5dfe058ef | 49 | sendSPI(0x0c,1); |
fabeltranm | 4:2bc5dfe058ef | 50 | sendSPI(0x0b,7); |
fabeltranm | 4:2bc5dfe058ef | 51 | sendSPI(0x09,0); |
acastiblancoc | 5:819f043b99a5 | 52 | sendSPI(0x0A,0x0E); |
fabeltranm | 4:2bc5dfe058ef | 53 | printMatrix(matrix_act); |
fabeltranm | 4:2bc5dfe058ef | 54 | int i; |
acastiblancoc | 5:819f043b99a5 | 55 | for (i=0;i<4;i++){ |
fabeltranm | 4:2bc5dfe058ef | 56 | sendSPI(0x0F,1); |
acastiblancoc | 5:819f043b99a5 | 57 | wait (0.2); |
fabeltranm | 4:2bc5dfe058ef | 58 | sendSPI(0x0f,0); |
acastiblancoc | 5:819f043b99a5 | 59 | wait (0.2); |
fabeltranm | 4:2bc5dfe058ef | 60 | } |
acastiblancoc | 5:819f043b99a5 | 61 | } |
acastiblancoc | 5:819f043b99a5 | 62 | |
acastiblancoc | 5:819f043b99a5 | 63 | |
acastiblancoc | 5:819f043b99a5 | 64 | |
fabeltranm | 4:2bc5dfe058ef | 65 | |
fabeltranm | 4:2bc5dfe058ef | 66 | void act_matrix(uint8_t* mascara){ |
fabeltranm | 4:2bc5dfe058ef | 67 | for (int i=0; i<8;i++) |
fabeltranm | 4:2bc5dfe058ef | 68 | matrix_act[i]= ( *(mascara+i)); |
fabeltranm | 4:2bc5dfe058ef | 69 | |
fabeltranm | 4:2bc5dfe058ef | 70 | } |
acastiblancoc | 5:819f043b99a5 | 71 | void copy_matrix(uint8_t* m2, char col){ |
fabeltranm | 4:2bc5dfe058ef | 72 | |
fabeltranm | 4:2bc5dfe058ef | 73 | for (int i=0; i<11;i++){ |
acastiblancoc | 5:819f043b99a5 | 74 | matrix_tmp[i]= (*(m2+i)>>col); |
fabeltranm | 4:2bc5dfe058ef | 75 | } |
fabeltranm | 4:2bc5dfe058ef | 76 | } |
fabeltranm | 4:2bc5dfe058ef | 77 | |
acastiblancoc | 5:819f043b99a5 | 78 | |
fabeltranm | 4:2bc5dfe058ef | 79 | void new_read(){ |
acastiblancoc | 5:819f043b99a5 | 80 | |
acastiblancoc | 5:819f043b99a5 | 81 | char ini=command.getc(); |
fabeltranm | 4:2bc5dfe058ef | 82 | char tf=command.getc(); |
acastiblancoc | 5:819f043b99a5 | 83 | char ang=command.getc(); |
acastiblancoc | 5:819f043b99a5 | 84 | char col=command.getc(); |
acastiblancoc | 5:819f043b99a5 | 85 | char fin=command.getc(); |
acastiblancoc | 5:819f043b99a5 | 86 | if(ini==0x3C && fin==0x3E) |
acastiblancoc | 5:819f043b99a5 | 87 | { |
fabeltranm | 4:2bc5dfe058ef | 88 | switch (tf) { |
fabeltranm | 4:2bc5dfe058ef | 89 | case L_TYPE: |
acastiblancoc | 5:819f043b99a5 | 90 | switch (ang) |
acastiblancoc | 5:819f043b99a5 | 91 | { case NOVEN: |
acastiblancoc | 5:819f043b99a5 | 92 | copy_matrix(FIG_L1); |
acastiblancoc | 5:819f043b99a5 | 93 | break; |
acastiblancoc | 5:819f043b99a5 | 94 | case ZERO: |
acastiblancoc | 5:819f043b99a5 | 95 | copy_matrix(FIG_L); |
acastiblancoc | 5:819f043b99a5 | 96 | break ; |
acastiblancoc | 5:819f043b99a5 | 97 | case PII: |
acastiblancoc | 5:819f043b99a5 | 98 | copy_matrix(FIG_L2); |
acastiblancoc | 5:819f043b99a5 | 99 | break; |
acastiblancoc | 5:819f043b99a5 | 100 | case DOSSE: |
acastiblancoc | 5:819f043b99a5 | 101 | copy_matrix(FIG_L3); |
acastiblancoc | 5:819f043b99a5 | 102 | break; |
acastiblancoc | 5:819f043b99a5 | 103 | } |
fabeltranm | 4:2bc5dfe058ef | 104 | break ; |
fabeltranm | 4:2bc5dfe058ef | 105 | case S_TYPE: |
acastiblancoc | 5:819f043b99a5 | 106 | switch (ang) |
acastiblancoc | 5:819f043b99a5 | 107 | { case NOVEN: |
acastiblancoc | 5:819f043b99a5 | 108 | copy_matrix(FIG_S1); |
acastiblancoc | 5:819f043b99a5 | 109 | break; |
acastiblancoc | 5:819f043b99a5 | 110 | case ZERO: |
acastiblancoc | 5:819f043b99a5 | 111 | copy_matrix(FIG_S); |
acastiblancoc | 5:819f043b99a5 | 112 | break ; |
acastiblancoc | 5:819f043b99a5 | 113 | case PII: |
acastiblancoc | 5:819f043b99a5 | 114 | copy_matrix(FIG_S2); |
acastiblancoc | 5:819f043b99a5 | 115 | break; |
acastiblancoc | 5:819f043b99a5 | 116 | case DOSSE: |
acastiblancoc | 5:819f043b99a5 | 117 | copy_matrix(FIG_S3); |
acastiblancoc | 5:819f043b99a5 | 118 | break; |
acastiblancoc | 5:819f043b99a5 | 119 | } |
fabeltranm | 4:2bc5dfe058ef | 120 | break ; |
fabeltranm | 4:2bc5dfe058ef | 121 | case I_TYPE: |
acastiblancoc | 5:819f043b99a5 | 122 | switch (ang) |
acastiblancoc | 5:819f043b99a5 | 123 | { case NOVEN: |
acastiblancoc | 5:819f043b99a5 | 124 | copy_matrix(FIG_I1); |
acastiblancoc | 5:819f043b99a5 | 125 | break; |
acastiblancoc | 5:819f043b99a5 | 126 | case ZERO: |
acastiblancoc | 5:819f043b99a5 | 127 | copy_matrix(FIG_I); |
acastiblancoc | 5:819f043b99a5 | 128 | break ; |
acastiblancoc | 5:819f043b99a5 | 129 | case PII: |
acastiblancoc | 5:819f043b99a5 | 130 | copy_matrix(FIG_I2); |
acastiblancoc | 5:819f043b99a5 | 131 | break; |
acastiblancoc | 5:819f043b99a5 | 132 | case DOSSE: |
acastiblancoc | 5:819f043b99a5 | 133 | copy_matrix(FIG_I3); |
acastiblancoc | 5:819f043b99a5 | 134 | break; |
acastiblancoc | 5:819f043b99a5 | 135 | } |
fabeltranm | 4:2bc5dfe058ef | 136 | break ; |
acastiblancoc | 5:819f043b99a5 | 137 | case C_TYPE: |
acastiblancoc | 5:819f043b99a5 | 138 | copy_matrix(FIG_C); |
acastiblancoc | 5:819f043b99a5 | 139 | break ; |
acastiblancoc | 5:819f043b99a5 | 140 | case T_TYPE: |
acastiblancoc | 5:819f043b99a5 | 141 | switch (ang) |
acastiblancoc | 5:819f043b99a5 | 142 | { case NOVEN: |
acastiblancoc | 5:819f043b99a5 | 143 | copy_matrix(FIG_T1); |
acastiblancoc | 5:819f043b99a5 | 144 | break; |
acastiblancoc | 5:819f043b99a5 | 145 | case ZERO: |
acastiblancoc | 5:819f043b99a5 | 146 | copy_matrix(FIG_T); |
acastiblancoc | 5:819f043b99a5 | 147 | break ; |
acastiblancoc | 5:819f043b99a5 | 148 | case PII: |
acastiblancoc | 5:819f043b99a5 | 149 | copy_matrix(FIG_T2); |
acastiblancoc | 5:819f043b99a5 | 150 | break; |
acastiblancoc | 5:819f043b99a5 | 151 | case DOSSE: |
acastiblancoc | 5:819f043b99a5 | 152 | copy_matrix(FIG_T3); |
acastiblancoc | 5:819f043b99a5 | 153 | break; |
acastiblancoc | 5:819f043b99a5 | 154 | } |
acastiblancoc | 5:819f043b99a5 | 155 | break; |
fabeltranm | 4:2bc5dfe058ef | 156 | default: |
fabeltranm | 4:2bc5dfe058ef | 157 | copy_matrix(FIG_NULL); |
acastiblancoc | 5:819f043b99a5 | 158 | break; |
acastiblancoc | 5:819f043b99a5 | 159 | } |
acastiblancoc | 5:819f043b99a5 | 160 | } |
acastiblancoc | 5:819f043b99a5 | 161 | else |
acastiblancoc | 5:819f043b99a5 | 162 | {} |
fabeltranm | 4:2bc5dfe058ef | 163 | } |
acastiblancoc | 5:819f043b99a5 | 164 | //desplazamiento horizontal |
fabeltranm | 4:2bc5dfe058ef | 165 | void desplazar_fila(){ |
acastiblancoc | 5:819f043b99a5 | 166 | for (int i=0; i<8;i++) |
fabeltranm | 4:2bc5dfe058ef | 167 | matrix_tmp[i]= matrix_tmp[i]>>1; |
fabeltranm | 3:4a8de2f71ed0 | 168 | |
acastiblancoc | 5:819f043b99a5 | 169 | } |
acastiblancoc | 5:819f043b99a5 | 170 | |
acastiblancoc | 5:819f043b99a5 | 171 | |
fabeltranm | 4:2bc5dfe058ef | 172 | int main() { |
fabeltranm | 4:2bc5dfe058ef | 173 | init_display(); |
fabeltranm | 4:2bc5dfe058ef | 174 | while(1){ |
fabeltranm | 4:2bc5dfe058ef | 175 | new_read(); |
acastiblancoc | 5:819f043b99a5 | 176 | for(int i=0;i<8;i++){ |
fabeltranm | 4:2bc5dfe058ef | 177 | desplazar_fila(); |
fabeltranm | 4:2bc5dfe058ef | 178 | act_matrix(matrix_tmp); |
fabeltranm | 4:2bc5dfe058ef | 179 | printMatrix(matrix_act); |
fabeltranm | 4:2bc5dfe058ef | 180 | wait_ms(VELOCITY); |
acastiblancoc | 5:819f043b99a5 | 181 | } |
acastiblancoc | 5:819f043b99a5 | 182 | } |
fabeltranm | 4:2bc5dfe058ef | 183 | } |