Mini projet LOO

Dependencies:   mbed APDS_9960 mbed-rtos

Dependents:   MoveYourTetris_OK

Home du projet

src/cSPI.cpp

Committer:
clementdoreau
Date:
2016-04-21
Revision:
31:7313366789f2
Parent:
30:c647da947bd9
Child:
32:029962133529

File content as of revision 31:7313366789f2:

/*
 * 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, mode);
    wait_us(500);
    _cs = 1;
    pc.printf("Liaison SPI Initialisee\n");
    
}

int cSPI::envoyerMatrice(cMatrice & mat)  // Renvoi la reponse SPI
{
       
    _cs = 0;
    wait_us(500);
    for(int i = 0; i < mat.getLig()/2; i++) {
        for(int j = 0; j < mat.getCol(); j ++) {
            _spi.write(mat.getValTab(j, i));
        }
    }
    for(int i = mat.getLig()/2; i < mat.getLig(); i++) {
        for(int j = 0; j < mat.getCol(); j ++) {
            _spi.write(mat.getValTab(j, i));
        }
    }
    wait_us(500);
    _cs = 1; //avant 0
    return 1; // temporaire
}

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