Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Backlight/Backlight.cpp@1:951d0ba43cb1, 2018-04-20 (annotated)
- Committer:
- abestat2
- Date:
- Fri Apr 20 01:52:39 2018 +0000
- Revision:
- 1:951d0ba43cb1
Changed Backlight from library to folder
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| abestat2 | 1:951d0ba43cb1 | 1 | #include "mbed.h" |
| abestat2 | 1:951d0ba43cb1 | 2 | #include "Backlight.h" |
| abestat2 | 1:951d0ba43cb1 | 3 | #include "ColorDefs.h" |
| abestat2 | 1:951d0ba43cb1 | 4 | |
| abestat2 | 1:951d0ba43cb1 | 5 | Backlight::Backlight(PinName mosiPin) : _bus(mosiPin,NC,D13) { |
| abestat2 | 1:951d0ba43cb1 | 6 | |
| abestat2 | 1:951d0ba43cb1 | 7 | _bus.format(8,1); |
| abestat2 | 1:951d0ba43cb1 | 8 | _bus.frequency(3333333); // 1/0.3us (4 pixels per out clock) |
| abestat2 | 1:951d0ba43cb1 | 9 | _brightness = 1.0; |
| abestat2 | 1:951d0ba43cb1 | 10 | |
| abestat2 | 1:951d0ba43cb1 | 11 | _statusColor = COLOR_OFF; |
| abestat2 | 1:951d0ba43cb1 | 12 | |
| abestat2 | 1:951d0ba43cb1 | 13 | CreateRawData(); |
| abestat2 | 1:951d0ba43cb1 | 14 | WritePixels(); |
| abestat2 | 1:951d0ba43cb1 | 15 | } |
| abestat2 | 1:951d0ba43cb1 | 16 | |
| abestat2 | 1:951d0ba43cb1 | 17 | void Backlight::UpdateBrightness(float brightness) { |
| abestat2 | 1:951d0ba43cb1 | 18 | _brightness = brightness; |
| abestat2 | 1:951d0ba43cb1 | 19 | } |
| abestat2 | 1:951d0ba43cb1 | 20 | |
| abestat2 | 1:951d0ba43cb1 | 21 | void Backlight::UpdateBacklight(BacklightStatus status) { |
| abestat2 | 1:951d0ba43cb1 | 22 | |
| abestat2 | 1:951d0ba43cb1 | 23 | switch(status) { |
| abestat2 | 1:951d0ba43cb1 | 24 | case BACKLIGHT_OK: |
| abestat2 | 1:951d0ba43cb1 | 25 | _statusColor = COLOR_OK; |
| abestat2 | 1:951d0ba43cb1 | 26 | break; |
| abestat2 | 1:951d0ba43cb1 | 27 | case BACKLIGHT_CONFIG: |
| abestat2 | 1:951d0ba43cb1 | 28 | _statusColor = COLOR_CONFIG; |
| abestat2 | 1:951d0ba43cb1 | 29 | break; |
| abestat2 | 1:951d0ba43cb1 | 30 | case BACKLIGHT_OFF: |
| abestat2 | 1:951d0ba43cb1 | 31 | _statusColor = COLOR_OFF; |
| abestat2 | 1:951d0ba43cb1 | 32 | break; |
| abestat2 | 1:951d0ba43cb1 | 33 | case BACKLIGHT_ERROR: |
| abestat2 | 1:951d0ba43cb1 | 34 | default: |
| abestat2 | 1:951d0ba43cb1 | 35 | _statusColor = COLOR_ERROR; |
| abestat2 | 1:951d0ba43cb1 | 36 | break; |
| abestat2 | 1:951d0ba43cb1 | 37 | } |
| abestat2 | 1:951d0ba43cb1 | 38 | |
| abestat2 | 1:951d0ba43cb1 | 39 | CreateRawData(); |
| abestat2 | 1:951d0ba43cb1 | 40 | |
| abestat2 | 1:951d0ba43cb1 | 41 | WritePixels(); |
| abestat2 | 1:951d0ba43cb1 | 42 | } |
| abestat2 | 1:951d0ba43cb1 | 43 | |
| abestat2 | 1:951d0ba43cb1 | 44 | void Backlight::CreateRawData(void) { |
| abestat2 | 1:951d0ba43cb1 | 45 | // Encode '0' bits as 1000 and '1' bits as 1100. |
| abestat2 | 1:951d0ba43cb1 | 46 | // Rearrange this partern: abcdefgh into this: 1a001b0001c001d001e001f001g001h00 |
| abestat2 | 1:951d0ba43cb1 | 47 | |
| abestat2 | 1:951d0ba43cb1 | 48 | // 255 - 0xCCCCCCCC; 0 - 0x88888888 |
| abestat2 | 1:951d0ba43cb1 | 49 | |
| abestat2 | 1:951d0ba43cb1 | 50 | uint32_t pixelColor,base; |
| abestat2 | 1:951d0ba43cb1 | 51 | base = 0x88888888; |
| abestat2 | 1:951d0ba43cb1 | 52 | |
| abestat2 | 1:951d0ba43cb1 | 53 | for(int i = 0; i < NUM_PIXELS; i++) { |
| abestat2 | 1:951d0ba43cb1 | 54 | for(int j = 0; j < NUM_COLORS_PER_PIXEL; j++) { |
| abestat2 | 1:951d0ba43cb1 | 55 | uint32_t tempColor = (uint32_t)_statusColor[i][j]*_brightness; |
| abestat2 | 1:951d0ba43cb1 | 56 | pixelColor = (tempColor << 26) & 0x40000000; |
| abestat2 | 1:951d0ba43cb1 | 57 | pixelColor |= (tempColor << 23) & 0x04000000; |
| abestat2 | 1:951d0ba43cb1 | 58 | pixelColor |= (tempColor << 20) & 0x00400000; |
| abestat2 | 1:951d0ba43cb1 | 59 | pixelColor |= (tempColor << 17) & 0x00040000; |
| abestat2 | 1:951d0ba43cb1 | 60 | pixelColor |= (tempColor << 14) & 0x00004000; |
| abestat2 | 1:951d0ba43cb1 | 61 | pixelColor |= (tempColor << 11) & 0x00000400; |
| abestat2 | 1:951d0ba43cb1 | 62 | pixelColor |= (tempColor << 8) & 0x00000040; |
| abestat2 | 1:951d0ba43cb1 | 63 | pixelColor |= (tempColor << 5) & 0x00000004; |
| abestat2 | 1:951d0ba43cb1 | 64 | pixelColor |= base; |
| abestat2 | 1:951d0ba43cb1 | 65 | // Invert signal since MOSI is idle high so idle can be 0 |
| abestat2 | 1:951d0ba43cb1 | 66 | pixelColor = ~pixelColor; |
| abestat2 | 1:951d0ba43cb1 | 67 | _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)] = pixelColor & 0x000000FF; |
| abestat2 | 1:951d0ba43cb1 | 68 | _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+1] = (pixelColor >> 8) & 0x000000FF; |
| abestat2 | 1:951d0ba43cb1 | 69 | _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+2] = (pixelColor >> 16) & 0x000000FF; |
| abestat2 | 1:951d0ba43cb1 | 70 | _rawPixelData[(i*NUM_BYTES_PER_COLOR*NUM_COLORS_PER_PIXEL)+(j*NUM_BYTES_PER_COLOR)+3] = (pixelColor >> 24) & 0x000000FF; |
| abestat2 | 1:951d0ba43cb1 | 71 | } |
| abestat2 | 1:951d0ba43cb1 | 72 | } |
| abestat2 | 1:951d0ba43cb1 | 73 | } |
| abestat2 | 1:951d0ba43cb1 | 74 | |
| abestat2 | 1:951d0ba43cb1 | 75 | void Backlight::WritePixels(void) { |
| abestat2 | 1:951d0ba43cb1 | 76 | _bus.lock(); |
| abestat2 | 1:951d0ba43cb1 | 77 | _bus.write((char*)_rawPixelData,TOTAL_PIXEL_BYTES,(char*)_rxBuffer,TOTAL_PIXEL_BYTES); |
| abestat2 | 1:951d0ba43cb1 | 78 | _bus.unlock(); |
| abestat2 | 1:951d0ba43cb1 | 79 | } |