Mini projet LOO

Dependencies:   mbed APDS_9960 mbed-rtos

Dependents:   MoveYourTetris_OK

Home du projet

src/cSPI.cpp

Committer:
Willheisen
Date:
2016-04-22
Revision:
33:a6f0be429ce0
Parent:
32:029962133529

File content as of revision 33:a6f0be429ce0:

/*
 * cSPI.cpp
 *
 *  Created on: 5 avr. 2016
 *      Author: clement
 */

#include "cSPI.h"
#include "cMatrice.h"

DigitalOut _cs(p14);
SPI _spi(p11,p12, p13);

// CONSTRUCTEURS

cSPI::cSPI()
{

}

cSPI::cSPI(unsigned int f, unsigned int m, unsigned int b)
{
    _freq = f;
    _mode = m;
    _bits = b;

    _cs = 0;
    wait(0.5);
    _spi.frequency(_freq);
    _spi.format(_bits, _mode);
    wait(0.5);
    _cs = 1;
}

// DESTRUCTEUR

cSPI::~cSPI()
{

}
// GETTERS

unsigned int cSPI::getFrequence()
{
    return _freq;
}

unsigned int cSPI::getMode()
{
    return _mode;
}

unsigned int cSPI::getBits()
{
    return _bits;
}

// SETTERS

void cSPI::setFrequence(unsigned int freq)
{
    _freq = freq;
}

void cSPI::setMode(unsigned int mode)
{
    _mode = mode;
}

void cSPI::setBits(unsigned int bits)
{
    _bits = bits;
}

// METHODES

void cSPI::initSPI(unsigned int frequence, unsigned int bits, unsigned int mode)
{
    Serial pc(USBTX, USBRX);
    _freq = frequence;
    _cs = 0;
    wait_us(500);
    _spi.frequency(_freq);
    //_spi.format(bits);
    wait_us(500);
    _cs = 1;
    pc.printf("Liaison SPI Initialisee\n");

}

int cSPI::envoyerMatrice(cMatrice & mat)  // Renvoi la reponse SPI
{
    Serial pc2(USBTX, USBRX);


    for(int i = 0; i < mat.getLig(); i++) {
        for(int j = 0; j < mat.getCol(); j ++) {
            if(mat.getValTab(j, i) != 0) {
                pc2.printf("%d|", 1);
            } else {
                pc2.printf("%d|", 0);
            }
        }
        pc2.printf("\n");
    }
    _cs = 0;
    wait_us(500);
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 8; j ++) {
            _spi.write(mat.getValTab(j, i));
        }
    }
    for(int i = 0; i < 8; i++) {
        for(int j = 8; j < 16; j ++) {
            _spi.write(mat.getValTab(j, i));
        }
    }
    wait_us(500);
    _cs = 1; //avant 0
    wait(0.1);
    /*_cs = 0;
    wait(0.01);
    for(int i =0; i<64; i++) {
        _spi.write(LED_NOIR);
    }
    wait(0.01);
    _cs=1;*/


    return 1; // temporaire
}

void cSPI::configurerNbMatrices(const int nb)
{
    _cs = 0;
    wait_us(500);
    _spi.write('%');
    _spi.write(nb);
    wait_us(500);
    _cs = 1;
}