Sille Van Landschoot / Mbed 2 deprecated mbed_slave_full

Dependencies:   mbed

main.cpp

Committer:
dwini
Date:
2014-10-13
Revision:
3:01b322df3731
Parent:
2:49bb6ee1191c
Child:
4:f537311ddc53

File content as of revision 3:01b322df3731:

#include "mbed.h"

DigitalOut aLED(LED1);
Serial pc(USBTX, USBRX); // tx, rx
I2CSlave slave(p28, p27);

const int SLAVE_ADDRESS = 0x84;
const int MEMORY_SIZE = 16;
const int I2C_FREQUENCY = 100000;
const int I2C_BUFFER_SIZE = 10;

enum COMMAND { PUSH, PULL, CLEAR, PRINT };

int memory[MEMORY_SIZE];

/*
 * Set all memory locations to 0x55
 */
void initializeMemory(void)
{
    for (int i = 0; i < MEMORY_SIZE; i++) {
        memory[i] = -1;
    }
}

/*
 * Print current memory content
 */
void printMemory(void)
{
    for (int i = 0; i < MEMORY_SIZE; i++) {
        pc.printf("%d@0x%x\r\n", memory[i], i);
    }
}

int main() {
    pc.baud(115200);
    pc.printf("Size of integer is %d bytes\r\n", sizeof(int));
    pc.printf("Size of memory buffer is %d elements\r\n", MEMORY_SIZE);
    
    // Alive LED
    int cAlive = 0;
    
    // Configure I2C
    slave.frequency(I2C_FREQUENCY);
    pc.printf("Slave is working @ %dHz\r\n", I2C_FREQUENCY);
    slave.address(SLAVE_ADDRESS);
    pc.printf("Slave is working @ SLAVE_ADDRESS = 0x%x\r\n", SLAVE_ADDRESS);

    // Make sure memory buffer is zeroed
    initializeMemory();
    printMemory();

    // I2C buffer
    char buffer[I2C_BUFFER_SIZE];
    
    while (1) {
        int i = slave.receive();
        switch (i) {
            /*case I2CSlave::ReadAddressed:
                pc.printf("Slave received command: %d [ReadAddressed]\r\n", i);

                if (slave.write(msg, strlen(msg) + 1) == 0) {
                    printf("Sending %s to master\r\n", msg);
                }
                break;*/
    
            case I2CSlave::WriteAddressed:
                
                if (!slave.read(buffer, I2C_BUFFER_SIZE)) {
                    // Check command byte
                    switch (buffer[0]) {
                        case PUSH:
                            pc.printf("Storing %d@%d\r\n", *((int*)(&(buffer[2]))), buffer[1]);
                            if (buffer[1] < MEMORY_SIZE) {
                                memory[buffer[1]] = *((int*)(&(buffer[2])));
                            } else {
                                pc.printf("Address out of boundary\r\n");    
                            }
                            break;
                        
                        case PRINT:
                            printMemory();
                            break;
                        
                        default:
                            pc.printf("Unknown command byte\r\n");
                    }
                } else {
                    pc.printf("Received WriteAddressed] without data\r\n");
                }
                break;
        }
        
        // Clear buffer
        for (int i = 0; i < I2C_BUFFER_SIZE; i++) {
            buffer[i] = 0;   
        }
        
        // Alive LED
        cAlive = (cAlive + 1) % 100000;
        if (!cAlive) {
            aLED = !aLED;
        }
    }
}