animation.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "mbed.h"
  2. #include "main.h"
  3. #include "pca9685.h"
  4. #include "animation.h"
  5. void Animate_Cycle_Basic(void) {
  6. PCA9685_SetVoltage(1.0);
  7. // Switch each digit from 0 to 100%
  8. for (int i = 0; i < 10; 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(void) {
  24. for (int i = 0; i < 10; i++) {
  25. PCA9685_EnableOutput(false);
  26. float minVoltage = 0.09;
  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(void) {
  59. PCA9685_SetVoltage(1.0);
  60. PCA9685_SetDot(PCA9685_Min_Brightness);
  61. int pwmMaxBrightness = 200;
  62. for (int i = 0; i < 10; 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.01);
  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.01);
  76. }
  77. wait(0.2);
  78. }
  79. }
  80. void Animate_Cycle_Pwm(void) {
  81. PCA9685_SetVoltage(1.0);
  82. PCA9685_SetDot(PCA9685_Min_Brightness);
  83. for (int i = 0; i < 10; 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(void) {
  102. PCA9685_SetVoltage(1.0);
  103. PCA9685_SetDot(PCA9685_Min_Brightness);
  104. for (int i = 0; i < 10; i++) {
  105. int i_next = (i == 9) ? 0 : i+1;
  106. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  107. for (int j = 0; j < 4; j++) {
  108. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness-k);
  109. PCA9685_SetDigit(j, i_next, k);
  110. }
  111. if (i % 2 == 0) {
  112. PCA9685_SetDot(k);
  113. } else {
  114. PCA9685_SetDot(PCA9685_Max_Brightness-k);
  115. }
  116. wait(0.00005);
  117. }
  118. wait(0.2);
  119. }
  120. for (int i = 0; i < 4; i++) {
  121. for (int j = 0; j < 10; j++) {
  122. PCA9685_SetDigit(i, j, PCA9685_Min_Brightness);
  123. }
  124. }
  125. }
  126. void Animate_Cycle_Fade_Random(void) {
  127. int i_curr[4];
  128. int i_next[4];
  129. PCA9685_SetVoltage(1.0);
  130. PCA9685_SetDot(PCA9685_Min_Brightness);
  131. for (int i = 0; i < 4; i++) {
  132. i_curr[i] = rand() % 10;
  133. }
  134. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  135. for (int j = 0; j < 4; j++) {
  136. PCA9685_SetDigit(j, i_curr[j], k);
  137. }
  138. wait(0.00005);
  139. }
  140. for (int iter = 0; iter < 10; iter++) {
  141. for (int i = 0; i < 4; i++) {
  142. do {
  143. i_next[i] = rand() % 10;
  144. } while (i_next[i] == i_curr[i]);
  145. }
  146. for (int k = PCA9685_Min_Brightness; k <= PCA9685_Max_Brightness; k += 4) {
  147. for (int j = 0; j < 4; j++) {
  148. PCA9685_SetDigit(j, i_curr[j], PCA9685_Max_Brightness-k);
  149. PCA9685_SetDigit(j, i_next[j], k);
  150. }
  151. if (iter % 2 == 0) {
  152. PCA9685_SetDot(k);
  153. } else {
  154. PCA9685_SetDot(PCA9685_Max_Brightness-k);
  155. }
  156. wait(0.00005);
  157. }
  158. wait(0.2);
  159. for (int i = 0; i < 4; i++) {
  160. i_curr[i] = i_next[i];
  161. }
  162. }
  163. for (int k = PCA9685_Max_Brightness; k >= PCA9685_Min_Brightness; k -= 4) {
  164. for (int j = 0; j < 4; j++) {
  165. PCA9685_SetDigit(j, i_curr[j], k);
  166. }
  167. wait(0.00005);
  168. }
  169. }
  170. void Animate_Cycle_Fast(void) {
  171. PCA9685_SetVoltage(1.0);
  172. PCA9685_SetDot(PCA9685_Min_Brightness);
  173. for (int k = 0; k < 10; k++) {
  174. for (int i = 0; i < 10; i++) {
  175. for (int j = 0; j < 4; j++) {
  176. PCA9685_SetDigit(j, i, PCA9685_Max_Brightness);
  177. }
  178. if (i % 2 == 0) {
  179. PCA9685_SetDot(PCA9685_Max_Brightness);
  180. } else {
  181. PCA9685_SetDot(PCA9685_Min_Brightness);
  182. }
  183. wait(0.1);
  184. for (int j = 0; j < 4; j++) {
  185. PCA9685_SetDigit(j, i, PCA9685_Min_Brightness);
  186. }
  187. }
  188. }
  189. }
  190. void Animate_Cycle_Fast_Random(void) {
  191. int i_curr[4];
  192. int i_next[4];
  193. PCA9685_SetVoltage(1.0);
  194. PCA9685_SetDot(PCA9685_Min_Brightness);
  195. for (int i = 0; i < 4; i++) {
  196. i_curr[i] = rand() % 10;
  197. }
  198. for (int k = 0; k < 100; k++) {
  199. for (int i = 0; i < 4; i++) {
  200. do {
  201. i_next[i] = rand() % 10;
  202. } while (i_next[i] == i_curr[i]);
  203. }
  204. for (int j = 0; j < 4; j++) {
  205. PCA9685_SetDigit(j, i_next[j], PCA9685_Max_Brightness);
  206. }
  207. if (k % 2 == 0) {
  208. PCA9685_SetDot(PCA9685_Max_Brightness);
  209. } else {
  210. PCA9685_SetDot(PCA9685_Min_Brightness);
  211. }
  212. wait(0.1);
  213. for (int j = 0; j < 4; j++) {
  214. PCA9685_SetDigit(j, i_next[j], PCA9685_Min_Brightness);
  215. }
  216. for (int i = 0; i < 4; i++) {
  217. i_curr[i] = i_next[i];
  218. }
  219. }
  220. }