Dilbert
Firmware for the Dilbert interactive badge.
 All Classes Files Functions Variables Enumerations Macros Pages
StarsApp.h
Go to the documentation of this file.
1 
3 #ifndef _STARSAPP_H_
4 #define _STARSAPP_H_
5 
6 #include <App.h>
7 
8 struct Star
9 {
10  long col;
11  float x;
12  float y;
13  int size;
14 };
15 
21 class StarsApp : public App
22 {
23 public:
24  static const int STAR_COUNT = 100; /* Number of Stars */
25 
26 public:
27  StarsApp()
28  : App("Stars")
29  {
30  }
31 
32  virtual ~StarsApp()
33  {
34  }
35 
39  virtual void onEntry()
40  {
41  App::onEntry();
42  /* Clear the screen */
43  m_badge->display().fillScreen(ILI9341_BLACK);
44 
45  for (int a = 0; a < STAR_COUNT; a++)
46  {
47  newRandomStar(a);
48  drawStar(a);
49  }
50  }
51 
55  virtual void run()
56  {
57  App::run();
58 
59  /* Keep backlight on */
60  m_manager->feedBacklight(BACKLIGHT_STATE_DIM);
61 
62  if (millis() >= m_lastRan + m_frameDelay and m_mode != 5)
63  {
64  m_lastRan = millis();
65  for (int a = 0; a < STAR_COUNT; a++)
66  {
67  hideStar(a);
68  if (m_mode == 0) /* LEFT */
69  {
70  m_stars[a].x += m_speed;
71  if (m_stars[a].x >= 240)
72  {
73  newRandomStar(a);
74  m_stars[a].x = random(m_speed);
75  }
76  }
77  else if (m_mode == 1) /* UP */
78  {
79  m_stars[a].y -= m_speed;
80  if (m_stars[a].y < 0)
81  {
82  newRandomStar(a);
83  m_stars[a].y = 319 - random(m_speed);
84  }
85  }
86  else if (m_mode == 2) /* DOWN */
87  {
88  m_stars[a].y += m_speed;
89  if (m_stars[a].y >= 320)
90  {
91  newRandomStar(a);
92  m_stars[a].y = random(m_speed);
93  }
94  }
95  else if (m_mode == 3) /* RIGHT */
96  {
97  m_stars[a].x -= m_speed;
98  if (m_stars[a].x < 0)
99  {
100  newRandomStar(a);
101  m_stars[a].x = 239 - random(m_speed);
102  }
103  }
104  else if (m_mode == 4) /* FORWARDS */
105  {
106  m_stars[a].x += (m_stars[a].x - 120) * m_zoom;
107  m_stars[a].y += (m_stars[a].y - 160) * m_zoom;
108  if (m_stars[a].x < 0 or m_stars[a].x >= 240 or m_stars[a].y < 0 or m_stars[a].y >= 320)
109  {
110  newRandomStar(a);
111  }
112  }
113  drawStar(a);
114  }
115  }
116  }
117 
121  virtual bool handleButton(IButton *button)
122  {
123  if (App::handleButton(button))
124  return true;
125 
126  if (button->isActive())
127  {
128  m_mode = button->getID();
129  return true;
130  }
131 
132  return false;
133  }
134 
135 private:
136  struct Star m_stars[STAR_COUNT];
137  long m_lastRan = millis();
138  int m_frameDelay = 150;
139  float m_zoom =
140  .1; /* Speed of forward motion (% of distance from centre point to add each frame) */
141  int m_speed = 10; /* No of pixels to travel for each frame in L/R/U/D modes */
142  int m_mode = 5; /* Direction of travel (relates to button IDs) */
143 
144  void newRandomStar(int a)
145  {
146  m_stars[a].col = random(16777215);
147  m_stars[a].size = random(3);
148  m_stars[a].size = random(m_stars[a].size);
149  do
150  {
151  m_stars[a].x = random(240);
152  m_stars[a].y = random(320);
153  } while (m_stars[a].x == 120 and m_stars[a].y == 160);
154  }
155 
156  void hideStar(int a)
157  {
158  if (m_stars[a].size == 0)
159  {
160  m_badge->display().drawPixel(int(m_stars[a].x), int(m_stars[a].y), 0);
161  }
162  else
163  {
164  m_badge->display().fillCircle(int(m_stars[a].x), int(m_stars[a].y), m_stars[a].size, 0);
165  }
166  }
167 
168  void drawStar(int a)
169  {
170  if (m_stars[a].size == 0)
171  {
172  m_badge->display().drawPixel(int(m_stars[a].x), int(m_stars[a].y), m_stars[a].col);
173  }
174  else
175  {
176  m_badge->display().fillCircle(int(m_stars[a].x), int(m_stars[a].y), m_stars[a].size,
177  m_stars[a].col);
178  }
179  }
180 };
181 
182 #endif
virtual bool handleButton(IButton *button)
Handle button presses.
Definition: StarsApp.h:121
Adafruit_ILI9341 & display()
Gets the TFT display driver.
Definition: Dilbert.h:44
AppManager * m_manager
Pointer to application manager.
Definition: App.h:130
virtual void onEntry()
Called when the application is entered.
Definition: App.h:70
virtual bool handleButton(IButton *button)
Handle button presses.
Definition: App.h:121
virtual void run()
Called in a loop while the application is active.
Definition: App.h:83
App(char *name)
Creates a new instance of a badge application.
Definition: App.h:22
Definition: StarsApp.h:8
virtual void onEntry()
Called when the application is entered.
Definition: StarsApp.h:39
virtual void run()
Definition: StarsApp.h:55
Dilbert * m_badge
Pointer to badge driver.
Definition: App.h:129
Displays Starfield.
Definition: StarsApp.h:21
Used to encapsulate an individual application.
Definition: App.h:15
void feedBacklight(uint8_t status=BACKLIGHT_STATE_FULL)
Ensures that the backlight stays on, otherwise it is susceptible to the timeouts set in SystemConfigD...
Definition: AppManager.cpp:152