A basic graphics package for the LPC4088 Display Module.

Dependents:   lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI lpc4088_displaymodule_fs_aid ... more

Fork of DMBasicGUI by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Mon Mar 09 10:50:17 2015 +0100
Revision:
14:647b1896ed84
Parent:
13:bff2288c2c61
Child:
15:a68bb30ab95e
Updated code documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 13:bff2288c2c61 1 /*
embeddedartists 13:bff2288c2c61 2 * Copyright 2014 Embedded Artists AB
embeddedartists 13:bff2288c2c61 3 *
embeddedartists 13:bff2288c2c61 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 13:bff2288c2c61 5 * you may not use this file except in compliance with the License.
embeddedartists 13:bff2288c2c61 6 * You may obtain a copy of the License at
embeddedartists 13:bff2288c2c61 7 *
embeddedartists 13:bff2288c2c61 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 13:bff2288c2c61 9 *
embeddedartists 13:bff2288c2c61 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 13:bff2288c2c61 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 13:bff2288c2c61 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 13:bff2288c2c61 13 * See the License for the specific language governing permissions and
embeddedartists 13:bff2288c2c61 14 * limitations under the License.
embeddedartists 13:bff2288c2c61 15 */
embeddedartists 5:f4de114c31c3 16
embeddedartists 5:f4de114c31c3 17 #ifndef SLIDESHOW_H
embeddedartists 5:f4de114c31c3 18 #define SLIDESHOW_H
embeddedartists 5:f4de114c31c3 19
embeddedartists 5:f4de114c31c3 20 #include "mbed.h"
embeddedartists 5:f4de114c31c3 21 #include "rtos.h"
embeddedartists 5:f4de114c31c3 22 #include "Image.h"
embeddedartists 5:f4de114c31c3 23 #include "Renderer.h"
embeddedartists 5:f4de114c31c3 24
embeddedartists 5:f4de114c31c3 25 /**
embeddedartists 14:647b1896ed84 26 * SlideShow engine.
embeddedartists 13:bff2288c2c61 27 *
embeddedartists 13:bff2288c2c61 28 * For information on how to use the SlideShow and some examples see
embeddedartists 13:bff2288c2c61 29 * https://developer.mbed.org/teams/Embedded-Artists/wiki/LPC4088DM-Using-the-SlideShow-Engine
embeddedartists 13:bff2288c2c61 30 *
embeddedartists 13:bff2288c2c61 31 * @code
embeddedartists 13:bff2288c2c61 32 * #include "mbed.h"
embeddedartists 13:bff2288c2c61 33 * #include "SlideShow.h"
embeddedartists 13:bff2288c2c61 34 * #include "Renderer.h"
embeddedartists 13:bff2288c2c61 35 *
embeddedartists 13:bff2288c2c61 36 * static void tRender(void const *args)
embeddedartists 13:bff2288c2c61 37 * {
embeddedartists 13:bff2288c2c61 38 * Renderer* s = (Renderer*)args;
embeddedartists 13:bff2288c2c61 39 * s->render();
embeddedartists 13:bff2288c2c61 40 * }
embeddedartists 13:bff2288c2c61 41 *
embeddedartists 13:bff2288c2c61 42 * int main(void) {
embeddedartists 13:bff2288c2c61 43 * // initialize the display
embeddedartists 13:bff2288c2c61 44 * ...
embeddedartists 13:bff2288c2c61 45 *
embeddedartists 13:bff2288c2c61 46 * // create the renderer that will handle the display
embeddedartists 13:bff2288c2c61 47 * Renderer r;
embeddedartists 13:bff2288c2c61 48 *
embeddedartists 13:bff2288c2c61 49 * // create the slideshow
embeddedartists 13:bff2288c2c61 50 * SlideShow s(&r);
embeddedartists 13:bff2288c2c61 51 *
embeddedartists 13:bff2288c2c61 52 * // parse and validate the script
embeddedartists 13:bff2288c2c61 53 * SlideShow::SlideShowError err = s.prepare("/mci/myscript.txt");
embeddedartists 13:bff2288c2c61 54 * if (err != SlideShow::Ok) {
embeddedartists 13:bff2288c2c61 55 * // handle error here
embeddedartists 13:bff2288c2c61 56 * }
embeddedartists 13:bff2288c2c61 57 *
embeddedartists 13:bff2288c2c61 58 * // Create the thread to handle the display
embeddedartists 13:bff2288c2c61 59 * Thread tr(tRender, &r, osPriorityHigh);
embeddedartists 13:bff2288c2c61 60 * r.setRenderThread(&tr);
embeddedartists 13:bff2288c2c61 61 *
embeddedartists 13:bff2288c2c61 62 * // Run the slideshow
embeddedartists 13:bff2288c2c61 63 * s.run();
embeddedartists 13:bff2288c2c61 64 * }
embeddedartists 13:bff2288c2c61 65 * @endcode
embeddedartists 5:f4de114c31c3 66 */
embeddedartists 5:f4de114c31c3 67 class SlideShow {
embeddedartists 5:f4de114c31c3 68 public:
embeddedartists 5:f4de114c31c3 69
embeddedartists 5:f4de114c31c3 70 enum SlideShowError {
embeddedartists 5:f4de114c31c3 71 Ok,
embeddedartists 5:f4de114c31c3 72 InvalidScript,
embeddedartists 5:f4de114c31c3 73 RuntimeError,
embeddedartists 5:f4de114c31c3 74 UserAbort,
embeddedartists 5:f4de114c31c3 75 ScriptEnd,
embeddedartists 5:f4de114c31c3 76 OutOfMemory,
embeddedartists 5:f4de114c31c3 77 FileError,
embeddedartists 5:f4de114c31c3 78 };
embeddedartists 5:f4de114c31c3 79
embeddedartists 13:bff2288c2c61 80 /** A function that the script can call during execution
embeddedartists 13:bff2288c2c61 81 *
embeddedartists 13:bff2288c2c61 82 * @param calloutId identifier given in setCalloutHandler()
embeddedartists 13:bff2288c2c61 83 * @param ss the slideshow instance
embeddedartists 13:bff2288c2c61 84 * @param identifier parameter to the "callout" tag in the script
embeddedartists 13:bff2288c2c61 85 *
embeddedartists 13:bff2288c2c61 86 * @returns
embeddedartists 13:bff2288c2c61 87 * Ok on success
embeddedartists 13:bff2288c2c61 88 * One of the SlideShowError error codes on failure
embeddedartists 13:bff2288c2c61 89 */
embeddedartists 5:f4de114c31c3 90 typedef SlideShowError (*calloutFunc)(int calloutId, SlideShow* ss, int identifier);
embeddedartists 5:f4de114c31c3 91
embeddedartists 13:bff2288c2c61 92 /** Create a new slideshow
embeddedartists 13:bff2288c2c61 93 *
embeddedartists 13:bff2288c2c61 94 * @param r the renderer
embeddedartists 13:bff2288c2c61 95 * @param pathPrefix optional path to prepend to all paths in the script file
embeddedartists 13:bff2288c2c61 96 * @param xoff optional x coordinate to draw the slideshow at, default is 0
embeddedartists 13:bff2288c2c61 97 * @param yoff optional y coordinate to draw the slideshow at, default is 0
embeddedartists 13:bff2288c2c61 98 * @param layer optional z-order, higher layers are drawn on top of lower layers
embeddedartists 13:bff2288c2c61 99 * @param fileMutex optional mutex to prevent simultaneous access of the file system
embeddedartists 13:bff2288c2c61 100 */
embeddedartists 13:bff2288c2c61 101 SlideShow(Renderer* r, const char* pathPrefix=NULL, int xoff=0, int yoff=0, int layer=0, Mutex* fileMutex=NULL);
embeddedartists 5:f4de114c31c3 102 ~SlideShow();
embeddedartists 13:bff2288c2c61 103
embeddedartists 13:bff2288c2c61 104 /** Parses the script file and prepares internal structures
embeddedartists 13:bff2288c2c61 105 *
embeddedartists 13:bff2288c2c61 106 * @param scriptFile the script with the slideshow
embeddedartists 13:bff2288c2c61 107 *
embeddedartists 13:bff2288c2c61 108 * @returns
embeddedartists 13:bff2288c2c61 109 * Ok on success
embeddedartists 13:bff2288c2c61 110 * One of the SlideShowError error codes on failure
embeddedartists 13:bff2288c2c61 111 */
embeddedartists 5:f4de114c31c3 112 SlideShowError prepare(const char* scriptFile);
embeddedartists 13:bff2288c2c61 113
embeddedartists 13:bff2288c2c61 114 /** Run the slideshow
embeddedartists 13:bff2288c2c61 115 *
embeddedartists 13:bff2288c2c61 116 * This function will run until the script ends. If the script has
embeddedartists 13:bff2288c2c61 117 * an eternal loop then this function will never return.
embeddedartists 13:bff2288c2c61 118 *
embeddedartists 13:bff2288c2c61 119 * @returns
embeddedartists 13:bff2288c2c61 120 * Ok on success
embeddedartists 13:bff2288c2c61 121 * One of the SlideShowError error codes on failure
embeddedartists 13:bff2288c2c61 122 */
embeddedartists 5:f4de114c31c3 123 SlideShowError run();
embeddedartists 13:bff2288c2c61 124
embeddedartists 13:bff2288c2c61 125 /** Register a function that the script can call.
embeddedartists 13:bff2288c2c61 126 *
embeddedartists 13:bff2288c2c61 127 * A callout function allows the use of the "callout" tag in a
embeddedartists 13:bff2288c2c61 128 * slideshow script to e.g. notify the system or to trigger
embeddedartists 13:bff2288c2c61 129 * something.
embeddedartists 13:bff2288c2c61 130 *
embeddedartists 13:bff2288c2c61 131 * There can only be one callout function.
embeddedartists 13:bff2288c2c61 132 */
embeddedartists 5:f4de114c31c3 133 void setCalloutHandler(calloutFunc func, int calloutId);
embeddedartists 13:bff2288c2c61 134
embeddedartists 13:bff2288c2c61 135 /** Decouples this SlideShow from the Renderer
embeddedartists 13:bff2288c2c61 136 */
embeddedartists 5:f4de114c31c3 137 void releaseScreen(void);
embeddedartists 13:bff2288c2c61 138
embeddedartists 13:bff2288c2c61 139 /** Stops the SlideShow.
embeddedartists 13:bff2288c2c61 140 *
embeddedartists 13:bff2288c2c61 141 * The SlideShow will be stopped at the next suitable time,
embeddedartists 13:bff2288c2c61 142 * typically before processing the next step in the script.
embeddedartists 13:bff2288c2c61 143 *
embeddedartists 13:bff2288c2c61 144 * This function is typically called upon an external trigger
embeddedartists 13:bff2288c2c61 145 * like the user pressing a button to end the slideshow.
embeddedartists 13:bff2288c2c61 146 */
embeddedartists 5:f4de114c31c3 147 void stop() { abortBeforeNextStep = true; }
embeddedartists 5:f4de114c31c3 148
embeddedartists 5:f4de114c31c3 149 private:
embeddedartists 5:f4de114c31c3 150
embeddedartists 5:f4de114c31c3 151 typedef uint16_t* image_t;
embeddedartists 5:f4de114c31c3 152
embeddedartists 5:f4de114c31c3 153 class Transition {
embeddedartists 5:f4de114c31c3 154 public:
embeddedartists 5:f4de114c31c3 155 typedef enum
embeddedartists 5:f4de114c31c3 156 {
embeddedartists 5:f4de114c31c3 157 None,
embeddedartists 5:f4de114c31c3 158 LeftRight,
embeddedartists 5:f4de114c31c3 159 DownUp,
embeddedartists 5:f4de114c31c3 160 TopDown,
embeddedartists 5:f4de114c31c3 161 Blinds,
embeddedartists 5:f4de114c31c3 162 Fade,
embeddedartists 5:f4de114c31c3 163 Unknown,
embeddedartists 5:f4de114c31c3 164 } Type;
embeddedartists 5:f4de114c31c3 165
embeddedartists 5:f4de114c31c3 166 Transition(const char* typeStr) {
embeddedartists 5:f4de114c31c3 167 if (typeStr == NULL) {
embeddedartists 5:f4de114c31c3 168 t = Unknown;
embeddedartists 5:f4de114c31c3 169 } else if (strcmp("none", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 170 t = None;
embeddedartists 5:f4de114c31c3 171 } else if (strcmp("left-right", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 172 t = LeftRight;
embeddedartists 5:f4de114c31c3 173 } else if (strcmp("down-up", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 174 t = DownUp;
embeddedartists 5:f4de114c31c3 175 } else if (strcmp("top-down", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 176 t = TopDown;
embeddedartists 5:f4de114c31c3 177 } else if (strcmp("blinds", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 178 t = Blinds;
embeddedartists 5:f4de114c31c3 179 } else if (strcmp("fade", typeStr) == 0) {
embeddedartists 5:f4de114c31c3 180 t = Fade;
embeddedartists 5:f4de114c31c3 181 } else {
embeddedartists 5:f4de114c31c3 182 t = Unknown;
embeddedartists 5:f4de114c31c3 183 }
embeddedartists 5:f4de114c31c3 184 };
embeddedartists 5:f4de114c31c3 185 ~Transition();
embeddedartists 5:f4de114c31c3 186 SlideShowError execute(SlideShow* ss, Image::ImageData_t* CurrentImage, Image::ImageData_t* NewImage);
embeddedartists 5:f4de114c31c3 187 Type type() { return this->t; }
embeddedartists 5:f4de114c31c3 188 const char* typeString() {
embeddedartists 5:f4de114c31c3 189 switch(t) {
embeddedartists 5:f4de114c31c3 190 case None: return "No";
embeddedartists 5:f4de114c31c3 191 case LeftRight: return "Left to Right";
embeddedartists 5:f4de114c31c3 192 case TopDown: return "Top to Bottom";
embeddedartists 5:f4de114c31c3 193 case Blinds: return "Blinds";
embeddedartists 5:f4de114c31c3 194 case Unknown:
embeddedartists 5:f4de114c31c3 195 default:
embeddedartists 5:f4de114c31c3 196 return "Unknown";
embeddedartists 5:f4de114c31c3 197 }
embeddedartists 5:f4de114c31c3 198 };
embeddedartists 5:f4de114c31c3 199
embeddedartists 5:f4de114c31c3 200 private:
embeddedartists 5:f4de114c31c3 201 Type t;
embeddedartists 5:f4de114c31c3 202
embeddedartists 5:f4de114c31c3 203 enum Constants {
embeddedartists 5:f4de114c31c3 204 LeftRight_DelayMs = 100,
embeddedartists 5:f4de114c31c3 205 LeftRight_PixelsToSkip = 20,
embeddedartists 5:f4de114c31c3 206 DownUp_DelayMs = 100,
embeddedartists 5:f4de114c31c3 207 DownUp_LineSkip = 20,
embeddedartists 5:f4de114c31c3 208 TopDown_DelayMs = 100,
embeddedartists 5:f4de114c31c3 209 TopDown_LineSkip = 20,
embeddedartists 5:f4de114c31c3 210 Blinds_LinesPerBlock = 8,
embeddedartists 5:f4de114c31c3 211 Blinds_DelayMs = 20,
embeddedartists 5:f4de114c31c3 212 Blinds_BeamColor = 0xffff,
embeddedartists 5:f4de114c31c3 213 Blinds_BackColor = 0x0000,
embeddedartists 5:f4de114c31c3 214 Fade_DelayMs = 80,
embeddedartists 5:f4de114c31c3 215 };
embeddedartists 5:f4de114c31c3 216 };
embeddedartists 5:f4de114c31c3 217
embeddedartists 5:f4de114c31c3 218 class Command {
embeddedartists 5:f4de114c31c3 219 public:
embeddedartists 5:f4de114c31c3 220 typedef enum
embeddedartists 5:f4de114c31c3 221 {
embeddedartists 5:f4de114c31c3 222 Clear,
embeddedartists 5:f4de114c31c3 223 Goto,
embeddedartists 5:f4de114c31c3 224 LoadImage,
embeddedartists 5:f4de114c31c3 225 Show,
embeddedartists 5:f4de114c31c3 226 Wait,
embeddedartists 5:f4de114c31c3 227 Callout,
embeddedartists 5:f4de114c31c3 228 } Type;
embeddedartists 5:f4de114c31c3 229
embeddedartists 5:f4de114c31c3 230 Command(Type cmd, int info=0, Transition* t=NULL, const char* filename=NULL, const char* prefix=NULL) {
embeddedartists 5:f4de114c31c3 231 type = cmd;
embeddedartists 5:f4de114c31c3 232 information = info;
embeddedartists 5:f4de114c31c3 233 transition = t;
embeddedartists 5:f4de114c31c3 234 if (filename == NULL) {
embeddedartists 5:f4de114c31c3 235 fname = NULL;
embeddedartists 5:f4de114c31c3 236 } else {
embeddedartists 5:f4de114c31c3 237 fname = (char*)malloc(strlen(filename) + strlen(prefix) + 1); //+1 for null termination
embeddedartists 5:f4de114c31c3 238 if (fname != NULL) {
embeddedartists 5:f4de114c31c3 239 fname[0] = '\0';
embeddedartists 5:f4de114c31c3 240 strcat(fname, prefix);
embeddedartists 5:f4de114c31c3 241 strcat(fname, filename);
embeddedartists 5:f4de114c31c3 242 }
embeddedartists 5:f4de114c31c3 243 }
embeddedartists 5:f4de114c31c3 244 };
embeddedartists 5:f4de114c31c3 245 ~Command() {
embeddedartists 5:f4de114c31c3 246 if (fname != NULL) {
embeddedartists 5:f4de114c31c3 247 free(fname);
embeddedartists 5:f4de114c31c3 248 fname = NULL;
embeddedartists 5:f4de114c31c3 249 }
embeddedartists 5:f4de114c31c3 250 };
embeddedartists 5:f4de114c31c3 251 void updateInfo(int newInfo) { information = newInfo; }
embeddedartists 5:f4de114c31c3 252 int info() { return information; }
embeddedartists 5:f4de114c31c3 253 void print();
embeddedartists 5:f4de114c31c3 254 SlideShowError handle(SlideShow* ss, int* seqIdx, int* lastTime);
embeddedartists 5:f4de114c31c3 255 private:
embeddedartists 5:f4de114c31c3 256 Type type;
embeddedartists 5:f4de114c31c3 257 Transition* transition;
embeddedartists 5:f4de114c31c3 258 int information;
embeddedartists 5:f4de114c31c3 259 char* fname;
embeddedartists 5:f4de114c31c3 260 };
embeddedartists 5:f4de114c31c3 261
embeddedartists 5:f4de114c31c3 262
embeddedartists 5:f4de114c31c3 263 typedef enum
embeddedartists 5:f4de114c31c3 264 {
embeddedartists 5:f4de114c31c3 265 Bmp,
embeddedartists 5:f4de114c31c3 266 Raw
embeddedartists 5:f4de114c31c3 267 } FileFormat;
embeddedartists 5:f4de114c31c3 268
embeddedartists 5:f4de114c31c3 269 typedef struct
embeddedartists 5:f4de114c31c3 270 {
embeddedartists 5:f4de114c31c3 271 const char* pLabel;
embeddedartists 5:f4de114c31c3 272 int index;
embeddedartists 5:f4de114c31c3 273 } LabelInfo;
embeddedartists 5:f4de114c31c3 274
embeddedartists 5:f4de114c31c3 275 enum {
embeddedartists 5:f4de114c31c3 276 MaxNumPreparedImages = 100,
embeddedartists 5:f4de114c31c3 277 } Constants;
embeddedartists 5:f4de114c31c3 278
embeddedartists 5:f4de114c31c3 279 int screenWidth;
embeddedartists 5:f4de114c31c3 280 int screenHeight;
embeddedartists 5:f4de114c31c3 281 int screenPixels;
embeddedartists 5:f4de114c31c3 282 int screenBytes;
embeddedartists 5:f4de114c31c3 283 int drawXoff;
embeddedartists 5:f4de114c31c3 284 int drawYoff;
embeddedartists 5:f4de114c31c3 285
embeddedartists 5:f4de114c31c3 286 image_t ImageBackBuffer;
embeddedartists 5:f4de114c31c3 287 Command** Sequence;
embeddedartists 5:f4de114c31c3 288 int allocatedSequenceItems;
embeddedartists 5:f4de114c31c3 289 int usedSequenceItems;
embeddedartists 5:f4de114c31c3 290
embeddedartists 5:f4de114c31c3 291 const char* pathPrefix;
embeddedartists 5:f4de114c31c3 292
embeddedartists 5:f4de114c31c3 293 int CurrentSlot;
embeddedartists 5:f4de114c31c3 294 Image::ImageData_t PreparedImages[MaxNumPreparedImages];
embeddedartists 5:f4de114c31c3 295
embeddedartists 5:f4de114c31c3 296 Mutex* fileMutex;
embeddedartists 5:f4de114c31c3 297
embeddedartists 5:f4de114c31c3 298 Renderer* rend;
embeddedartists 5:f4de114c31c3 299 uint32_t rendHnd;
embeddedartists 5:f4de114c31c3 300 int layer;
embeddedartists 5:f4de114c31c3 301
embeddedartists 5:f4de114c31c3 302 calloutFunc callout;
embeddedartists 5:f4de114c31c3 303 int calloutId;
embeddedartists 5:f4de114c31c3 304
embeddedartists 5:f4de114c31c3 305 bool abortBeforeNextStep;
embeddedartists 5:f4de114c31c3 306
embeddedartists 5:f4de114c31c3 307 // Utilities
embeddedartists 5:f4de114c31c3 308 SlideShowError loadFile(const char* path, uint8_t** pData, uint32_t* pSize);
embeddedartists 5:f4de114c31c3 309
embeddedartists 5:f4de114c31c3 310 // Parsing functions
embeddedartists 5:f4de114c31c3 311 int getNextLine(char* pBuf, int* pOffset, char** ppLine);
embeddedartists 5:f4de114c31c3 312 int splitLine(char* pLine, char** ppPart1, char** ppPart2, char** ppPart3);
embeddedartists 5:f4de114c31c3 313 int findLabel(LabelInfo* pLabels, int numLabels, const char* pLabel);
embeddedartists 5:f4de114c31c3 314 void freeSequence(void);
embeddedartists 5:f4de114c31c3 315 SlideShowError expandSequence();
embeddedartists 5:f4de114c31c3 316 SlideShowError parseScript(char* pBuf);
embeddedartists 5:f4de114c31c3 317
embeddedartists 5:f4de114c31c3 318 // Command related functions
embeddedartists 5:f4de114c31c3 319 SlideShowError loadImage(const char* pFileName, int slot);
embeddedartists 5:f4de114c31c3 320 void delay(int lastTime, int millis);
embeddedartists 5:f4de114c31c3 321 SlideShowError runScript();
embeddedartists 5:f4de114c31c3 322
embeddedartists 5:f4de114c31c3 323 };
embeddedartists 5:f4de114c31c3 324
embeddedartists 5:f4de114c31c3 325 #endif