AberLED shield library
Library for the bicolor LED (and TFT screen) shield used in CS12020
AberLED.h
Go to the documentation of this file.
1 /**
2  * @file AberLED.h
3  * @author Jim Finnis (jcf1@aber.ac.uk)
4  * @version 3.0
5  * @date 5 September 2023
6  * @copyright 2014-2023 Jim Finnis, Aberstwyth University.
7  * @brief The declaration for the AberLED class.
8  * Many implementation details are inside the AberLED implementation file
9  * AberLED.cpp. Note that there must be only one of these objects, but there's
10  * no code to enforce this. Weird things will happen if you have
11  * more than one.
12  *
13  */
14 
15 #ifndef __ABERLED_H
16 #define __ABERLED_H
17 
18 #include "Arduino.h"
19 
20 /// the "off" colour for pixels, used in set()
21 #define BLACK 0
22 /// the green colour for pixels, used in set()
23 #define GREEN 1
24 /// the red colour for pixels, used in set()
25 #define RED 2
26 /// the yellow colour for pixels, used in set()
27 #define YELLOW 3
28 
29 ///////////////// board revision codes
30 
31 // beige board
32 #define REV00 0
33 // black board
34 #define REV01 1
35 
36 
37 
38 /// the number for button S1, the "up" button
39 extern int UP;
40 /// the number for button S2, the "down" button
41 extern int DOWN;
42 /// the number for button S3, the "right" button
43 extern int LEFT;
44 /// the number for button S4, the "left" button
45 extern int RIGHT;
46 /// the number for button S5, the "action" or "fire" button
47 extern int FIRE;
48 
50  /// Use the old-style bicolor LED display, not the TFT screen
52  /// Use a TFT screen display (this is the default)
54  /// Do not set up the interrupt. The screen will not be refreshed
55  /// automatically. You will need to do by calling refresh() often.
57 };
58 
59 /// The class for the AberLED shield. One object of this class, called
60 /// **AberLED**, is automatically created (similar to how Serial works).
61 /// Yes, all this time you've been writing C++, not C. You should put
62 /// `AberLED.` before every function, so `AberLED.begin()` instead
63 /// of just `begin()`.
64 ///
65 /// \warning
66 /// If you are using an bicolor LED display, you should call
67 /// `AberLED.begin(AF_LEDDISPLAY)`!
68 ///
69 ///
70 /// Calling begin() will initialise the display, setting all the pins to
71 /// the appropriate mode and setting up the interrupt handler.
72 /// Thereafter, every nth of a second the Arduino will automatically
73 /// send the front buffer to the matrix.
74 /// Calling getBuffer() will give a pointer to the back buffer, which
75 /// can be be written to. Alternatively, pixels can be written by calling
76 /// set() with x,y coordinates and a colour (GREEN, RED, YELLOW or BLACK).
77 /// Once writing is complete, calling swap() will
78 /// exchange the front and back buffers, so that the Arduino will
79 /// now write the new front buffer to the matrix every interrupt tick.
80 ///
81 /// The format of the buffers is 8 16-bit integers: unsigned shorts.
82 /// Each integer represents a single row. Each pixel - individual LED -
83 /// is represented by 2 adjacent bits with the most significant bit
84 /// being red and the least significant bit being green.
85 
86 
87 class AberLEDClass {
88 public:
89  /// Initialises all pin modes, clears the buffers,
90  /// starts the interrupt and begins to output data to the LED.
91  /// \param flags the flags to use - AF_LEDDISPLAY for a bicolor LED display, AF_TFTDISPLAY for a TFT display,
92  /// AF_NOINTERRUPT to use disable interrupts. If you specify AF_NOINTERRUPT, the display will not be refreshed
93  /// automatically and you will have to call refresh() frequently.
94  /// \param colourMap a pointer to an array of 9 8-bit integers - each triple defines how to convert the 2-bit colour
95  /// into RGB. The first 3 bytes are for BLACK, the next 3 for GREEN, the next 3 for RED and the last 3 for YELLOW.
96  /// The default is the "standard" colours but this could be useful for people with colour vision deficiencies.
97 
98  void begin(AberLEDFlags flags=AF_TFTDISPLAY, uint8_t *colourMap=NULL);
99 
100  /// Call this code to finish drawing operations.
101  /// It swaps the back and front buffer, so that
102  /// the newly written back buffer becomes the front buffer and is
103  /// displayed.
104 
105  void swap();
106 
107  /// This sets the given pixel in the back buffer to the given value.
108  /// The pixel values are 0 (off), 1 (green), 2 (red) or 3 (yellow),
109  /// but the macros BLACK, GREEN, RED and YELLOW should be used.
110  /// \param x the x coordinate of the pixel to write (0-7)
111  /// \param y the y coordinate of the pixel to write (0-7)
112  /// \param col the colour to use: BLACK, GREEN, RED or YELLOW.
113 
114  void set(int x, int y, unsigned char col);
115 
116  /// Set all pixels in the back buffer to black
117 
118  void clear();
119 
120  /// Return nonzero if a given switch is pressed - switches are numbered
121  /// 1 to 5, which is against my Golden Rule of computers numbering
122  /// things from zero, but that's how they're labelled on the physical
123  /// board.
124  /// It's better to use the UP, DOWN, LEFT, RIGHT and FIRE constants
125  /// instead of numbers.
126  /// \param c the button code (UP, DOWN, LEFT, RIGHT or FIRE)
127 
128 
129  int getButton(unsigned char c);
130 
131  /// Return nonzero if the button has been pressed since the
132  /// last swap().
133  /// It's better to use the UP, DOWN, LEFT, RIGHT and FIRE constants
134  /// instead of numbers.
135  /// \param c the button code (UP, DOWN, LEFT, RIGHT or FIRE)
136 
137  int getButtonDown(unsigned char c);
138 
139  /// Call this to write to the back buffer directly.
140  /// It returns a pointer to the buffer: a set of 8 16-bit ints,
141  /// each pair of bits representing a pixel.
142 
143  uint16_t *getBuffer();
144 
145  /// Return the number of interrupt ticks which occurred in the
146  /// last swap()-swap() cycle
147  int getTicks();
148 
149  /// clear the string which is written to the text area on a TFT display.
150  void clearText();
151 
152  /// Append a string to the text area on a TFT display. Does nothing
153  /// if the display is not a TFT display. This just stores the data in
154  /// an internal variable, the actual writing is done when swap() is called.
155  /// If the resulting string would be too long, nothing is done.
156  void addToText(const char *s);
157 
158  /// Write a number to the text area - see the string version for details.
159  void addToText(int n);
160 
161  /// Use only when interrupts are disabled - copies the front
162  /// buffer to the display
163  static void refresh();
164 
165  /// Call this to set the revision, after AberLED.begin() (which
166  /// will set it to REV01 by default). <b>Only relevant to LED
167  /// matrix boards, and only the really old beige ones</b>. You
168  /// shouldn't ever need to use this.
169  /// \param rev revision number - REV00 or REV01.
170  static void setRevision(int rev);
171 
172  /// return the version string
173  static const char *version();
174 };
175 
176 /// this is the single instance of the LED class - for documentation see AberLEDClass.
177 
178 extern AberLEDClass AberLED;
179 
180 
181 #endif /* __ABERLED_H */
void clear()
Set all pixels in the back buffer to black.
Definition: AberLED.cpp:356
int UP
the number for button S1, the "up" button
Definition: AberLED.cpp:48
Use a TFT screen display (this is the default)
Definition: AberLED.h:53
void begin(AberLEDFlags flags=AF_TFTDISPLAY, uint8_t *colourMap=NULL)
Initialises all pin modes, clears the buffers, starts the interrupt and begins to output data to the ...
Definition: AberLED.cpp:158
int DOWN
the number for button S2, the "down" button
Definition: AberLED.cpp:48
int getButtonDown(unsigned char c)
Return nonzero if the button has been pressed since the last swap(). It&#39;s better to use the UP...
Definition: AberLED.cpp:326
static void setRevision(int rev)
Call this to set the revision, after AberLED.begin() (which will set it to REV01 by default)...
Definition: AberLED.cpp:88
int RIGHT
the number for button S4, the "left" button
Definition: AberLED.cpp:48
void clearText()
clear the string which is written to the text area on a TFT display.
Definition: AberLED.cpp:291
static void refresh()
Use only when interrupts are disabled - copies the front buffer to the display.
Definition: AberLED.cpp:411
AberLEDClass AberLED
this is the single instance of the LED class - for documentation see AberLEDClass.
Definition: AberLED.cpp:76
The class for the AberLED shield. One object of this class, called AberLED, is automatically created ...
Definition: AberLED.h:87
Do not set up the interrupt. The screen will not be refreshed automatically. You will need to do by c...
Definition: AberLED.h:56
int getButton(unsigned char c)
Return nonzero if a given switch is pressed - switches are numbered 1 to 5, which is against my Golde...
Definition: AberLED.cpp:321
int FIRE
the number for button S5, the "action" or "fire" button
Definition: AberLED.cpp:48
static const char * version()
return the version string
Definition: AberLED.cpp:27
uint16_t * getBuffer()
Call this to write to the back buffer directly. It returns a pointer to the buffer: a set of 8 16-bit...
Definition: AberLED.cpp:334
Use the old-style bicolor LED display, not the TFT screen.
Definition: AberLED.h:51
void addToText(const char *s)
Append a string to the text area on a TFT display. Does nothing if the display is not a TFT display...
Definition: AberLED.cpp:300
void swap()
Call this code to finish drawing operations. It swaps the back and front buffer, so that the newly wr...
Definition: AberLED.cpp:259
int getTicks()
Return the number of interrupt ticks which occurred in the last swap()-swap() cycle.
Definition: AberLED.cpp:109
AberLEDFlags
Definition: AberLED.h:49
int LEFT
the number for button S3, the "right" button
Definition: AberLED.cpp:48