Blue LED matrix (8x16) program. Gets text string through bluetooth and displays it on led matrix. Also has a clock function- get system time from a phone through bluetooth and enters clock mode. In clock mode it acts as a clock showing hours and minutes and blinking led every second. Clock mode can be broken if a text string is received through bluetooth.

Dependencies:   mbed

main.cpp

Committer:
DaniusKalv
Date:
2014-11-04
Revision:
13:96590015edd8
Parent:
12:a8364a98c38c
Child:
14:adbb11e53c70

File content as of revision 13:96590015edd8:

#include "mbed.h"
#include "matrix.h"
#include "text.h"
#include "string.h"
#include "rtos.h"

text generator;
matrix display(p13, p12, p11, p14, p15, p17, p16);
DigitalOut led(LED1);
Serial pc(USBTX, USBRX);
Serial bluetooth(p28,p27);
static char line_buffer[99];
static bool mode_buffer = false;
static bool buffer_flag = false;
void bluetoothThread(void const *args);
Mutex buffer_mutex;

int main()
{
    bool matrix_mode = false;
    char line[99];
    char buffer[4];
    bool dot;
    pc.baud(115200);
    generator.generate("ABCD");
    Thread thread(bluetoothThread);

    while(true) {

        if(buffer_flag) {
            buffer_mutex.lock();
            matrix_mode = mode_buffer;
            memcpy(line, line_buffer, sizeof(line_buffer));
            led = 0;
            buffer_flag = false;
            buffer_mutex.unlock();
            
            if (matrix_mode == 0) generator.generate(line);    
        }
        
        pc.printf("\r\nMode = %i", matrix_mode);
        
        if (matrix_mode == 1){
            time_t seconds = time(NULL);
            strftime(buffer, 4, "%H%M", localtime(&seconds));
            if ((seconds % 2) == 0) dot = true;
            else dot = false;
            display.clock(buffer, dot);
        }
        else display.show();
    }
}

void bluetoothThread(void const *args)
{
    bluetooth.baud(38400);

    while(true) {
        
        if (bluetooth.readable()) {
            int i, j = 0;
            i = 10 * (bluetooth.getc() - 48);
            i += bluetooth.getc() - 48;
            
            buffer_mutex.lock();
            memset(line_buffer, 0, sizeof(line_buffer));
            if(i > 0) {
                mode_buffer = false;
                do {
                    line_buffer[j] = bluetooth.getc();
                    j++;
                    Thread::wait(0.4);
                } 
                while(bluetooth.readable() && (j < i) && (j < 99));
            } 
            else {
                mode_buffer = true;
                bluetooth.gets(line_buffer, 10);
                set_time(atoi(line_buffer));
            }
            led = 1;
            buffer_flag = true;
            buffer_mutex.unlock();
        }
        Thread::wait(10);
    }
}