PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 line
3.1KB

  1. // Die - A class to handle the display of a six sided die, using
  2. // seven light emitting diodes
  3. //
  4. // Copyright 2012 by Walter Anderson
  5. //
  6. // This file is part of Entropy, an Arduino library.
  7. // Entropy is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // Entropy is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with Entropy. If not, see <http://www.gnu.org/licenses/>.
  19. #include <Arduino.h>
  20. #include "Die.h"
  21. // Initializes the class with pin numbers to use as outputs for the
  22. // seven light emitting diodes use to display a die. The diodes are
  23. // arranged as:
  24. // a b
  25. // c d e
  26. // f g
  27. void Die::Initialize(int a, int b, int c, int d, int e, int f, int g)
  28. {
  29. // Store pin assignments
  30. led_a = a;
  31. led_b = b;
  32. led_c = c;
  33. led_d = d;
  34. led_e = e;
  35. led_f = f;
  36. led_g = g;
  37. // Configure pin modes
  38. pinMode(led_a, OUTPUT);
  39. digitalWrite(led_a, LOW);
  40. pinMode(led_b, OUTPUT);
  41. digitalWrite(led_b, LOW);
  42. pinMode(led_c, OUTPUT);
  43. digitalWrite(led_c, LOW);
  44. pinMode(led_d, OUTPUT);
  45. digitalWrite(led_d, LOW);
  46. pinMode(led_e, OUTPUT);
  47. digitalWrite(led_e, LOW);
  48. pinMode(led_f, OUTPUT);
  49. digitalWrite(led_f, LOW);
  50. pinMode(led_g, OUTPUT);
  51. digitalWrite(led_g, LOW);
  52. }
  53. // Turn on the appropriate LED's based upon value
  54. void Die::Show(unsigned char value)
  55. {
  56. Off();
  57. delay(200);
  58. switch (value)
  59. {
  60. case 1:
  61. digitalWrite(led_d, HIGH);
  62. break;
  63. case 2:
  64. digitalWrite(led_b, HIGH);
  65. digitalWrite(led_f, HIGH);
  66. break;
  67. case 3:
  68. digitalWrite(led_b, HIGH);
  69. digitalWrite(led_d, HIGH);
  70. digitalWrite(led_f, HIGH);
  71. break;
  72. case 4:
  73. digitalWrite(led_a, HIGH);
  74. digitalWrite(led_b, HIGH);
  75. digitalWrite(led_f, HIGH);
  76. digitalWrite(led_g, HIGH);
  77. break;
  78. case 5:
  79. digitalWrite(led_a, HIGH);
  80. digitalWrite(led_b, HIGH);
  81. digitalWrite(led_d, HIGH);
  82. digitalWrite(led_f, HIGH);
  83. digitalWrite(led_g, HIGH);
  84. break;
  85. case 6:
  86. digitalWrite(led_a, HIGH);
  87. digitalWrite(led_b, HIGH);
  88. digitalWrite(led_c, HIGH);
  89. digitalWrite(led_e, HIGH);
  90. digitalWrite(led_f, HIGH);
  91. digitalWrite(led_g, HIGH);
  92. break;
  93. default:
  94. Error();
  95. }
  96. }
  97. void Die::On(void)
  98. {
  99. digitalWrite(led_a, HIGH);
  100. digitalWrite(led_b, HIGH);
  101. digitalWrite(led_c, HIGH);
  102. digitalWrite(led_d, HIGH);
  103. digitalWrite(led_e, HIGH);
  104. digitalWrite(led_f, HIGH);
  105. digitalWrite(led_g, HIGH);
  106. }
  107. void Die::Off(void)
  108. {
  109. digitalWrite(led_a, LOW);
  110. digitalWrite(led_b, LOW);
  111. digitalWrite(led_c, LOW);
  112. digitalWrite(led_d, LOW);
  113. digitalWrite(led_e, LOW);
  114. digitalWrite(led_f, LOW);
  115. digitalWrite(led_g, LOW);
  116. }
  117. void Die::Error(void)
  118. {
  119. for (int i=0; i<10; i++)
  120. {
  121. delay(50);
  122. On();
  123. delay(50);
  124. Off();
  125. }
  126. }