BackgroundRingClass.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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->setColor(color, brightness, true);
  78. }
  79. void BackgroundRingClass::tick_solid() {
  80. // noop. Here for consistency
  81. }
  82. ///////////////////////// Pulse
  83. void BackgroundRingClass::init_pulse(unsigned long color, uint8_t maxbrightness, unsigned long ms) {
  84. Serial.println("Initializing pulse");
  85. this->action = Pulse;
  86. this->pulseinfo.color = color;
  87. this->pulseinfo.maxbrightness = maxbrightness;
  88. this->pulseinfo.ms = ms * 1000;
  89. this->pulseinfo.starttime = micros();
  90. this->pulseinfo.curbrightness = 1;
  91. this->pulseinfo.steptime = (ms * 1000) / (2 * (unsigned long)maxbrightness);
  92. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true); // dimmest setting
  93. }
  94. void BackgroundRingClass::tick_pulse() {
  95. unsigned long elapsed = micros() - this->pulseinfo.starttime;
  96. int newbrightness;
  97. if(elapsed < this->pulseinfo.ms/2) {
  98. // increasing
  99. newbrightness = (elapsed / this->pulseinfo.steptime) + 1;
  100. //Serial.print("Increasing. elapsed="); Serial.print(elapsed);
  101. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  102. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  103. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  104. } else if(elapsed < this->pulseinfo.ms) {
  105. // decreasing
  106. newbrightness = ((this->pulseinfo.ms - elapsed) / this->pulseinfo.steptime) + 1;
  107. //Serial.print("Decreasing. elapsed="); Serial.print(elapsed);
  108. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  109. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  110. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  111. } else {
  112. // done
  113. //Serial.print("DONE. elapsed="); Serial.print(elapsed);
  114. this->init_idle();
  115. return;
  116. }
  117. if(newbrightness != this->pulseinfo.curbrightness) {
  118. this->pulseinfo.curbrightness = newbrightness;
  119. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  120. this->show();
  121. }
  122. return;
  123. }
  124. ///////////////////////// Pulse and Beep
  125. //
  126. // We can't call show() while beeping because it disabled interrupts,
  127. // so we beep at the peak of the pulse.
  128. //
  129. void BackgroundRingClass::init_pulse_and_beep(
  130. unsigned long color,
  131. uint8_t maxbrightness,
  132. unsigned long ms,
  133. int pin,
  134. unsigned int frequency,
  135. unsigned long duration
  136. ) {
  137. Serial.println("Initializing pulse and beep");
  138. this->action = PulseAndBeep;
  139. this->pulseinfo.color = color;
  140. this->pulseinfo.maxbrightness = maxbrightness;
  141. this->pulseinfo.ms = ms * 1000;
  142. this->pulseinfo.starttime = micros();
  143. this->pulseinfo.curbrightness = 1;
  144. this->pulseinfo.steptime = ( (ms - duration) * 1000) / ((2 * (unsigned long)maxbrightness) - 1);
  145. this->beepinfo.pin = pin;
  146. this->beepinfo.frequency = frequency;
  147. this->beepinfo.duration = duration;
  148. this->beepinfo.state = Beeping;
  149. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true); // dimmest setting
  150. }
  151. void BackgroundRingClass::tick_pulse_and_beep() {
  152. // What we're doing should be based on time.
  153. // Increasing: 0 to (ms - duration)/2))
  154. // Beeping: (ms - duration)/2 to (ms - duration/2)+duration
  155. // Decreasing: (ms - duration/2)+duration - ms
  156. unsigned long elapsed = micros() - this->pulseinfo.starttime;
  157. int newbrightness;
  158. if(elapsed < (this->pulseinfo.ms/2 - 1000*this->beepinfo.duration/2)) {
  159. // increasing
  160. this->beepinfo.state = Increasing;
  161. newbrightness = (elapsed / this->pulseinfo.steptime) + 1;
  162. //Serial.print("Increasing. elapsed="); Serial.print(elapsed);
  163. //Serial.print("; ms="); Serial.print(this->pulseinfo.ms);
  164. //Serial.print("; duration="); Serial.print(this->beepinfo.duration);
  165. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  166. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  167. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  168. } else if(elapsed < (this->pulseinfo.ms/2 + 1000*this->beepinfo.duration/2)) {
  169. if(this->beepinfo.state != Beeping && this->beepinfo.frequency != 0) {
  170. tone(this->beepinfo.pin, this->beepinfo.frequency, this->beepinfo.duration);
  171. this->beepinfo.state = Beeping;
  172. }
  173. //Serial.print("Beeping. elapsed="); Serial.print(elapsed);
  174. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  175. //Serial.print("; curbrightness="); Serial.println(this->pulseinfo.curbrightness);
  176. return;
  177. } else if(elapsed < this->pulseinfo.ms) {
  178. // decreasing
  179. this->beepinfo.state = Decreasing;
  180. //newbrightness = ((this->pulseinfo.ms - this->beepinfo.duration/2) / this->pulseinfo.steptime) + 1;
  181. newbrightness = ((this->pulseinfo.ms - elapsed) / this->pulseinfo.steptime) + 1;
  182. //Serial.print("Decreasing. elapsed="); Serial.print(elapsed);
  183. //Serial.print("; ms="); Serial.print(this->pulseinfo.ms);
  184. //Serial.print("; duration="); Serial.print(this->beepinfo.duration);
  185. //Serial.print("; steptime="); Serial.print(this->pulseinfo.steptime);
  186. //Serial.print("; curbrightness="); Serial.print(this->pulseinfo.curbrightness);
  187. //Serial.print("; newbrightenss="); Serial.println(newbrightness);
  188. } else {
  189. // done
  190. //Serial.print("DONE. elapsed="); Serial.print(elapsed);
  191. this->init_idle();
  192. return;
  193. }
  194. if(newbrightness != this->pulseinfo.curbrightness) {
  195. this->pulseinfo.curbrightness = newbrightness;
  196. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  197. this->show();
  198. }
  199. return;
  200. }
  201. /*
  202. void BackgroundRingClass::tick_pulse_and_beep() {
  203. int stepspassed = 0;
  204. if(this->beepinfo.state == Beeping && (micros() - this->beepinfo.beepstart < 1000*this->beepinfo.duration)) {
  205. return;
  206. } else if(this->beepinfo.state == Beeping) {
  207. Serial.print("Beep Duration was: "); Serial.println((micros() - this->beepinfo.beepstart)/1000);
  208. Serial.print("Stopping beeping at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  209. this->beepinfo.state = Decreasing;
  210. this->pulseinfo.starttime = micros() + this->pulseinfo.steptime;
  211. }
  212. if((micros() - this->pulseinfo.starttime) < this->pulseinfo.steptime) {
  213. return;
  214. } else {
  215. stepspassed = (micros() - this->pulseinfo.starttime)/this->pulseinfo.steptime;
  216. }
  217. //Serial.print("Taking action at t="); Serial.print(micros() - this->beepinfo.truestart);
  218. //Serial.print("; brightness="); Serial.print(this->pulseinfo.curbrightness);
  219. //Serial.print("; Steps to take="); Serial.println(stepspassed);
  220. this->pulseinfo.starttime = micros();
  221. if(this->beepinfo.state == Increasing && this->pulseinfo.curbrightness == this->pulseinfo.maxbrightness) {
  222. this->beepinfo.state = Beeping;
  223. this->beepinfo.beepstart = micros();
  224. if(this->beepinfo.frequency != 0) {
  225. Serial.print("Beginning beeping at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  226. tone(this->beepinfo.pin, this->beepinfo.frequency, this->beepinfo.duration);
  227. return;
  228. }
  229. }
  230. if(this->beepinfo.state == Increasing) {
  231. if(this->pulseinfo.curbrightness < this->pulseinfo.maxbrightness - stepspassed) {
  232. this->pulseinfo.curbrightness += stepspassed;
  233. } else {
  234. this->pulseinfo.curbrightness = this->pulseinfo.maxbrightness;
  235. }
  236. } else {
  237. if(this->pulseinfo.curbrightness > stepspassed) {
  238. this->pulseinfo.curbrightness -= stepspassed;
  239. } else {
  240. this->pulseinfo.curbrightness = 0;
  241. }
  242. }
  243. if(this->pulseinfo.curbrightness == 0) {
  244. // We are done
  245. Serial.print("Stopping pulse at t="); Serial.println((micros() - this->beepinfo.truestart)/1000);
  246. this->init_idle();
  247. } else {
  248. //Serial.print("Setting Brightness: "); Serial.println(this->pulseinfo.curbrightness);
  249. this->setColor(this->pulseinfo.color, this->pulseinfo.curbrightness, true);
  250. this->show();
  251. }
  252. }
  253. */
  254. ///////////////////////// Spin
  255. void BackgroundRingClass::init_spin(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  256. this->action = Spin;
  257. this->spininfo.color1 = color1;
  258. this->spininfo.color2 = color2;
  259. this->spininfo.ms = ms * 1000;
  260. this->spininfo.tickdelay = tickdelay * 1000;
  261. this->spininfo.starttime = micros();
  262. this->spininfo.lasttick = micros() - this->spininfo.tickdelay;
  263. this->spininfo.rotation = 0;
  264. }
  265. void BackgroundRingClass::tick_spin() {
  266. if(micros() - this->spininfo.starttime > this->spininfo.ms) {
  267. // we're done
  268. Serial.println(micros() - this->spininfo.starttime);
  269. this->init_idle();
  270. return;
  271. }
  272. if(micros() - this->spininfo.lasttick > this->spininfo.tickdelay) {
  273. // Time to spin
  274. this->spininfo.lasttick = micros();
  275. this->spininfo.rotation = (this->spininfo.rotation + 1) % 3;
  276. //unsigned long tickcolors[] = { this->spininfo.color1, this->spininfo.color1, this->spininfo.color1, this->spininfo.color2 };
  277. //unsigned long tickcolors[] = { BLACK, this->spininfo.color1, this->spininfo.color2 };
  278. unsigned long tickcolors[] = { BLACK, this->spininfo.color1, this->spininfo.color1, this->spininfo.color2 };
  279. this->setRepeating(tickcolors, 4, this->spininfo.rotation);
  280. }
  281. }
  282. ///////////////////////// Flip
  283. void BackgroundRingClass::init_flip(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  284. this->action = Flip;
  285. this->flipinfo.color1 = color1;
  286. this->flipinfo.color2 = color2;
  287. this->flipinfo.ms = ms * 1000;
  288. this->flipinfo.tickdelay = tickdelay * 1000;
  289. this->flipinfo.starttime = micros();
  290. this->flipinfo.lasttick = micros() - this->flipinfo.tickdelay;
  291. this->flipinfo.flipper = false;
  292. }
  293. void BackgroundRingClass::tick_flip() {
  294. if(micros() - this->flipinfo.starttime > this->flipinfo.ms) {
  295. // we're done
  296. Serial.println(micros() - this->flipinfo.starttime);
  297. this->init_idle();
  298. return;
  299. }
  300. if(micros() - this->flipinfo.lasttick > this->flipinfo.tickdelay) {
  301. // Time to flip
  302. this->flipinfo.lasttick = micros();
  303. unsigned long tickcolors[] = { this->flipinfo.color1, this->flipinfo.color2 };
  304. this->setRepeating(tickcolors, 2, (this->flipinfo.flipper ? 0 : 1));
  305. this->flipinfo.flipper = not(this->flipinfo.flipper);
  306. }
  307. }
  308. ///////////////////////// Slowroll
  309. void BackgroundRingClass::init_slowroll(unsigned long color1, unsigned long color2, unsigned long ms, unsigned long tickdelay) {
  310. this->action = Slowroll;
  311. this->slowrollinfo.color1 = color1;
  312. this->slowrollinfo.color2 = color2;
  313. this->slowrollinfo.ms = ms * 1000;
  314. this->slowrollinfo.tickdelay = tickdelay * 1000;
  315. this->slowrollinfo.starttime = micros();
  316. this->slowrollinfo.lasttick = micros() - this->slowrollinfo.tickdelay;
  317. this->slowrollinfo.flipper = false;
  318. }
  319. void BackgroundRingClass::tick_slowroll() {
  320. if(micros() - this->slowrollinfo.starttime > this->slowrollinfo.ms) {
  321. // we're done
  322. Serial.println(micros() - this->slowrollinfo.starttime);
  323. this->init_idle();
  324. return;
  325. }
  326. if(micros() - this->slowrollinfo.lasttick > this->slowrollinfo.tickdelay) {
  327. // Time to slowroll
  328. this->slowrollinfo.lasttick = micros();
  329. unsigned long tickcolors[] = { this->slowrollinfo.color1, this->slowrollinfo.color2, BLACK };
  330. this->setRepeating(tickcolors, 3, (this->slowrollinfo.flipper ? 0 : 2));
  331. this->slowrollinfo.flipper = not(this->slowrollinfo.flipper);
  332. }
  333. }