simon.ino 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <GD2.h>
  4. void setup()
  5. {
  6. Serial.begin(1000000); // JCB
  7. GD.begin(~GD_STORAGE);
  8. }
  9. #define DARK_GREEN 0x007000 //' a{
  10. #define LIGHT_GREEN 0x33ff33
  11. #define DARK_RED 0x700000
  12. #define LIGHT_RED 0xff3333
  13. #define DARK_YELLOW 0x707000
  14. #define LIGHT_YELLOW 0xffff33
  15. #define DARK_BLUE 0x007070
  16. #define LIGHT_BLUE 0x33ffff //' }a
  17. void drawscreen(int pressed) //' b{
  18. {
  19. GD.get_inputs(); //' touch{
  20. GD.Clear(); //' }touch
  21. GD.PointSize(16 * 60); // 60-pixel radius points
  22. GD.Begin(POINTS);
  23. GD.Tag(1);
  24. if (pressed == 1)
  25. GD.ColorRGB(LIGHT_GREEN);
  26. else
  27. GD.ColorRGB(DARK_GREEN);
  28. GD.Vertex2ii(240 - 70, 136 - 70);
  29. GD.Tag(2);
  30. if (pressed == 2)
  31. GD.ColorRGB(LIGHT_RED);
  32. else
  33. GD.ColorRGB(DARK_RED);
  34. GD.Vertex2ii(240 + 70, 136 - 70);
  35. GD.Tag(3);
  36. if (pressed == 3)
  37. GD.ColorRGB(LIGHT_YELLOW);
  38. else
  39. GD.ColorRGB(DARK_YELLOW);
  40. GD.Vertex2ii(240 - 70, 136 + 70);
  41. GD.Tag(4);
  42. if (pressed == 4)
  43. GD.ColorRGB(LIGHT_BLUE);
  44. else
  45. GD.ColorRGB(DARK_BLUE);
  46. GD.Vertex2ii(240 + 70, 136 + 70);
  47. GD.swap();
  48. } //' }b
  49. void play(int pressed) //' play{
  50. {
  51. // G R Y B
  52. // E3 A4 C#4 E4
  53. byte note[] = { 0, 52, 69, 61, 64 };
  54. GD.play(BELL, note[pressed]);
  55. for (int i = 0; i < 30; i++)
  56. drawscreen(pressed);
  57. for (int i = 0; i < 15; i++)
  58. drawscreen(0);
  59. } //' }play
  60. static int get_note() //' getnote{
  61. {
  62. byte pressed = 0;
  63. while (pressed == 0) {
  64. GD.random();
  65. drawscreen(0);
  66. if ((1 <= GD.inputs.tag) && (GD.inputs.tag <= 4))
  67. pressed = GD.inputs.tag;
  68. #ifdef DUMPDEV // JCB{
  69. pressed = 1;
  70. #endif // }JCB
  71. }
  72. play(pressed);
  73. return pressed;
  74. } //' }getnote
  75. static int random_note()
  76. {
  77. return 1 + GD.random(4);
  78. }
  79. void loop() //' loop{
  80. {
  81. int sequence[100];
  82. int length = 0;
  83. while (1) {
  84. delay(500);
  85. sequence[length++] = random_note();
  86. for (int i = 0; i < length; i++)
  87. play(sequence[i]);
  88. for (int i = 0; i < length; i++) {
  89. int pressed = get_note();
  90. if (pressed != sequence[i]) {
  91. for (int i = 69; i > 49; i--) {
  92. GD.play(BELL, i);
  93. delay(50);
  94. }
  95. return;
  96. }
  97. }
  98. }
  99. } //' }loop