BackgroundRingClass.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "Arduino.h"
  2. #include "Adafruit_NeoPixel.h"
  3. #include "BackgroundRingClass.h"
  4. #include "WS2812_Definitions.h"
  5. BackgroundRingClass::BackgroundRingClass(int led_count, int pin) : Adafruit_NeoPixel(led_count+1, pin, NEO_GRB + NEO_KHZ800) {
  6. this->led_count = led_count;
  7. this->action = Idle;
  8. this->begin();
  9. }
  10. void BackgroundRingClass::tick() {
  11. switch(this->action) {
  12. case Idle: this->tick_idle(); break;
  13. case Solid: this->tick_solid(); break;
  14. case Pulse: this->tick_pulse(); break;
  15. case PulseAndBeep: this->tick_pulse_and_beep(); break;
  16. case Spin: this->tick_spin(); break;
  17. case Flip: this->tick_flip(); break;
  18. case Slowroll: this->tick_slowroll(); break;
  19. default: this->action = Idle;
  20. }
  21. }
  22. /* Ring Functions */
  23. void BackgroundRingClass::setColor(unsigned long color, uint8_t brightness, bool show) {
  24. this->setBrightness(brightness);
  25. for(int i=0; i < this->led_count; i++) {
  26. this->setPixelColor(i, color);
  27. }
  28. if(show) {
  29. this->show();
  30. }
  31. }
  32. void BackgroundRingClass::setRepeating(unsigned long* colors, int elements, int rotation) {
  33. this->setBrightness(50);
  34. int curcolor = 0;
  35. for(int i = 0; i < this->led_count; i++) {
  36. this->setPixelColor(i, colors[(i+rotation) % elements]);
  37. }
  38. this->show();
  39. }
  40. ///////////////////////// Idle
  41. void BackgroundRingClass::init_idle() {
  42. this->action = Idle;
  43. this->setColor(BLACK, 255, true);
  44. }
  45. void BackgroundRingClass::tick_idle() {
  46. // noop. Here for consistency
  47. }
  48. ///////////////////////// Random
  49. int BackgroundRingClass::random_event(int copy) {
  50. int draw = random(0, 480); // this lets us choose events with weighted probability
  51. if(copy >= 0) { // we wish to copy the other one's action
  52. draw = copy;
  53. }
  54. // Christmas Colors
  55. if(draw < 50) this->init_pulse(DARKRED, 80, 2000);
  56. else if(draw < 100) this->init_pulse(DARKGREEN, 80, 2000);
  57. else if(draw < 150) this->init_spin(DARKRED, DARKGREEN);
  58. else if(draw < 200) this->init_spin(DARKGREEN, DARKRED);
  59. else if(draw < 250) this->init_flip(DARKRED, DARKGREEN);
  60. else if(draw < 300) this->init_flip(DARKGREEN, DARKRED);
  61. else if(draw < 350) this->init_slowroll(DARKRED, DARKGREEN);
  62. else if(draw < 400) this->init_slowroll(DARKGREEN, DARKRED);
  63. // random colors
  64. else if(draw < 410) this->init_pulse(random(0, 0xFFFFFF), 80, 2000);
  65. else if(draw < 420) this->init_pulse(random(0, 0xFFFFFF), 80, 2000);
  66. else if(draw < 430) this->init_spin(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  67. else if(draw < 440) this->init_spin(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  68. else if(draw < 450) this->init_flip(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  69. else if(draw < 460) this->init_flip(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  70. else if(draw < 470) this->init_slowroll(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  71. else if(draw < 480) this->init_slowroll(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
  72. return draw;
  73. }
  74. ///////////////////////// Solid
  75. void BackgroundRingClass::init_solid(unsigned long color, uint8_t brightness) {
  76. this->action = Solid;
  77. this->setBrightness(brightness);
  78. this->setColor(color);
  79. this->show();
  80. }
  81. void BackgroundRingClass::tick_solid() {
  82. // noop. Here for consistency
  83. }
  84. ///////////////////////// Pulse
  85. void BackgroundRingClass::init_pulse(unsigned long color, uint8_t maxbrightness, unsigned long ms) {
  86. Serial.println("Initializing pulse");
  87. this->action = Pulse;
  88. this->pulseinfo.color = color;
  89. this->pulseinfo.maxbrightness = maxbrightness;
  90. this->pulseinfo.ms = ms * 1000;
  91. this->pulseinfo.starttime = micros();
  92. this->pulseinfo.curbrightness = 1;
  93. this->pulseinfo.steptime = (ms * 1000) / (2 * (unsigned long)maxbrightness);
  94. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true); // dimmest setting
  95. }
  96. void BackgroundRingClass::tick_pulse() {
  97. unsigned long elapsed = micros() - this->pulseinfo.starttime;
  98. int newbrightness;
  99. if(elapsed < this->pulseinfo.ms/2) {
  100. // increasing
  101. newbrightness = (elapsed / this->pulseinfo.steptime) + 1;
  102. //Serial.print("Increasing. elapsed="); Serial.print(elapsed);
  103. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  104. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  105. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  106. } else if(elapsed < this->pulseinfo.ms) {
  107. // decreasing
  108. newbrightness = ((this->pulseinfo.ms - elapsed) / this->pulseinfo.steptime) + 1;
  109. //Serial.print("Decreasing. elapsed="); Serial.print(elapsed);
  110. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  111. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  112. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  113. } else {
  114. // done
  115. //Serial.print("DONE. elapsed="); Serial.print(elapsed);
  116. this->init_idle();
  117. return;
  118. }
  119. if(newbrightness != this->pulseinfo.curbrightness) {
  120. this->pulseinfo.curbrightness = newbrightness;
  121. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  122. this->show();
  123. }
  124. return;
  125. }
  126. ///////////////////////// Pulse and Beep
  127. //
  128. // We can't call show() while beeping because it disabled interrupts,
  129. // so we beep at the peak of the pulse.
  130. //
  131. void BackgroundRingClass::init_pulse_and_beep(
  132. unsigned long color,
  133. uint8_t maxbrightness,
  134. unsigned long ms,
  135. int pin,
  136. unsigned int frequency,
  137. unsigned long duration
  138. ) {
  139. Serial.println("Initializing pulse and beep");
  140. this->action = PulseAndBeep;
  141. this->pulseinfo.color = color;
  142. this->pulseinfo.maxbrightness = maxbrightness;
  143. this->pulseinfo.ms = ms * 1000;
  144. this->pulseinfo.starttime = micros();
  145. this->pulseinfo.curbrightness = 1;
  146. this->pulseinfo.steptime = ( (ms - duration) * 1000) / ((2 * (unsigned long)maxbrightness) - 1);
  147. this->beepinfo.pin = pin;
  148. this->beepinfo.frequency = frequency;
  149. this->beepinfo.duration = duration;
  150. this->beepinfo.state = Beeping;
  151. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true); // dimmest setting
  152. }
  153. void BackgroundRingClass::tick_pulse_and_beep() {
  154. // What we're doing should be based on time.
  155. // Increasing: 0 to (ms - duration)/2))
  156. // Beeping: (ms - duration)/2 to (ms - duration/2)+duration
  157. // Decreasing: (ms - duration/2)+duration - ms
  158. unsigned long elapsed = micros() - this->pulseinfo.starttime;
  159. int newbrightness;
  160. if(elapsed < (this->pulseinfo.ms/2 - 1000*this->beepinfo.duration/2)) {
  161. // increasing
  162. this->beepinfo.state = Increasing;
  163. newbrightness = (elapsed / this->pulseinfo.steptime) + 1;
  164. //Serial.print("Increasing. elapsed="); Serial.print(elapsed);
  165. //Serial.print("; ms="); Serial.print(this->pulseinfo.ms);
  166. //Serial.print("; duration="); Serial.print(this->beepinfo.duration);
  167. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  168. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  169. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  170. } else if(elapsed < (this->pulseinfo.ms/2 + 1000*this->beepinfo.duration/2)) {
  171. if(this->beepinfo.state != Beeping && this->beepinfo.frequency != 0) {
  172. tone(this->beepinfo.pin, this->beepinfo.frequency, this->beepinfo.duration);
  173. this->beepinfo.state = Beeping;
  174. }
  175. //Serial.print("Beeping. elapsed="); Serial.print(elapsed);
  176. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  177. //Serial.print("; curbrightness="); Serial.println(this->pulseinfo.curbrightness);
  178. return;
  179. } else if(elapsed < this->pulseinfo.ms) {
  180. // decreasing
  181. this->beepinfo.state = Decreasing;
  182. //newbrightness = ((this->pulseinfo.ms - this->beepinfo.duration/2) / this->pulseinfo.steptime) + 1;
  183. newbrightness = ((this->pulseinfo.ms - elapsed) / this->pulseinfo.steptime) + 1;
  184. //Serial.print("Decreasing. elapsed="); Serial.print(elapsed);
  185. //Serial.print("; ms="); Serial.print(this->pulseinfo.ms);
  186. //Serial.print("; duration="); Serial.print(this->beepinfo.duration);
  187. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  188. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  189. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  190. } else {
  191. // done
  192. //Serial.print("DONE. elapsed="); Serial.print(elapsed);
  193. this->init_idle();
  194. return;
  195. }
  196. if(newbrightness != this->pulseinfo.curbrightness) {
  197. this->pulseinfo.curbrightness = newbrightness;
  198. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  199. this->show();
  200. }
  201. return;
  202. }
  203. /*
  204. void BackgroundRingClass::tick_pulse_and_beep() {
  205. int stepspassed = 0;
  206. if(this->beepinfo.state == Beeping && (micros() - this->beepinfo.beepstart < 1000*this->beepinfo.duration)) {
  207. return;
  208. } else if(this->beepinfo.state == Beeping) {
  209. Serial.print("Beep Duration was: "); Serial.println((micros() - this->beepinfo.beepstart)/1000);
  210. Serial.print("Stopping beeping at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  211. this->beepinfo.state = Decreasing;
  212. this->pulseinfo.starttime = micros() + this->pulseinfo.steptime;
  213. }
  214. if((micros() - this->pulseinfo.starttime) < this->pulseinfo.steptime) {
  215. return;
  216. } else {
  217. stepspassed = (micros() - this->pulseinfo.starttime)/this->pulseinfo.steptime;
  218. }
  219. //Serial.print("Taking action at t="); Serial.print(micros() - this->beepinfo.truestart);
  220. //Serial.print("; brightness="); Serial.print(this->pulseinfo.curbrightness);
  221. //Serial.print("; Steps to take="); Serial.println(stepspassed);
  222. this->pulseinfo.starttime = micros();
  223. if(this->beepinfo.state == Increasing && this->pulseinfo.curbrightness == this->pulseinfo.maxbrightness) {
  224. this->beepinfo.state = Beeping;
  225. this->beepinfo.beepstart = micros();
  226. if(this->beepinfo.frequency != 0) {
  227. Serial.print("Beginning beeping at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  228. tone(this->beepinfo.pin, this->beepinfo.frequency, this->beepinfo.duration);
  229. return;
  230. }
  231. }
  232. if(this->beepinfo.state == Increasing) {
  233. if(this->pulseinfo.curbrightness < this->pulseinfo.maxbrightness - stepspassed) {
  234. this->pulseinfo.curbrightness += stepspassed;
  235. } else {
  236. this->pulseinfo.curbrightness = this->pulseinfo.maxbrightness;
  237. }
  238. } else {
  239. if(this->pulseinfo.curbrightness > stepspassed) {
  240. this->pulseinfo.curbrightness -= stepspassed;
  241. } else {
  242. this->pulseinfo.curbrightness = 0;
  243. }
  244. }
  245. if(this->pulseinfo.curbrightness == 0) {
  246. // We are done
  247. Serial.print("Stopping pulse at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  248. this->init_idle();
  249. } else {
  250. //Serial.print("Setting Brightness: "); Serial.println(this->pulseinfo.curbrightness);
  251. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  252. this->show();
  253. }
  254. }
  255. */
  256. ///////////////////////// Spin
  257. void BackgroundRingClass::init_spin(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  258. this->action = Spin;
  259. this->spininfo.color1 = color1;
  260. this->spininfo.color2 = color2;
  261. this->spininfo.ms = ms * 1000;
  262. this->spininfo.tickdelay = tickdelay * 1000;
  263. this->spininfo.starttime = micros();
  264. this->spininfo.lasttick = micros() - this->spininfo.tickdelay;
  265. this->spininfo.rotation = 0;
  266. }
  267. void BackgroundRingClass::tick_spin() {
  268. if(micros() - this->spininfo.starttime > this->spininfo.ms) {
  269. // we're done
  270. Serial.println(micros() - this->spininfo.starttime);
  271. this->init_idle();
  272. return;
  273. }
  274. if(micros() - this->spininfo.lasttick > this->spininfo.tickdelay) {
  275. // Time to spin
  276. this->spininfo.lasttick = micros();
  277. this->spininfo.rotation = (this->spininfo.rotation + 1) % 3;
  278. //unsigned long tickcolors[] = { this->spininfo.color1, this->spininfo.color1, this->spininfo.color1, this->spininfo.color2 };
  279. //unsigned long tickcolors[] = { BLACK, this->spininfo.color1, this->spininfo.color2 };
  280. unsigned long tickcolors[] = { BLACK, this->spininfo.color1, this->spininfo.color1, this->spininfo.color2 };
  281. this->setRepeating(tickcolors, 4, this->spininfo.rotation);
  282. }
  283. }
  284. ///////////////////////// Flip
  285. void BackgroundRingClass::init_flip(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  286. this->action = Flip;
  287. this->flipinfo.color1 = color1;
  288. this->flipinfo.color2 = color2;
  289. this->flipinfo.ms = ms * 1000;
  290. this->flipinfo.tickdelay = tickdelay * 1000;
  291. this->flipinfo.starttime = micros();
  292. this->flipinfo.lasttick = micros() - this->flipinfo.tickdelay;
  293. this->flipinfo.flipper = false;
  294. }
  295. void BackgroundRingClass::tick_flip() {
  296. if(micros() - this->flipinfo.starttime > this->flipinfo.ms) {
  297. // we're done
  298. Serial.println(micros() - this->flipinfo.starttime);
  299. this->init_idle();
  300. return;
  301. }
  302. if(micros() - this->flipinfo.lasttick > this->flipinfo.tickdelay) {
  303. // Time to flip
  304. this->flipinfo.lasttick = micros();
  305. unsigned long tickcolors[] = { this->flipinfo.color1, this->flipinfo.color2 };
  306. this->setRepeating(tickcolors, 2, (this->flipinfo.flipper ? 0 : 1));
  307. this->flipinfo.flipper = not(this->flipinfo.flipper);
  308. }
  309. }
  310. ///////////////////////// Slowroll
  311. void BackgroundRingClass::init_slowroll(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  312. this->action = Slowroll;
  313. this->slowrollinfo.color1 = color1;
  314. this->slowrollinfo.color2 = color2;
  315. this->slowrollinfo.ms = ms * 1000;
  316. this->slowrollinfo.tickdelay = tickdelay * 1000;
  317. this->slowrollinfo.starttime = micros();
  318. this->slowrollinfo.lasttick = micros() - this->slowrollinfo.tickdelay;
  319. this->slowrollinfo.flipper = false;
  320. }
  321. void BackgroundRingClass::tick_slowroll() {
  322. if(micros() - this->slowrollinfo.starttime > this->slowrollinfo.ms) {
  323. // we're done
  324. Serial.println(micros() - this->slowrollinfo.starttime);
  325. this->init_idle();
  326. return;
  327. }
  328. if(micros() - this->slowrollinfo.lasttick > this->slowrollinfo.tickdelay) {
  329. // Time to slowroll
  330. this->slowrollinfo.lasttick = micros();
  331. unsigned long tickcolors[] = { this->slowrollinfo.color1, this->slowrollinfo.color2, BLACK };
  332. this->setRepeating(tickcolors, 3, (this->slowrollinfo.flipper ? 0 : 2));
  333. this->slowrollinfo.flipper = not(this->slowrollinfo.flipper);
  334. }
  335. }