Adafruit Arcada Library
arcadatype.h
1 
2 #ifndef ARCADATYPE
3 #define ARCADATYPE
4 
5 #include "Adafruit_Arcada_Def.h"
6 #include <Adafruit_GFX.h>
7 #include <Adafruit_ImageReader.h> // Image-reading functions
8 #include <Adafruit_NeoPixel.h>
9 #include <Adafruit_SPIFlash.h>
10 #include <Adafruit_WavePlayer.h>
11 #include <SdFat.h>
12 #include <TouchScreen.h>
13 
14 #if defined(USE_TINYUSB)
15 #include "Adafruit_TinyUSB.h"
16 #endif
17 
18 #if defined(ARCADA_USE_JSON)
19 #include <ArduinoJson.h>
20 #endif
21 
22 #define SD_MAX_FILENAME_SIZE 80
23 #define ARCADA_DEFAULT_CONFIGURATION_FILENAME "/arcada_config.json"
24 
25 typedef void (*arcada_callback_t)(void);
26 
28 typedef enum _FilesystemType {
29  ARCADA_FILESYS_NONE,
30  ARCADA_FILESYS_SD,
31  ARCADA_FILESYS_QSPI,
32  ARCADA_FILESYS_SD_AND_QSPI
33 } Arcada_FilesystemType;
34 
35 /**************************************************************************/
41 /**************************************************************************/
43 
44 public:
45  Adafruit_SPITFT *display =
46  0;
47 
49 
50  bool arcadaBegin(void);
51 
52  /**************************************************************************/
58  /**************************************************************************/
59  virtual bool variantBegin(void) = 0;
60 
61  /**************************************************************************/
66  /**************************************************************************/
67  virtual uint32_t variantReadButtons(void) = 0;
68 
69  bool timerCallback(float freq, arcada_callback_t callback);
70  float getTimerCallbackFreq(void);
71 
76  arcada_callback_t getTimerCallback(void);
77 
78  void timerStop(void);
79 
80  void printf(const char *format, ...);
81 
82  // Filesystem stuff!
83  Arcada_FilesystemType filesysBegin(
84  Arcada_FilesystemType desiredFilesys = ARCADA_FILESYS_SD_AND_QSPI);
85  int16_t filesysListFiles(const char *path = NULL,
86  const char *extensionFilter = NULL);
87  bool chdir(const char *path);
88  File open(const char *path = NULL, uint32_t flags = O_READ);
89  File openFileByIndex(const char *path, uint16_t index,
90  uint32_t flags = O_READ,
91  const char *extensionFilter = NULL);
92 
93  bool exists(const char *path);
94  bool mkdir(const char *path);
95  bool remove(const char *path);
96  uint8_t *writeDataToFlash(uint8_t *data, uint32_t len);
97  uint8_t *writeFileToFlash(const char *filename);
98  uint32_t availableFlash(void);
99  bool filesysBeginMSD(
100  Arcada_FilesystemType desiredFilesys = ARCADA_FILESYS_SD_AND_QSPI);
101  bool recentUSB(uint32_t timeout = 100);
102  bool chooseFile(const char *path, char *filename, uint16_t filename_max,
103  const char *extensionFilter = NULL);
104 
105  bool hasControlPad(void);
106  int16_t readJoystickX(uint8_t oversampling = 3);
107  int16_t readJoystickY(uint8_t oversampling = 3);
108  uint32_t readButtons(void);
109  uint32_t justPressedButtons(void);
110  uint32_t justReleasedButtons(void);
111 
112  bool hasTouchscreen(void);
113  void setTouchscreenCalibration(int16_t xmin, int16_t xmax, int16_t ymin,
114  int16_t ymax);
115  TSPoint getTouchscreenPoint(void);
116 
117  uint16_t readLightSensor(void);
118  float readBatterySensor(void);
119 
120  /**************************************************************************/
124  /**************************************************************************/
125  virtual void displayBegin(void) = 0;
126  bool createFrameBuffer(uint16_t width, uint16_t height);
127  /************************************************************************/
132  /************************************************************************/
133  GFXcanvas16 *getCanvas(void) { return _canvas; }
134  /************************************************************************/
139  /************************************************************************/
140  uint16_t *getFrameBuffer(void) {
141  return _canvas ? _canvas->getBuffer() : NULL;
142  }
143  bool blitFrameBuffer(uint16_t x, uint16_t y, bool blocking = false,
144  bool bigEndian = false, Adafruit_SPITFT *display = NULL);
145  uint16_t ColorHSV565(int16_t H, uint8_t S, uint8_t V);
146 
147  bool setBacklight(uint8_t brightness, bool saveToDisk = false);
148  uint8_t getBacklight(void);
149  bool setVolume(uint8_t volume, bool saveToDisk = false);
150  uint8_t getVolume(void);
151  void enableSpeaker(bool flag);
152 
153  // Alerts
154  void alertBox(const char *string, uint16_t boxColor, uint16_t textColor,
155  uint32_t continueButtonMask);
156  void infoBox(const char *string,
157  uint32_t continueButtonMask = ARCADA_BUTTONMASK_A);
158  void warnBox(const char *string,
159  uint32_t continueButtonMask = ARCADA_BUTTONMASK_A);
160  void errorBox(const char *string,
161  uint32_t continueButtonMask = ARCADA_BUTTONMASK_A);
162  void haltBox(const char *string);
163  uint8_t menu(const char **menu_strings, uint8_t menu_num, uint16_t boxColor,
164  uint16_t textColor, bool cancellable = false);
165 
166  // Configuration JSON files
168  const char *filename = ARCADA_DEFAULT_CONFIGURATION_FILENAME);
170  const char *filename = ARCADA_DEFAULT_CONFIGURATION_FILENAME);
171 
172  Adafruit_NeoPixel
174 
175  /**************************************************************************/
180  /**************************************************************************/
181 
182  bool hasAccel(void) { return _has_accel; }
183 
184  /**************************************************************************/
189  /**************************************************************************/
190  bool hasWiFi(void) { return _has_wifi; }
191 
192 #ifdef ARCADA_USE_JSON
193  StaticJsonDocument<256>
194  configJSON;
195 #endif
197 
198  ImageReturnCode drawBMP(char *filename, int16_t x, int16_t y,
199  Adafruit_SPITFT *tft = 0x0, boolean transact = true);
200  Adafruit_ImageReader *getImageReader(void);
201 
202  wavStatus WavLoad(char *filename, uint32_t *samplerate);
203  wavStatus WavLoad(File f, uint32_t *samplerate);
204  wavStatus WavReadFile();
205  wavStatus WavPlayNextSample(void);
206  bool WavReadyForData();
207  wavStatus WavPlayComplete(char *filename);
208  wavStatus WavPlayComplete(File f);
209 
210 protected:
211  bool _has_accel =
212  false;
213  bool _has_wifi =
214  false;
215  Adafruit_ImageReader
217  0,
219  0;
220 
221 private:
222  uint32_t last_buttons,
223  curr_buttons,
225  justpressed_buttons,
227  justreleased_buttons;
229 
231  void _initAlertFonts(void);
232 
233  int16_t _joyx_center = 512;
234  int16_t _joyy_center = 512;
235 
236  Arcada_FilesystemType _filesys_type = ARCADA_FILESYS_NONE;
237  char _cwd_path[255];
238 
239  GFXcanvas16 *_canvas = NULL;
240  bool _first_frame = true;
241 
242  uint8_t _volume = 255, _brightness = 255;
243 
244  TouchScreen *_touchscreen;
245  int16_t _ts_xmin = 0, _ts_xmax = 1023, _ts_ymin = 0, _ts_ymax = 1023;
246 
247  Adafruit_WavePlayer *player = NULL;
248  File _wav_file;
249  volatile bool _wav_readflag = true;
250  volatile bool _wav_playing = false;
251 
252  float _callback_freq = 0;
253  arcada_callback_t _callback_func = NULL;
254 
255  // Pins
256  int8_t _sd_cs, _speaker_en, _neopixel_pin, _backlight_pin, _touch_xp,
257  _touch_yp, _touch_xm, _touch_ym,
258  _start_button = -1, _select_button, _a_button, _b_button, _up_button,
259  _down_button, _left_button, _right_button, _button_latch, _button_clock,
260  _button_data, _battery_sensor, _light_sensor, _joystick_x, _joystick_y;
261  uint8_t _neopixel_num = 0, _shift_up, _shift_down, _shift_left, _shift_right,
262  _shift_a, _shift_b, _shift_select, _shift_start;
263 };
264 
265 #endif
bool _has_wifi
Internally tracked variable if wifi module was found.
Definition: arcadatype.h:213
bool setBacklight(uint8_t brightness, bool saveToDisk=false)
Set the backlight brightness and save to the configuration.
Definition: Adafruit_Arcada.cpp:182
void haltBox(const char *string)
Display an error box and halt operation.
Definition: Adafruit_Arcada_Alerts.cpp:63
uint16_t * getFrameBuffer(void)
Return address of internal framebuffer (NULL if not allocated)
Definition: arcadatype.h:140
wavStatus WavReadFile()
Reads another buffer&#39;s full of wave data into the internal buffer from the filesystem.
Definition: Adafruit_Arcada_Wav.cpp:84
bool recentUSB(uint32_t timeout=100)
Hints whether we&#39;re doing a bunch of USB stuff recently.
Definition: Adafruit_Arcada_USBMSD.cpp:128
bool loadConfigurationFile(const char *filename=ARCADA_DEFAULT_CONFIGURATION_FILENAME)
Loads the configuration JSON from the filesystem (which must be initialized before this function is c...
Definition: Adafruit_Arcada_Config.cpp:16
float getTimerCallbackFreq(void)
Get the final frequency created for the callback helper.
Definition: Adafruit_Arcada.cpp:336
Arcada_FilesystemType filesysBegin(Arcada_FilesystemType desiredFilesys=ARCADA_FILESYS_SD_AND_QSPI)
Initialize the filesystem, either SD or QSPI.
Definition: Adafruit_Arcada_Filesystem.cpp:44
bool _has_accel
Internally tracked variable if accelerometer was found.
Definition: arcadatype.h:211
bool hasControlPad(void)
Does this board have a control pad - latch, buttons or analog joystick?
Definition: Adafruit_Arcada.cpp:782
wavStatus WavLoad(char *filename, uint32_t *samplerate)
Load up a wave file from the filesystem, to prepare for playback.
Definition: Adafruit_Arcada_Wav.cpp:29
bool hasAccel(void)
Getter for accelerometer existance.
Definition: arcadatype.h:182
uint32_t readButtons(void)
Read all buttons/joystick and return a bitmask of which buttons are pressed, check ARCADA_BUTTONMASK_...
Definition: Adafruit_Arcada.cpp:437
virtual uint32_t variantReadButtons(void)=0
Perform any variant input reading (say if we use seesaw or whatnot)
Adafruit_ImageReader * QSPI_imagereader
If initalized, the imagereader for the QSPI filesystem.
Definition: arcadatype.h:216
int16_t readJoystickX(uint8_t oversampling=3)
Read X analog joystick.
Definition: Adafruit_Arcada.cpp:392
bool saveConfigurationFile(const char *filename=ARCADA_DEFAULT_CONFIGURATION_FILENAME)
Saves the configuration JSON to the filesystem (which must be initialized before this function is cal...
Definition: Adafruit_Arcada_Config.cpp:53
uint8_t * writeFileToFlash(const char *filename)
Opens a file and writes the data contents to the internal chip flash memory. NOT the QSPI flash but t...
Definition: Adafruit_Arcada_InternalFlash.cpp:216
void infoBox(const char *string, uint32_t continueButtonMask=ARCADA_BUTTONMASK_A)
Display an info box with optional &#39;press to continue&#39; button.
Definition: Adafruit_Arcada_Alerts.cpp:26
bool chdir(const char *path)
Set working filesys directory to a given path (makes file naming easier)
Definition: Adafruit_Arcada_Filesystem.cpp:102
bool chooseFile(const char *path, char *filename, uint16_t filename_max, const char *extensionFilter=NULL)
Presents a navigation menu for choosing a file from the filesys.
Definition: Adafruit_Arcada_Filesystem.cpp:364
GFXcanvas16 * getCanvas(void)
Return pointer to GFX canvas (NULL if not allocated)
Definition: arcadatype.h:133
uint16_t ColorHSV565(int16_t H, uint8_t S, uint8_t V)
Convert hue, saturation and value into a packed 16-bit RGB color that can be passed to TFT...
Definition: Adafruit_Arcada.cpp:802
uint16_t readLightSensor(void)
Read the light sensor onboard if there is one.
Definition: Adafruit_Arcada.cpp:592
bool setVolume(uint8_t volume, bool saveToDisk=false)
Set the audio volume (not working at this time)
Definition: Adafruit_Arcada.cpp:220
Adafruit_Arcada_SPITFT(void)
Instantiator for Arcada class, will allso inistantiate (but not init) the TFT.
Definition: Adafruit_Arcada.cpp:27
uint32_t justReleasedButtons(void)
What buttons were just released as of the last readButtons() call. Use ARCADA_BUTTONMASK_* defines to...
Definition: Adafruit_Arcada.cpp:582
bool hasTouchscreen(void)
Does this board have a touchscreen.
Definition: Adafruit_Arcada.cpp:692
bool hasWiFi(void)
Gets the status of the ESP32 module connected via SPI.
Definition: arcadatype.h:190
bool filesysBeginMSD(Arcada_FilesystemType desiredFilesys=ARCADA_FILESYS_SD_AND_QSPI)
Make the raw filesystem of the Arcada board available over USB.
Definition: Adafruit_Arcada_USBMSD.cpp:36
bool blitFrameBuffer(uint16_t x, uint16_t y, bool blocking=false, bool bigEndian=false, Adafruit_SPITFT *display=NULL)
Write the internal framebuffer to the display at coord (x, y)
Definition: Adafruit_Arcada.cpp:662
uint8_t getVolume(void)
Get the audio volume (not working at this time)
Definition: Adafruit_Arcada.cpp:237
uint8_t menu(const char **menu_strings, uint8_t menu_num, uint16_t boxColor, uint16_t textColor, bool cancellable=false)
Draws a menu and lets a user select one of the menu items.
Definition: Adafruit_Arcada_Alerts.cpp:200
uint8_t getBacklight(void)
Get the backlight brightness.
Definition: Adafruit_Arcada.cpp:209
void alertBox(const char *string, uint16_t boxColor, uint16_t textColor, uint32_t continueButtonMask)
Display an alert box with optional &#39;press to continue&#39; button.
Definition: Adafruit_Arcada_Alerts.cpp:80
Adafruit_SPITFT * display
The Adafruit_GFX subclass display which the variant must create.
Definition: arcadatype.h:45
Adafruit_NeoPixel pixels
The neopixel strip, of length ARCADA_NEOPIXEL_NUM.
Definition: arcadatype.h:173
virtual void displayBegin(void)=0
Initialize TFT display, doesn&#39;t turn on the backlight.
bool mkdir(const char *path)
Make a directory in the filesys.
Definition: Adafruit_Arcada_Filesystem.cpp:215
Adafruit_ImageReader * SD_imagereader
If initalized, the imagereader for the SD card filesystem.
Definition: arcadatype.h:218
void setTouchscreenCalibration(int16_t xmin, int16_t xmax, int16_t ymin, int16_t ymax)
Set the X/Y calibration values for the touchscreen.
Definition: Adafruit_Arcada.cpp:703
uint32_t justPressedButtons(void)
What buttons were just pressed as of the last readButtons() call. Use ARCADA_BUTTONMASK_* defines to ...
Definition: Adafruit_Arcada.cpp:570
int16_t filesysListFiles(const char *path=NULL, const char *extensionFilter=NULL)
Debugging helper, prints to Serial a list of files in a path.
Definition: Adafruit_Arcada_Filesystem.cpp:144
bool createFrameBuffer(uint16_t width, uint16_t height)
Create (allocate) an internal GFX canvas of given width and height.
Definition: Adafruit_Arcada.cpp:622
virtual bool variantBegin(void)=0
Perform any specialized variant setup such as initializing accelerometers, seesaw, etc.
uint8_t * writeDataToFlash(uint8_t *data, uint32_t len)
Write a block of data in RAM to the NEXT AVAILABLE position in flash memory (NOT a specific location)...
Definition: Adafruit_Arcada_InternalFlash.cpp:81
void errorBox(const char *string, uint32_t continueButtonMask=ARCADA_BUTTONMASK_A)
Display an error box with optional &#39;press to continue&#39; button.
Definition: Adafruit_Arcada_Alerts.cpp:52
int16_t readJoystickY(uint8_t oversampling=3)
Read Y analog joystick.
Definition: Adafruit_Arcada.cpp:414
uint32_t availableFlash(void)
Determine amount of unused flash memory remaining.
Definition: Adafruit_Arcada_InternalFlash.cpp:29
bool timerCallback(float freq, arcada_callback_t callback)
Create a repetative callback to a function using a timer.
Definition: Adafruit_Arcada.cpp:260
wavStatus WavPlayNextSample(void)
Output the next sample to the DAC(s) - should be called at &#39;samplerate&#39;!
Definition: Adafruit_Arcada_Wav.cpp:108
void printf(const char *format,...)
printf wrapper to serial debug, handy for logging config, C inclusion
Definition: Adafruit_Arcada.cpp:376
void enableSpeaker(bool flag)
Turn on the speaker amplifier.
Definition: Adafruit_Arcada.cpp:245
TSPoint getTouchscreenPoint(void)
Get a calibrated point that corresponds to the TFT.
Definition: Adafruit_Arcada.cpp:720
File openFileByIndex(const char *path, uint16_t index, uint32_t flags=O_READ, const char *extensionFilter=NULL)
Opens a file as an indexed # in a directory (0 is first file), and returns the object.
Definition: Adafruit_Arcada_Filesystem.cpp:312
void timerStop(void)
Stop a previously-initiated timer.
Definition: Adafruit_Arcada.cpp:364
wavStatus WavPlayComplete(char *filename)
Play a wave file completely thru by blocking until end of file.
Definition: Adafruit_Arcada_Wav.cpp:155
bool arcadaBegin(void)
Initialize GPIO, NeoPixels, and speaker.
Definition: Adafruit_Arcada.cpp:77
arcada_callback_t getTimerCallback(void)
Get the previous callback function we were using.
Definition: Adafruit_Arcada.cpp:351
bool exists(const char *path)
Tests if a file exists on the filesys.
Definition: Adafruit_Arcada_Filesystem.cpp:199
File open(const char *path=NULL, uint32_t flags=O_READ)
Opens a file and returns the object, a wrapper for our filesystem.
Definition: Adafruit_Arcada_Filesystem.cpp:263
ImageReturnCode drawBMP(char *filename, int16_t x, int16_t y, Adafruit_SPITFT *tft=0x0, boolean transact=true)
Loads BMP image file from QSPI or SD card directly to SPITFT screen.
Definition: Adafruit_Arcada_ImageReader.cpp:23
Adafruit_ImageReader * getImageReader(void)
Reques the ImageReader object used, either for SD or QSPI.
Definition: Adafruit_Arcada_ImageReader.cpp:43
void warnBox(const char *string, uint32_t continueButtonMask=ARCADA_BUTTONMASK_A)
Display an warning box with optional &#39;press to continue&#39; button.
Definition: Adafruit_Arcada_Alerts.cpp:39
float readBatterySensor(void)
Read the batterysensor onboard if there is one.
Definition: Adafruit_Arcada.cpp:606
bool WavReadyForData()
Checks if we need to load the double buffer with WavReadFile() soon!
Definition: Adafruit_Arcada_Wav.cpp:99
An abstraction class for something with a display, and possibly button controls or speaker...
Definition: arcadatype.h:42