Allows users to seamlessly write to 2 or 3 uLCD screens as if they were one large screen.
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
ScreenUpdater.cpp@0:15002a72309b, 2015-03-11 (annotated)
- Committer:
- Mkuchnik3
- Date:
- Wed Mar 11 21:33:18 2015 +0000
- Revision:
- 0:15002a72309b
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mkuchnik3 | 0:15002a72309b | 1 | #include "ScreenUpdater.h" |
Mkuchnik3 | 0:15002a72309b | 2 | #include "uLCD_4DGL.h" |
Mkuchnik3 | 0:15002a72309b | 3 | #include "mbed.h" |
Mkuchnik3 | 0:15002a72309b | 4 | static void uLCD_thread_func(void const *args) { |
Mkuchnik3 | 0:15002a72309b | 5 | //printf("Thread alive."); |
Mkuchnik3 | 0:15002a72309b | 6 | uLCD_4DGL *lcd = ((uLCD_Thread_Args*)args)->uLCD; |
Mkuchnik3 | 0:15002a72309b | 7 | std::queue<Command*> *commands = ((uLCD_Thread_Args*)args)->commands; |
Mkuchnik3 | 0:15002a72309b | 8 | Mutex *commands_mutex = ((uLCD_Thread_Args*)args)->commands_mutex; |
Mkuchnik3 | 0:15002a72309b | 9 | while (true) { |
Mkuchnik3 | 0:15002a72309b | 10 | if (!commands->empty()) { |
Mkuchnik3 | 0:15002a72309b | 11 | commands_mutex->lock(); |
Mkuchnik3 | 0:15002a72309b | 12 | Command *command = commands->front(); |
Mkuchnik3 | 0:15002a72309b | 13 | commands->pop(); |
Mkuchnik3 | 0:15002a72309b | 14 | command->execute(lcd); |
Mkuchnik3 | 0:15002a72309b | 15 | delete command; |
Mkuchnik3 | 0:15002a72309b | 16 | commands_mutex->unlock(); |
Mkuchnik3 | 0:15002a72309b | 17 | } else { |
Mkuchnik3 | 0:15002a72309b | 18 | //printf("empty."); |
Mkuchnik3 | 0:15002a72309b | 19 | Thread::signal_wait(0x1); |
Mkuchnik3 | 0:15002a72309b | 20 | } |
Mkuchnik3 | 0:15002a72309b | 21 | } |
Mkuchnik3 | 0:15002a72309b | 22 | } |
Mkuchnik3 | 0:15002a72309b | 23 | ScreenUpdater::ScreenUpdater(uLCD_4DGL *uLCD) { |
Mkuchnik3 | 0:15002a72309b | 24 | //printf("ScreenUpdater booting."); |
Mkuchnik3 | 0:15002a72309b | 25 | uLCD_Thread_Args args = {uLCD, &commands, &commands_mutex}; |
Mkuchnik3 | 0:15002a72309b | 26 | Thread *_thread = new Thread(uLCD_thread_func, &(args), osPriorityHigh); |
Mkuchnik3 | 0:15002a72309b | 27 | uLCD_thread = _thread; |
Mkuchnik3 | 0:15002a72309b | 28 | //printf("Thread creation complete\n"); |
Mkuchnik3 | 0:15002a72309b | 29 | this->uLCD = uLCD; |
Mkuchnik3 | 0:15002a72309b | 30 | } |
Mkuchnik3 | 0:15002a72309b | 31 | void ScreenUpdater::addCommand(Command* _command) { |
Mkuchnik3 | 0:15002a72309b | 32 | //printf("I'm here!"); |
Mkuchnik3 | 0:15002a72309b | 33 | commands_mutex.lock(); |
Mkuchnik3 | 0:15002a72309b | 34 | commands.push(_command); |
Mkuchnik3 | 0:15002a72309b | 35 | //printf("Adding command %d. State %d\n", commands.size(), uLCD_thread->get_state()); |
Mkuchnik3 | 0:15002a72309b | 36 | commands_mutex.unlock(); |
Mkuchnik3 | 0:15002a72309b | 37 | uLCD_thread->signal_set(0x1); |
Mkuchnik3 | 0:15002a72309b | 38 | } |
Mkuchnik3 | 0:15002a72309b | 39 | ScreenUpdater::~ScreenUpdater() { |
Mkuchnik3 | 0:15002a72309b | 40 | //printf("KILLING THREAD!!!!!!!!!!!!!!\n"); |
Mkuchnik3 | 0:15002a72309b | 41 | uLCD_thread->terminate(); |
Mkuchnik3 | 0:15002a72309b | 42 | } |