ferney alberto beltran molina
/
00_01LAB_matrix8x8_fig_tetris
EJEMPLO
Fork of 00_LAB_matrix8x8_SPI by
main.cpp@5:d1c3238ea285, 2018-09-14 (annotated)
- Committer:
- fabeltranm
- Date:
- Fri Sep 14 17:59:35 2018 +0000
- Revision:
- 5:d1c3238ea285
- Parent:
- 4:2bc5dfe058ef
ok1
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 |
fabeltranm | 4:2bc5dfe058ef | 10 | #define VELOCITY 500 // ms |
fabeltranm | 1:b732136485e0 | 11 | |
fabeltranm | 4:2bc5dfe058ef | 12 | void debug_m(char *s , ... ); |
fabeltranm | 4:2bc5dfe058ef | 13 | |
fabeltranm | 4:2bc5dfe058ef | 14 | uint8_t matrix_act[8]={0,0,0,0,0,0,0,0}; |
fabeltranm | 5:d1c3238ea285 | 15 | uint8_t matrix_tmp[11]={0,0,0,0,0,0,0,0,0,0}; |
fabeltranm | 5:d1c3238ea285 | 16 | uint8_t matrix_fig[3]={0,0,0}; |
fabeltranm | 4:2bc5dfe058ef | 17 | |
fabeltranm | 0:61199d9d0aed | 18 | |
fabeltranm | 0:61199d9d0aed | 19 | void sendSPI(uint8_t d1, uint8_t d2) |
fabeltranm | 0:61199d9d0aed | 20 | { |
fabeltranm | 0:61199d9d0aed | 21 | deviceM.unlock(); |
fabeltranm | 0:61199d9d0aed | 22 | ssel=0; |
fabeltranm | 0:61199d9d0aed | 23 | deviceM.write(d1); |
fabeltranm | 0:61199d9d0aed | 24 | deviceM.write(d2); |
fabeltranm | 0:61199d9d0aed | 25 | ssel=1; |
fabeltranm | 0:61199d9d0aed | 26 | deviceM.lock(); |
fabeltranm | 0:61199d9d0aed | 27 | }; |
fabeltranm | 0:61199d9d0aed | 28 | |
fabeltranm | 0:61199d9d0aed | 29 | |
fabeltranm | 0:61199d9d0aed | 30 | void printMatrix(uint8_t* vC) |
fabeltranm | 0:61199d9d0aed | 31 | |
fabeltranm | 0:61199d9d0aed | 32 | { |
fabeltranm | 0:61199d9d0aed | 33 | uint8_t i =0; |
fabeltranm | 0:61199d9d0aed | 34 | for (i=1;i<9;i++){ |
fabeltranm | 4:2bc5dfe058ef | 35 | sendSPI(i,*(vC+i-1)); |
fabeltranm | 0:61199d9d0aed | 36 | } |
fabeltranm | 0:61199d9d0aed | 37 | |
fabeltranm | 0:61199d9d0aed | 38 | }; |
fabeltranm | 2:414129d953dd | 39 | |
fabeltranm | 4:2bc5dfe058ef | 40 | |
fabeltranm | 4:2bc5dfe058ef | 41 | |
fabeltranm | 4:2bc5dfe058ef | 42 | int init_display(){ |
fabeltranm | 4:2bc5dfe058ef | 43 | sendSPI(0x0c,1); |
fabeltranm | 4:2bc5dfe058ef | 44 | sendSPI(0x0b,7); |
fabeltranm | 4:2bc5dfe058ef | 45 | sendSPI(0x09,0); |
fabeltranm | 4:2bc5dfe058ef | 46 | sendSPI(0x0A,0x0f); |
fabeltranm | 4:2bc5dfe058ef | 47 | printMatrix(matrix_act); |
fabeltranm | 4:2bc5dfe058ef | 48 | int i; |
fabeltranm | 4:2bc5dfe058ef | 49 | for (i=0;i<2;i++){ |
fabeltranm | 4:2bc5dfe058ef | 50 | sendSPI(0x0F,1); |
fabeltranm | 4:2bc5dfe058ef | 51 | wait (0.5); |
fabeltranm | 4:2bc5dfe058ef | 52 | sendSPI(0x0f,0); |
fabeltranm | 4:2bc5dfe058ef | 53 | wait (0.5); |
fabeltranm | 4:2bc5dfe058ef | 54 | } |
fabeltranm | 4:2bc5dfe058ef | 55 | } |
fabeltranm | 4:2bc5dfe058ef | 56 | |
fabeltranm | 5:d1c3238ea285 | 57 | void actualizar_matrix(){ |
fabeltranm | 5:d1c3238ea285 | 58 | for (int i=fig_act.posx; i<fig_act.posx+3;i++) |
fabeltranm | 5:d1c3238ea285 | 59 | matrix_act[i]= fig_act.fig[i] ; |
fabeltranm | 4:2bc5dfe058ef | 60 | |
fabeltranm | 4:2bc5dfe058ef | 61 | } |
fabeltranm | 5:d1c3238ea285 | 62 | |
fabeltranm | 5:d1c3238ea285 | 63 | |
fabeltranm | 5:d1c3238ea285 | 64 | void copy_fig(uint8_t* m2, uint8_t ncol){ |
fabeltranm | 5:d1c3238ea285 | 65 | for (int i=0; i<3;i++){ |
fabeltranm | 5:d1c3238ea285 | 66 | fig_act.fig[i]= *(m2+i); |
fabeltranm | 5:d1c3238ea285 | 67 | debug_m("%b\n",*(m2+i)); |
fabeltranm | 4:2bc5dfe058ef | 68 | } |
fabeltranm | 5:d1c3238ea285 | 69 | fig_act.posx=ncol; |
fabeltranm | 5:d1c3238ea285 | 70 | |
fabeltranm | 5:d1c3238ea285 | 71 | } |
fabeltranm | 5:d1c3238ea285 | 72 | |
fabeltranm | 5:d1c3238ea285 | 73 | |
fabeltranm | 4:2bc5dfe058ef | 74 | |
fabeltranm | 4:2bc5dfe058ef | 75 | void new_read(){ |
fabeltranm | 5:d1c3238ea285 | 76 | uint8_t tf=command.getc(); |
fabeltranm | 5:d1c3238ea285 | 77 | char nc=command.getc(); |
fabeltranm | 5:d1c3238ea285 | 78 | debug_m("se recibe la figura %d, en la columna %d",tf); |
fabeltranm | 4:2bc5dfe058ef | 79 | switch (tf) { |
fabeltranm | 4:2bc5dfe058ef | 80 | case L_TYPE: |
fabeltranm | 5:d1c3238ea285 | 81 | copy_fig(FIG_L,nc); |
fabeltranm | 4:2bc5dfe058ef | 82 | break ; |
fabeltranm | 4:2bc5dfe058ef | 83 | case S_TYPE: |
fabeltranm | 5:d1c3238ea285 | 84 | copy_fig(FIG_S,nc); |
fabeltranm | 4:2bc5dfe058ef | 85 | break ; |
fabeltranm | 4:2bc5dfe058ef | 86 | case I_TYPE: |
fabeltranm | 5:d1c3238ea285 | 87 | copy_fig(FIG_I,nc); |
fabeltranm | 4:2bc5dfe058ef | 88 | break ; |
fabeltranm | 4:2bc5dfe058ef | 89 | default: |
fabeltranm | 5:d1c3238ea285 | 90 | copy_fig(FIG_NULL,nc); |
fabeltranm | 4:2bc5dfe058ef | 91 | |
fabeltranm | 2:414129d953dd | 92 | } |
fabeltranm | 0:61199d9d0aed | 93 | |
fabeltranm | 2:414129d953dd | 94 | |
fabeltranm | 4:2bc5dfe058ef | 95 | } |
fabeltranm | 5:d1c3238ea285 | 96 | |
fabeltranm | 4:2bc5dfe058ef | 97 | void desplazar_fila(){ |
fabeltranm | 5:d1c3238ea285 | 98 | for (int i=0; i<3;i++) |
fabeltranm | 5:d1c3238ea285 | 99 | fig_act.fig[i]= fig_act.fig[i]>>1; |
fabeltranm | 3:4a8de2f71ed0 | 100 | |
fabeltranm | 4:2bc5dfe058ef | 101 | } |
fabeltranm | 4:2bc5dfe058ef | 102 | int main() { |
fabeltranm | 4:2bc5dfe058ef | 103 | init_display(); |
fabeltranm | 4:2bc5dfe058ef | 104 | while(1){ |
fabeltranm | 4:2bc5dfe058ef | 105 | new_read(); |
fabeltranm | 4:2bc5dfe058ef | 106 | for(int i=0;i<8;i++){ |
fabeltranm | 4:2bc5dfe058ef | 107 | desplazar_fila(); |
fabeltranm | 5:d1c3238ea285 | 108 | actualizar_matrix(); |
fabeltranm | 4:2bc5dfe058ef | 109 | printMatrix(matrix_act); |
fabeltranm | 4:2bc5dfe058ef | 110 | wait_ms(VELOCITY); |
fabeltranm | 4:2bc5dfe058ef | 111 | } |
fabeltranm | 4:2bc5dfe058ef | 112 | } |
fabeltranm | 2:414129d953dd | 113 | } |
fabeltranm | 2:414129d953dd | 114 | |
fabeltranm | 0:61199d9d0aed | 115 | |
fabeltranm | 4:2bc5dfe058ef | 116 | void debug_m(char *s , ... ){ |
fabeltranm | 4:2bc5dfe058ef | 117 | #if DEBUG |
fabeltranm | 4:2bc5dfe058ef | 118 | command.printf(s); |
fabeltranm | 4:2bc5dfe058ef | 119 | #endif |
fabeltranm | 4:2bc5dfe058ef | 120 | } |