![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
screensaver extension to uLCD
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
main.cpp@1:8a33c01add45, 2015-03-13 (annotated)
- Committer:
- jdanner3
- Date:
- Fri Mar 13 09:42:17 2015 +0000
- Revision:
- 1:8a33c01add45
- Parent:
- 0:b193ec191a95
commented out problematic int WHITE statement
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jdanner3 | 0:b193ec191a95 | 1 | #include "mbed.h" |
jdanner3 | 0:b193ec191a95 | 2 | #include "uLCD_4DGL.h" |
jdanner3 | 0:b193ec191a95 | 3 | #include "rtos.h" |
jdanner3 | 0:b193ec191a95 | 4 | #include <vector> |
jdanner3 | 0:b193ec191a95 | 5 | |
jdanner3 | 0:b193ec191a95 | 6 | DigitalOut led1(LED1); |
jdanner3 | 0:b193ec191a95 | 7 | DigitalOut led2(LED2); |
jdanner3 | 0:b193ec191a95 | 8 | DigitalOut led3(LED3); |
jdanner3 | 0:b193ec191a95 | 9 | DigitalOut led4(LED4); |
jdanner3 | 0:b193ec191a95 | 10 | uLCD_4DGL lcd(p28, p27, p30); |
jdanner3 | 0:b193ec191a95 | 11 | DigitalIn refreshButton(p21); |
jdanner3 | 0:b193ec191a95 | 12 | Serial pc(USBTX, USBRX); |
jdanner3 | 0:b193ec191a95 | 13 | Mutex mutex; |
jdanner3 | 0:b193ec191a95 | 14 | |
jdanner3 | 0:b193ec191a95 | 15 | // Function declarations |
jdanner3 | 0:b193ec191a95 | 16 | void checkRefreshButton(); |
jdanner3 | 0:b193ec191a95 | 17 | void screensaver(void const*); |
jdanner3 | 0:b193ec191a95 | 18 | |
jdanner3 | 0:b193ec191a95 | 19 | int main() { |
jdanner3 | 0:b193ec191a95 | 20 | refreshButton.mode(PullUp); |
jdanner3 | 0:b193ec191a95 | 21 | lcd.background_color(0x00000000); |
jdanner3 | 0:b193ec191a95 | 22 | |
jdanner3 | 0:b193ec191a95 | 23 | // Draw some initial objects |
jdanner3 | 1:8a33c01add45 | 24 | //int WHITE = 0x00FFFFFF; |
jdanner3 | 0:b193ec191a95 | 25 | lcd.filled_circle(64, 64, 25, WHITE); |
jdanner3 | 0:b193ec191a95 | 26 | lcd.filled_circle(25, 45, 25, WHITE); |
jdanner3 | 0:b193ec191a95 | 27 | lcd.filled_circle(90, 90, 25, WHITE); |
jdanner3 | 0:b193ec191a95 | 28 | lcd.filled_circle(90, 35, 25, WHITE); |
jdanner3 | 0:b193ec191a95 | 29 | Thread screensave(screensaver); |
jdanner3 | 0:b193ec191a95 | 30 | |
jdanner3 | 0:b193ec191a95 | 31 | while(1) { |
jdanner3 | 0:b193ec191a95 | 32 | // Lock the LCD and check to see if screensaver |
jdanner3 | 0:b193ec191a95 | 33 | // should be (de)activated |
jdanner3 | 0:b193ec191a95 | 34 | mutex.lock(); |
jdanner3 | 0:b193ec191a95 | 35 | lcd.checkActiveScreenTime(); |
jdanner3 | 0:b193ec191a95 | 36 | mutex.unlock(); |
jdanner3 | 0:b193ec191a95 | 37 | |
jdanner3 | 0:b193ec191a95 | 38 | // Check state of screen refresh pushbutton |
jdanner3 | 0:b193ec191a95 | 39 | checkRefreshButton(); |
jdanner3 | 0:b193ec191a95 | 40 | led1 = !led1; |
jdanner3 | 0:b193ec191a95 | 41 | Thread::wait(500); |
jdanner3 | 0:b193ec191a95 | 42 | } |
jdanner3 | 0:b193ec191a95 | 43 | } |
jdanner3 | 0:b193ec191a95 | 44 | |
jdanner3 | 0:b193ec191a95 | 45 | |
jdanner3 | 0:b193ec191a95 | 46 | // Graphics for the screensaver |
jdanner3 | 0:b193ec191a95 | 47 | void screensaver(void const* p) { |
jdanner3 | 0:b193ec191a95 | 48 | |
jdanner3 | 0:b193ec191a95 | 49 | // Show the screensaver |
jdanner3 | 0:b193ec191a95 | 50 | int trail_size = 64; |
jdanner3 | 0:b193ec191a95 | 51 | int xbound_l = 0; |
jdanner3 | 0:b193ec191a95 | 52 | int xbound_r = 127; |
jdanner3 | 0:b193ec191a95 | 53 | int ybound_t = 0; |
jdanner3 | 0:b193ec191a95 | 54 | int ybound_b = 127; |
jdanner3 | 0:b193ec191a95 | 55 | |
jdanner3 | 0:b193ec191a95 | 56 | int x = 64; |
jdanner3 | 0:b193ec191a95 | 57 | int y = 64; |
jdanner3 | 0:b193ec191a95 | 58 | int px = 0; |
jdanner3 | 0:b193ec191a95 | 59 | int py = 0; |
jdanner3 | 0:b193ec191a95 | 60 | int dx = 2; |
jdanner3 | 0:b193ec191a95 | 61 | int dy = 1; |
jdanner3 | 0:b193ec191a95 | 62 | int loopCounter = 0; |
jdanner3 | 0:b193ec191a95 | 63 | |
jdanner3 | 0:b193ec191a95 | 64 | uint8_t R = 0xFF; |
jdanner3 | 0:b193ec191a95 | 65 | uint8_t G = 0x00; |
jdanner3 | 0:b193ec191a95 | 66 | uint8_t dG = 0x02; |
jdanner3 | 0:b193ec191a95 | 67 | uint8_t B = 0xFF; |
jdanner3 | 0:b193ec191a95 | 68 | uint8_t dB = 0x02; |
jdanner3 | 0:b193ec191a95 | 69 | uint32_t COLOR = 0x0; |
jdanner3 | 0:b193ec191a95 | 70 | |
jdanner3 | 0:b193ec191a95 | 71 | lcd.baudrate(3000000); |
jdanner3 | 0:b193ec191a95 | 72 | |
jdanner3 | 0:b193ec191a95 | 73 | std::vector<int> trail_px; |
jdanner3 | 0:b193ec191a95 | 74 | std::vector<int> trail_py; |
jdanner3 | 0:b193ec191a95 | 75 | |
jdanner3 | 0:b193ec191a95 | 76 | while(1) { |
jdanner3 | 0:b193ec191a95 | 77 | if (!lcd.isInScreensaverMode()) { |
jdanner3 | 0:b193ec191a95 | 78 | Thread::yield(); |
jdanner3 | 0:b193ec191a95 | 79 | } else { |
jdanner3 | 0:b193ec191a95 | 80 | |
jdanner3 | 0:b193ec191a95 | 81 | if (loopCounter++ == 100000) { |
jdanner3 | 0:b193ec191a95 | 82 | COLOR=0x0; |
jdanner3 | 0:b193ec191a95 | 83 | R = ~B; |
jdanner3 | 0:b193ec191a95 | 84 | G += dG; |
jdanner3 | 0:b193ec191a95 | 85 | B += dB; |
jdanner3 | 0:b193ec191a95 | 86 | |
jdanner3 | 0:b193ec191a95 | 87 | COLOR |= R << 16; |
jdanner3 | 0:b193ec191a95 | 88 | COLOR |= G << 8; |
jdanner3 | 0:b193ec191a95 | 89 | COLOR |= B; |
jdanner3 | 0:b193ec191a95 | 90 | COLOR = COLOR ^ 0xFFFFFF; |
jdanner3 | 0:b193ec191a95 | 91 | |
jdanner3 | 0:b193ec191a95 | 92 | px = x; |
jdanner3 | 0:b193ec191a95 | 93 | py = y; |
jdanner3 | 0:b193ec191a95 | 94 | |
jdanner3 | 0:b193ec191a95 | 95 | trail_px.push_back(px); |
jdanner3 | 0:b193ec191a95 | 96 | trail_py.push_back(py); |
jdanner3 | 0:b193ec191a95 | 97 | if(trail_px.size() > trail_size) { |
jdanner3 | 0:b193ec191a95 | 98 | lcd.pixel(*trail_px.begin(), *trail_py.begin(), 0x0, true); |
jdanner3 | 0:b193ec191a95 | 99 | trail_px.erase( trail_px.begin() ); |
jdanner3 | 0:b193ec191a95 | 100 | trail_py.erase( trail_py.begin() ); |
jdanner3 | 0:b193ec191a95 | 101 | } |
jdanner3 | 0:b193ec191a95 | 102 | |
jdanner3 | 0:b193ec191a95 | 103 | if(trail_px.size() > trail_size) trail_px.pop_back(); |
jdanner3 | 0:b193ec191a95 | 104 | if(trail_py.size() > trail_size) trail_py.pop_back(); |
jdanner3 | 0:b193ec191a95 | 105 | |
jdanner3 | 0:b193ec191a95 | 106 | if((x+dx) >= xbound_r || (x+dx) <= xbound_l) dx *= -1; |
jdanner3 | 0:b193ec191a95 | 107 | if((y+dy) >= ybound_b || (y+dy) <= ybound_t) dy *= -1; |
jdanner3 | 0:b193ec191a95 | 108 | |
jdanner3 | 0:b193ec191a95 | 109 | x += dx; |
jdanner3 | 0:b193ec191a95 | 110 | y += dy; |
jdanner3 | 0:b193ec191a95 | 111 | |
jdanner3 | 0:b193ec191a95 | 112 | mutex.lock(); |
jdanner3 | 0:b193ec191a95 | 113 | lcd.pixel(x, y, COLOR, true); |
jdanner3 | 0:b193ec191a95 | 114 | mutex.unlock(); |
jdanner3 | 0:b193ec191a95 | 115 | |
jdanner3 | 0:b193ec191a95 | 116 | loopCounter = 0; |
jdanner3 | 0:b193ec191a95 | 117 | } |
jdanner3 | 0:b193ec191a95 | 118 | } |
jdanner3 | 0:b193ec191a95 | 119 | } |
jdanner3 | 0:b193ec191a95 | 120 | } |
jdanner3 | 0:b193ec191a95 | 121 | |
jdanner3 | 0:b193ec191a95 | 122 | |
jdanner3 | 0:b193ec191a95 | 123 | // Check the state of the screen refresh pushbutton |
jdanner3 | 0:b193ec191a95 | 124 | void checkRefreshButton() { |
jdanner3 | 0:b193ec191a95 | 125 | if (refreshButton == 0) { |
jdanner3 | 0:b193ec191a95 | 126 | if (lcd.isInScreensaverMode()) { |
jdanner3 | 0:b193ec191a95 | 127 | mutex.lock(); |
jdanner3 | 0:b193ec191a95 | 128 | lcd.exitScreensaverMode(); |
jdanner3 | 0:b193ec191a95 | 129 | mutex.unlock(); |
jdanner3 | 0:b193ec191a95 | 130 | } |
jdanner3 | 0:b193ec191a95 | 131 | else { |
jdanner3 | 0:b193ec191a95 | 132 | lcd.reportScreenInteraction(); |
jdanner3 | 0:b193ec191a95 | 133 | } |
jdanner3 | 0:b193ec191a95 | 134 | } |
jdanner3 | 0:b193ec191a95 | 135 | } |