Allow user to connect multiple screen.

Dependencies:   mbed-rtos mbed

Committer:
Ratchapong
Date:
Wed Mar 11 05:00:37 2015 +0000
Revision:
0:052d0f82433e
Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ratchapong 0:052d0f82433e 1 #include "ScreenUpdater.h"
Ratchapong 0:052d0f82433e 2 #include "uLCD_4DGL.h"
Ratchapong 0:052d0f82433e 3 #include "mbed.h"
Ratchapong 0:052d0f82433e 4 static void uLCD_thread_func(void const *args) {
Ratchapong 0:052d0f82433e 5 //printf("Thread alive.");
Ratchapong 0:052d0f82433e 6 uLCD_4DGL *lcd = ((uLCD_Thread_Args*)args)->uLCD;
Ratchapong 0:052d0f82433e 7 std::queue<Command*> *commands = ((uLCD_Thread_Args*)args)->commands;
Ratchapong 0:052d0f82433e 8 Mutex *commands_mutex = ((uLCD_Thread_Args*)args)->commands_mutex;
Ratchapong 0:052d0f82433e 9 while (true) {
Ratchapong 0:052d0f82433e 10 if (!commands->empty()) {
Ratchapong 0:052d0f82433e 11 commands_mutex->lock();
Ratchapong 0:052d0f82433e 12 Command *command = commands->front();
Ratchapong 0:052d0f82433e 13 commands->pop();
Ratchapong 0:052d0f82433e 14 command->execute(lcd);
Ratchapong 0:052d0f82433e 15 delete command;
Ratchapong 0:052d0f82433e 16 commands_mutex->unlock();
Ratchapong 0:052d0f82433e 17 } else {
Ratchapong 0:052d0f82433e 18 //printf("empty.");
Ratchapong 0:052d0f82433e 19 Thread::signal_wait(0x1);
Ratchapong 0:052d0f82433e 20 }
Ratchapong 0:052d0f82433e 21 }
Ratchapong 0:052d0f82433e 22 }
Ratchapong 0:052d0f82433e 23 ScreenUpdater::ScreenUpdater(uLCD_4DGL *uLCD) {
Ratchapong 0:052d0f82433e 24 //printf("ScreenUpdater booting.");
Ratchapong 0:052d0f82433e 25 uLCD_Thread_Args args = {uLCD, &commands, &commands_mutex};
Ratchapong 0:052d0f82433e 26 Thread *_thread = new Thread(uLCD_thread_func, &(args), osPriorityHigh);
Ratchapong 0:052d0f82433e 27 uLCD_thread = _thread;
Ratchapong 0:052d0f82433e 28 //printf("Thread creation complete\n");
Ratchapong 0:052d0f82433e 29 this->uLCD = uLCD;
Ratchapong 0:052d0f82433e 30 }
Ratchapong 0:052d0f82433e 31 void ScreenUpdater::addCommand(Command* _command) {
Ratchapong 0:052d0f82433e 32 //printf("I'm here!");
Ratchapong 0:052d0f82433e 33 commands_mutex.lock();
Ratchapong 0:052d0f82433e 34 commands.push(_command);
Ratchapong 0:052d0f82433e 35 //printf("Adding command %d. State %d\n", commands.size(), uLCD_thread->get_state());
Ratchapong 0:052d0f82433e 36 commands_mutex.unlock();
Ratchapong 0:052d0f82433e 37 uLCD_thread->signal_set(0x1);
Ratchapong 0:052d0f82433e 38 }
Ratchapong 0:052d0f82433e 39 ScreenUpdater::~ScreenUpdater() {
Ratchapong 0:052d0f82433e 40 //printf("KILLING THREAD!!!!!!!!!!!!!!\n");
Ratchapong 0:052d0f82433e 41 uLCD_thread->terminate();
Ratchapong 0:052d0f82433e 42 }