animation.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "mbed.h"
  2. #include "main.h"
  3. #include "pca9685.h"
  4. #include "animation.h"
  5. void Animate_Cycle_Basic(int iterations) {
  6. PCA9685_SetVoltage(1.0);
  7. // Switch each digit from 0 to 100%
  8. for (int i = 0; i < iterations; i++) {
  9. if (i % 2 == 0) {
  10. PCA9685_SetDot(PCA9685_Min_Brightness);
  11. } else {
  12. PCA9685_SetDot(PCA9685_Max_Brightness);
  13. }
  14. for (int j = 0; j < 4; j++) {
  15. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness);
  16. }
  17. wait(0.5);
  18. for (int j = 0; j < 4; j++) {
  19. PCA9685_SetDigit(j, i, PCA9685_Min_Brightness);
  20. }
  21. }
  22. }
  23. void Animate_Cycle_Analog(int iterations) {
  24. for (int i = 0; i < iterations; i++) {
  25. PCA9685_EnableOutput(false);
  26. float minVoltage = 0.10;
  27. for (int j = 0; j < 4; j++) {
  28. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness);
  29. PCA9685_SetDot(PCA9685_Max_Brightness);
  30. }
  31. // Slowly drop the voltage to the minimum level
  32. for (double k = 1; k >= minVoltage; k -= 0.001) {
  33. PCA9685_SetVoltage(k);
  34. wait(0.001);
  35. }
  36. PCA9685_EnableOutput(true);
  37. for (double k = minVoltage; k <= 1; k += 0.0001) {
  38. PCA9685_SetVoltage(k);
  39. wait(0.0002);
  40. }
  41. for (double k = 1; k >= minVoltage; k -= 0.0001) {
  42. PCA9685_SetVoltage(k);
  43. wait(0.0002);
  44. }
  45. PCA9685_EnableOutput(false);
  46. // Slowly raise the voltage to the maximum level
  47. for (double k = minVoltage; k <= 1; k += 0.01) {
  48. PCA9685_SetVoltage(k);
  49. wait(0.001);
  50. }
  51. for (int j = 0; j < 4; j++) {
  52. PCA9685_SetDigit(j, i, PCA9685_Min_Brightness);
  53. PCA9685_SetDot(PCA9685_Min_Brightness);
  54. }
  55. }
  56. PCA9685_EnableOutput(true);
  57. }
  58. void Animate_Cycle_Low_Pwm(int iterations) {
  59. PCA9685_SetVoltage(1.0);
  60. PCA9685_SetDot(PCA9685_Min_Brightness);
  61. int pwmMaxBrightness = 300;
  62. for (int i = 0; i < iterations; i++) {
  63. for (int k = PCA9685_Min_Brightness; k <= pwmMaxBrightness; k++) {
  64. for (int j = 0; j < 4; j++) {
  65. PCA9685_SetDigit(j, i, k);
  66. PCA9685_SetDot(k);
  67. }
  68. wait(0.005);
  69. }
  70. for (int k = PCA9685_Min_Brightness; k <= pwmMaxBrightness; k++) {
  71. for (int j = 0; j < 4; j++) {
  72. PCA9685_SetDigit(j, i, pwmMaxBrightness-k);
  73. PCA9685_SetDot(pwmMaxBrightness-k);;
  74. }
  75. wait(0.005);
  76. }
  77. wait(0.2);
  78. }
  79. }
  80. void Animate_Cycle_Pwm(int iterations) {
  81. PCA9685_SetVoltage(1.0);
  82. PCA9685_SetDot(PCA9685_Min_Brightness);
  83. for (int i = 0; i < iterations; i++) {
  84. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  85. for (int j = 0; j < 4; j++) {
  86. PCA9685_SetDigit(j, i, k);
  87. PCA9685_SetDot(k);
  88. }
  89. wait(0.00001);
  90. }
  91. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  92. for (int j = 0; j < 4; j++) {
  93. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness-k);
  94. PCA9685_SetDot(PCA9685_Max_Brightness-k);;
  95. }
  96. wait(0.00001);
  97. }
  98. wait(0.2);
  99. }
  100. }
  101. void Animate_Cycle_Fade(int iterations) {
  102. PCA9685_SetVoltage(1.0);
  103. PCA9685_SetDot(PCA9685_Min_Brightness);
  104. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  105. for (int j = 0; j < 4; j++) {
  106. PCA9685_SetDigit(j, 0, k);
  107. }
  108. wait(0.00001);
  109. }
  110. for (int i = 0; i < iterations; i++) {
  111. int i_next = (i == 9) ? 0 : i+1;
  112. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  113. for (int j = 0; j < 4; j++) {
  114. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness-k);
  115. PCA9685_SetDigit(j, i_next, k);
  116. }
  117. if (i % 2 == 0) {
  118. PCA9685_SetDot(k);
  119. } else {
  120. PCA9685_SetDot(PCA9685_Max_Brightness-k);
  121. }
  122. wait(0.00005);
  123. }
  124. wait(0.2);
  125. }
  126. for (int i = 0; i < 4; i++) {
  127. for (int j = 0; j < 10; j++) {
  128. PCA9685_SetDigit(i, j, PCA9685_Min_Brightness);
  129. }
  130. }
  131. }
  132. void Animate_Cycle_Fade_Random(int iterations) {
  133. int i_curr[4];
  134. int i_next[4];
  135. PCA9685_SetVoltage(1.0);
  136. PCA9685_SetDot(PCA9685_Min_Brightness);
  137. for (int i = 0; i < 4; i++) {
  138. i_curr[i] = rand() % 10;
  139. }
  140. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  141. for (int j = 0; j < 4; j++) {
  142. PCA9685_SetDigit(j, i_curr[j], k);
  143. }
  144. wait(0.00001);
  145. }
  146. for (int iter = 0; iter < iterations; iter++) {
  147. for (int i = 0; i < 4; i++) {
  148. do {
  149. i_next[i] = rand() % 10;
  150. } while (i_next[i] == i_curr[i]);
  151. }
  152. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  153. for (int j = 0; j < 4; j++) {
  154. PCA9685_SetDigit(j, i_curr[j], PCA9685_Max_Brightness-k);
  155. PCA9685_SetDigit(j, i_next[j], k);
  156. }
  157. if (iter % 2 == 0) {
  158. PCA9685_SetDot(k);
  159. } else {
  160. PCA9685_SetDot(PCA9685_Max_Brightness-k);
  161. }
  162. wait(0.00001);
  163. }
  164. wait(0.2);
  165. for (int i = 0; i < 4; i++) {
  166. i_curr[i] = i_next[i];
  167. }
  168. }
  169. for (int k = PCA9685_Max_Brightness; k >= PCA9685_Min_Brightness; k -= 4) {
  170. for (int j = 0; j < 4; j++) {
  171. PCA9685_SetDigit(j, i_curr[j], k);
  172. }
  173. wait(0.00001);
  174. }
  175. }
  176. void Animate_Cycle_Fast(int iterations) {
  177. PCA9685_SetVoltage(1.0);
  178. PCA9685_SetDot(PCA9685_Min_Brightness);
  179. for (int k = 0; k < iterations; k++) {
  180. for (int i = 0; i < 10; i++) {
  181. for (int j = 0; j < 4; j++) {
  182. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness);
  183. }
  184. if (i % 2 == 0) {
  185. PCA9685_SetDot(PCA9685_Max_Brightness);
  186. } else {
  187. PCA9685_SetDot(PCA9685_Min_Brightness);
  188. }
  189. wait(0.1);
  190. for (int j = 0; j < 4; j++) {
  191. PCA9685_SetDigit(j, i, PCA9685_Min_Brightness);
  192. }
  193. }
  194. }
  195. }
  196. void Animate_Cycle_Fast_Random(int iterations) {
  197. int i_curr[4];
  198. int i_next[4];
  199. PCA9685_SetVoltage(1.0);
  200. PCA9685_SetDot(PCA9685_Min_Brightness);
  201. for (int i = 0; i < 4; i++) {
  202. i_curr[i] = rand() % 10;
  203. }
  204. for (int k = 0; k < iterations; k++) {
  205. for (int i = 0; i < 4; i++) {
  206. do {
  207. i_next[i] = rand() % 10;
  208. } while (i_next[i] == i_curr[i]);
  209. }
  210. for (int j = 0; j < 4; j++) {
  211. PCA9685_SetDigit(j, i_next[j], PCA9685_Max_Brightness);
  212. }
  213. if (k % 2 == 0) {
  214. PCA9685_SetDot(PCA9685_Max_Brightness);
  215. } else {
  216. PCA9685_SetDot(PCA9685_Min_Brightness);
  217. }
  218. wait(0.1);
  219. for (int j = 0; j < 4; j++) {
  220. PCA9685_SetDigit(j, i_next[j], PCA9685_Min_Brightness);
  221. }
  222. for (int i = 0; i < 4; i++) {
  223. i_curr[i] = i_next[i];
  224. }
  225. }
  226. }