Mini projet LOO

Dependencies:   mbed APDS_9960 mbed-rtos

Dependents:   MoveYourTetris_OK

Home du projet

Committer:
Willheisen
Date:
Sun Apr 24 15:47:15 2016 +0000
Revision:
36:f58a566595ad
Parent:
35:7aef0bbdf335
Child:
38:418278e5eb6b
Child:
39:1aead50ff166
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Willheisen 9:6f3d8b714a59 1 /*
Willheisen 9:6f3d8b714a59 2 * cSPI.cpp
Willheisen 9:6f3d8b714a59 3 *
Willheisen 9:6f3d8b714a59 4 * Created on: 5 avr. 2016
Willheisen 9:6f3d8b714a59 5 * Author: clement
Willheisen 9:6f3d8b714a59 6 */
Willheisen 9:6f3d8b714a59 7
Willheisen 9:6f3d8b714a59 8 #include "cSPI.h"
Willheisen 9:6f3d8b714a59 9 #include "cMatrice.h"
Willheisen 9:6f3d8b714a59 10
clementdoreau 35:7aef0bbdf335 11 DigitalOut _cs(p14);
clementdoreau 35:7aef0bbdf335 12 SPI _spi(p11,p12, p13);
Willheisen 9:6f3d8b714a59 13
Willheisen 9:6f3d8b714a59 14
Willheisen 36:f58a566595ad 15 // CONSTRUCTEURS
Willheisen 36:f58a566595ad 16
Willheisen 36:f58a566595ad 17 cSPI::cSPI()
Willheisen 36:f58a566595ad 18 {
Willheisen 36:f58a566595ad 19 _freq = 0;
Willheisen 36:f58a566595ad 20 _mode = 0;
Willheisen 36:f58a566595ad 21 _bits = 0;
Willheisen 36:f58a566595ad 22 }
Willheisen 36:f58a566595ad 23
Willheisen 36:f58a566595ad 24 // DESTRUCTEUR
Willheisen 9:6f3d8b714a59 25
Willheisen 36:f58a566595ad 26 cSPI::~cSPI()
Willheisen 36:f58a566595ad 27 {
Willheisen 9:6f3d8b714a59 28
Willheisen 36:f58a566595ad 29 }
Willheisen 36:f58a566595ad 30 // GETTERS
Willheisen 9:6f3d8b714a59 31
Willheisen 36:f58a566595ad 32 unsigned int cSPI::getFrequence()
Willheisen 36:f58a566595ad 33 {
Willheisen 36:f58a566595ad 34 return _freq;
Willheisen 36:f58a566595ad 35 }
Willheisen 9:6f3d8b714a59 36
Willheisen 36:f58a566595ad 37 unsigned int cSPI::getMode()
Willheisen 36:f58a566595ad 38 {
Willheisen 36:f58a566595ad 39 return _mode;
Willheisen 36:f58a566595ad 40 }
Willheisen 9:6f3d8b714a59 41
Willheisen 36:f58a566595ad 42 unsigned char cSPI::getBits()
Willheisen 36:f58a566595ad 43 {
Willheisen 36:f58a566595ad 44 return _bits;
Willheisen 36:f58a566595ad 45 }
Willheisen 36:f58a566595ad 46
Willheisen 36:f58a566595ad 47 // SETTERS
Willheisen 9:6f3d8b714a59 48
Willheisen 36:f58a566595ad 49 void cSPI::setFrequence(unsigned int freq)
Willheisen 36:f58a566595ad 50 {
Willheisen 36:f58a566595ad 51 _freq = freq;
Willheisen 36:f58a566595ad 52 }
Willheisen 9:6f3d8b714a59 53
Willheisen 36:f58a566595ad 54 void cSPI::setMode(unsigned int mode)
Willheisen 36:f58a566595ad 55 {
Willheisen 36:f58a566595ad 56 _mode = mode;
Willheisen 36:f58a566595ad 57 }
Willheisen 9:6f3d8b714a59 58
Willheisen 36:f58a566595ad 59 void cSPI::setBits(unsigned char bits)
Willheisen 36:f58a566595ad 60 {
Willheisen 36:f58a566595ad 61 _bits = bits;
Willheisen 36:f58a566595ad 62 }
Willheisen 36:f58a566595ad 63
Willheisen 36:f58a566595ad 64 // METHODES
Willheisen 9:6f3d8b714a59 65
Willheisen 36:f58a566595ad 66 void cSPI::initSPI(unsigned int frequence)
Willheisen 36:f58a566595ad 67 {
Willheisen 36:f58a566595ad 68 _cs = 0;
Willheisen 36:f58a566595ad 69 wait_ms(1);
Willheisen 36:f58a566595ad 70 _spi.frequency(frequence);
Willheisen 36:f58a566595ad 71 wait_ms(1);
Willheisen 36:f58a566595ad 72 _cs = 1;
Willheisen 36:f58a566595ad 73 }
Willheisen 9:6f3d8b714a59 74
Willheisen 36:f58a566595ad 75 int cSPI::envoyerMatrice(cMatrice & mat) // Renvoi la reponse SPI
Willheisen 36:f58a566595ad 76 {
Willheisen 36:f58a566595ad 77 Serial pc(USBTX, USBRX);
Willheisen 36:f58a566595ad 78 _cs = 0;
Willheisen 36:f58a566595ad 79 wait_ms(1);
Willheisen 36:f58a566595ad 80 for(unsigned char i = 0; i < mat.getCol(); i++) {
Willheisen 36:f58a566595ad 81 for(unsigned char j = 0; j < mat.getLig()/2; j ++) {
Willheisen 36:f58a566595ad 82 _spi.write(mat.getValTab(i, j));
Willheisen 36:f58a566595ad 83 /* if(mat.getValTab(i, j)) {
Willheisen 36:f58a566595ad 84 pc.printf("1|");
Willheisen 36:f58a566595ad 85 } else {
Willheisen 36:f58a566595ad 86 pc.printf("0|");
Willheisen 36:f58a566595ad 87 }*/
Willheisen 9:6f3d8b714a59 88 }
Willheisen 36:f58a566595ad 89 //pc.printf("\n");
Willheisen 36:f58a566595ad 90 }
Willheisen 36:f58a566595ad 91 //pc.printf("\n");
Willheisen 36:f58a566595ad 92 for(unsigned char i = 0; i < mat.getCol(); i++) {
Willheisen 36:f58a566595ad 93 for(unsigned char j = mat.getLig()/2; j < mat.getLig(); j ++) {
Willheisen 36:f58a566595ad 94 _spi.write(mat.getValTab(i, j));
Willheisen 36:f58a566595ad 95 /* if(mat.getValTab(i, j)) {
Willheisen 36:f58a566595ad 96 pc.printf("1|");
Willheisen 36:f58a566595ad 97 } else {
Willheisen 36:f58a566595ad 98 pc.printf("0|");
Willheisen 36:f58a566595ad 99 }*/
Willheisen 36:f58a566595ad 100 }
Willheisen 36:f58a566595ad 101 //pc.printf("\n");
Willheisen 9:6f3d8b714a59 102 }
Willheisen 36:f58a566595ad 103 wait_ms(1);
Willheisen 36:f58a566595ad 104 _cs = 1; //avant 0
Willheisen 36:f58a566595ad 105 return 1; // temporaire
Willheisen 36:f58a566595ad 106
Willheisen 36:f58a566595ad 107 }
Willheisen 36:f58a566595ad 108
Willheisen 36:f58a566595ad 109 void cSPI::setNbMatrice(unsigned int nb)
Willheisen 36:f58a566595ad 110 {
Willheisen 36:f58a566595ad 111 _cs = 0;
Willheisen 36:f58a566595ad 112 wait_ms(1);
Willheisen 36:f58a566595ad 113 _spi.write('%');
Willheisen 36:f58a566595ad 114 _spi.write(nb);
Willheisen 36:f58a566595ad 115 wait_ms(1);
Willheisen 36:f58a566595ad 116 _cs = 1;
Willheisen 36:f58a566595ad 117 }