Justin

myopic

Aug 13th, 2017
163
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.25 KB | None | 0 0
  1. // myopic: A simple chess playing program for small systems
  2. //
  3. // Copyright (C) 2010 by [email protected]   (Some rights reserved)
  4. //
  5. // License: Creative Commons Attribution-Share Alike 3.0 License
  6. // See: http://creativecommons.org/licenses/by-sa/3.0/
  7. //
  8. // Caution: No warranty; use at your own risk.
  9.  
  10. // Arduino include files
  11.  
  12. #include <avr/interrupt.h>
  13. #include <avr/pgmspace.h>
  14.  
  15. // The Myopic source code file preprocessor include directives
  16.  
  17. #include "Source/BB.cpp"
  18. #include "Source/BBMoveNode.cpp"
  19. #include "Source/BBPosNode.cpp"
  20. #include "Source/Board.cpp"
  21. #include "Source/Book.cpp"
  22. #include "Source/BookMove.cpp"
  23. #include "Source/CIB.cpp"
  24. #include "Source/CNPair.cpp"
  25. #include "Source/Constants.cpp"
  26. #include "Source/Counter.cpp"
  27. #include "Source/FEnv.cpp"
  28. #include "Source/FPos.cpp"
  29. #include "Source/Hash.cpp"
  30. #include "Source/History.cpp"
  31. #include "Source/ICP.cpp"
  32. #include "Source/ICPTest.cpp"
  33. #include "Source/ICPCommands.cpp"
  34. #include "Source/MGS.cpp"
  35. #include "Source/ML.cpp"
  36. #include "Source/Move.cpp"
  37. #include "Source/Myopic.cpp"
  38. #include "Source/PEnv.cpp"
  39. #include "Source/PIR.cpp"
  40. #include "Source/PVTable.cpp"
  41. #include "Source/Pos.cpp"
  42. #include "Source/PosCountMoves.cpp"
  43. #include "Source/PosEvaluate.cpp"
  44. #include "Source/PosGenerate.cpp"
  45. #include "Source/PosMotion.cpp"
  46. #include "Source/PosNoMoves.cpp"
  47. #include "Source/SAN.cpp"
  48. #include "Source/SS.cpp"
  49. #include "Source/SSKing.cpp"
  50. #include "Source/SSKnight.cpp"
  51. #include "Source/Score.cpp"
  52. #include "Source/Search.cpp"
  53. #include "Source/SearchAB.cpp"
  54. #include "Source/SearchMP.cpp"
  55. #include "Source/State.cpp"
  56. #include "Source/TBV.cpp"
  57. #include "Source/TinyMove.cpp"
  58. #include "Source/UnDo.cpp"
  59. #include "Source/UnDoStack.cpp"
  60. #include "Source/Utilities.cpp"
  61. #include "Source/Window.cpp"
  62. #include "Source/XCP.cpp"
  63.  
  64. // Configuration constants for optional hardware
  65.  
  66. static const bool AllowActLED    = true; // Set to allow activity LED output
  67. static const bool AllowAudible   = true; // Set to allow piezo buzzer audible alert
  68. static const bool AllowBackpack  = true; // Set to allow SparkFun RGB LED backpack (needs SPI)
  69. static const bool AllowInterrupt = true; // Set to allow user interrupt zero
  70. static const bool AllowSpi       = true; // Set to allow SPI usage
  71.  
  72. // The application control stage indicator
  73.  
  74. static acsType acs = acsInit;
  75.  
  76. // The application object single instance
  77.  
  78. static Myopic myopic;
  79.  
  80. // LED color assignments for chessmen (Don't use LC(1, 1, 1) under any circumstances!)
  81.  
  82. #define LC(r, g, b) (((r) << 5) | ((g) << 2) | (b))  // Bits: red[765], green[432], blue[10]
  83.  
  84. static const unsigned char ManToLEDColor[manLen] =
  85. {
  86.   LC(0, 1, 3), LC(0, 2, 3), LC(0, 4, 3), LC(0, 5, 2), LC(0, 6, 1), LC(0, 7, 0),  // White PNBRQK
  87.   LC(1, 0, 3), LC(2, 0, 3), LC(4, 0, 3), LC(5, 0, 2), LC(6, 0, 1), LC(7, 0, 0),  // Black PNBRQK
  88.   LC(0, 0, 0), LC(0, 0, 0)  // Vacant and Extra
  89. };
  90.  
  91. // Arduino interrupt number for pushbutton interrupt
  92.  
  93. static const unsigned int userInterrupt = 0;  // Pushbutton interrupt on Mega pin 2
  94.  
  95. // Arduino Mega pin number for piezo buzzer output
  96.  
  97. static const unsigned int pinPiezoBuzzer = 12;  // Optional piezo buzzer
  98.  
  99. // Arduino Mega pin number for activity LED output
  100.  
  101. static const unsigned int pinActivityLED = 13;  // Standard single LED
  102.  
  103. // Arduino Mega pin numbers for SPI communication
  104.  
  105. static const unsigned int pinSpiMISO = 50;  // Master In/Slave Out (not used with SparkFun 8x8 RGB LED backpack)
  106. static const unsigned int pinSpiMOSI = 51;  // Master Out/Slave In
  107. static const unsigned int pinSpiSCK  = 52;  // SPI Clock
  108. static const unsigned int pinSpiSS   = 53;  // Slave Select (selects SparkFun 8x8 RGB LED backpack)
  109.  
  110. // User interrupt routine
  111.  
  112. static void Interrupt(void) {myopic.Interrupt();}
  113.  
  114. // Low level SPI communication routines
  115.  
  116. static void InitializeSpi(void)
  117. {
  118.   // Initialize the Arduino SPI pin directions
  119.  
  120.   pinMode(pinSpiMISO, INPUT);
  121.   pinMode(pinSpiMOSI, OUTPUT);
  122.   pinMode(pinSpiSCK,  OUTPUT);
  123.   pinMode(pinSpiSS,   OUTPUT);
  124.  
  125.   // Initialization of the AVR SPI control register
  126.  
  127.   SPCR =
  128.     (0 << SPIE) | // Disable SPI interrupt
  129.     (1 << SPE ) | // Enable SPI
  130.     (0 << DORD) | // Most significant bit of byte sent first
  131.     (1 << MSTR) | // AVR is SPI master
  132.     (0 << CPOL) | // Data clock idle when low
  133.     (0 << CPHA) | // Sample data on rising clock rising edge
  134.     (1 << SPR1) | // SPI clock is 1/64 AVR clock (first timing bit)
  135.     (0 << SPR0);  // SPI clock is 1/64 AVR clock (second timing bit)
  136.  
  137.   // Toss the updated values of the SPI status and data registers
  138.  
  139.   volatile byte discard;
  140.  
  141.   discard = SPSR; discard = SPDR;
  142. }
  143.  
  144. static unsigned char WriteSpiByte(unsigned char data)
  145. {
  146.   SPDR = data;
  147.   while (!(SPSR & (1 << SPIF)));
  148.   return SPDR;
  149. }
  150.  
  151. static void SetSpiSlaveSelectStatus(const unsigned int status) {digitalWrite(pinSpiSS, status);}
  152.  
  153. // SparkFun 8x8 RGB LED matrix display routines
  154.  
  155. static void SparkFunBackpackEnable(void)  {SetSpiSlaveSelectStatus(LOW ); delayMicroseconds(200);}
  156. static void SparkFunBackpackDisable(void) {SetSpiSlaveSelectStatus(HIGH);}
  157.  
  158. static void SparkFunBackpackSingleColor(const unsigned int ledcolor)
  159. {
  160.   const unsigned int edgeLen = 8;
  161.  
  162.   SparkFunBackpackEnable();
  163.   for (unsigned int row = 0; row < edgeLen; row++)
  164.     for (unsigned int col = 0; col < edgeLen; col++)
  165.       WriteSpiByte(ledcolor);
  166.   SparkFunBackpackDisable();
  167. }
  168.  
  169. static void SparkFunBackpackResetDisplay(void) {SparkFunBackpackSingleColor(0x00);}
  170.  
  171. // Arduino elapsed milliseconds (can be called externally)
  172.  
  173. unsigned long int TargetElapsedMsec(void) {return millis();}
  174.  
  175. // Arduino serial input status (can be called externally)
  176.  
  177. bool TargetSerialAvailable(void) {return Serial.available() > 0;}
  178.  
  179. // Arduino serial character input (can be called externally)
  180.  
  181. char TargetSerialInput(void) {return Serial.read();}
  182.  
  183. // Arduino serial character output (can be called externally)
  184.  
  185. void TargetSerialOutput(const char ch)
  186. {
  187.   if (ch != '\n') Serial.print(ch, BYTE); else Serial.println();
  188. }
  189.  
  190. // Arduino random seed (can be called externally)
  191.  
  192. void TargetRandomSeed(const unsigned int seed) {randomSeed(seed);}
  193.  
  194. // Arduino random pick (can be called externally)
  195.  
  196. long int TargetRandomPick(const unsigned int limit) {return random(limit);}
  197.  
  198. // Control the standard single LED output (can be called externally)
  199.  
  200. void TargetLEDOutput(const bool flag)
  201. {
  202.   if (AllowActLED) digitalWrite(pinActivityLED, (flag ? HIGH : LOW));
  203. }
  204.  
  205. // Issue a brief audible alert (can be called externally)
  206.  
  207. void TargetAudioAlert(void)
  208. {
  209.   if (AllowAudible)
  210.   {
  211.     for (unsigned int index = 0; index < 100; index++)
  212.     {
  213.       digitalWrite(pinPiezoBuzzer, HIGH);
  214.       delayMicroseconds(1000);
  215.       digitalWrite(pinPiezoBuzzer, LOW);
  216.       delayMicroseconds(1000);
  217.     };
  218.   };
  219. }
  220.  
  221. // Redraw the chessboard display (can be called externally)
  222.  
  223. void TargetUpdateBackpackBoardDisplay(const Board& board)
  224. {
  225.   if (AllowSpi && AllowBackpack)
  226.   {
  227.     SparkFunBackpackEnable();
  228.     for (rankType rank = 0; rank < rankLen; rank++)
  229.       for (fileType file = 0; file < fileLen; file++)
  230.         WriteSpiByte(ManToLEDColor[board.GetMan(OtherFile(file), OtherRank(rank))]);
  231.     SparkFunBackpackDisable();
  232.   };
  233. }
  234.  
  235. // Arduino system entry for one time initialization
  236.  
  237. void setup(void)
  238. {
  239.   // Initialize the only Arduino serial channel in use
  240.  
  241.   delay(2000); Serial.begin(9600);
  242.  
  243.   // Initialize the piezo buzzer
  244.  
  245.   if (AllowAudible) {pinMode(pinPiezoBuzzer, OUTPUT); digitalWrite(pinPiezoBuzzer, LOW);};
  246.  
  247.   // Initialize the activity LED
  248.  
  249.   if (AllowActLED) {pinMode(pinActivityLED, OUTPUT); TargetLEDOutput(false);};
  250.  
  251.   // Initialize the Serial Peripheral Interface
  252.  
  253.   if (AllowSpi) InitializeSpi();
  254.  
  255.   // Initialize the user interrupt
  256.  
  257.   if (AllowInterrupt) attachInterrupt(userInterrupt, Interrupt, RISING);
  258.  
  259.   // Set the SparkFun 8x8 RGB LED backpack to its default state
  260.  
  261.   if (AllowSpi && AllowBackpack)
  262.   {
  263.     SparkFunBackpackDisable();
  264.     SparkFunBackpackResetDisplay();
  265.   };
  266. }
  267.  
  268. // Arduino system entry for main loop
  269.  
  270. void loop(void)
  271. {
  272.   switch (acs)
  273.   {
  274.     case acsInit: myopic.Init(); acs = acsMain; break;
  275.     case acsMain: myopic.Main(); acs = acsTerm; break;
  276.     case acsTerm: myopic.Term(); acs = acsExit; break;
  277.     default: break;
  278.   };
  279. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment