Advertisement
Guest User

catch

a guest
Oct 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include "system.h"
  2. #include "pacer.h"
  3. #include "pio.h"
  4. #include "navswitch.h"
  5. #include "ir_serial.h"
  6.  
  7. //Define pace in Hz
  8. #define PACER_RATE 1000
  9.  
  10.  
  11. //Define PIO pins and LED Mat rows and columns
  12. static pio_t ledmat_rows[] = {
  13. LEDMAT_ROW1_PIO, LEDMAT_ROW2_PIO, LEDMAT_ROW3_PIO, LEDMAT_ROW4_PIO,
  14. LEDMAT_ROW5_PIO, LEDMAT_ROW6_PIO, LEDMAT_ROW7_PIO
  15. };
  16. static pio_t ledmat_cols[] = {
  17. LEDMAT_COL1_PIO, LEDMAT_COL2_PIO, LEDMAT_COL3_PIO,
  18. LEDMAT_COL4_PIO, LEDMAT_COL5_PIO
  19. };
  20.  
  21. static void ledmat_pixel_set (int col, int row, bool state)
  22. {
  23. if (state) {
  24. pio_output_low (ledmat_rows[row]);
  25. pio_output_low (ledmat_cols[col]);
  26. } else {
  27. pio_output_high (ledmat_rows[row]);
  28. pio_output_high (ledmat_cols[col]);
  29. }
  30. }
  31.  
  32. static void ledmat_init (void)
  33. {
  34. uint8_t row;
  35. uint8_t col;
  36.  
  37. //initialise each row
  38. for (row = 0; row < 7; row++) {
  39. pio_config_set (ledmat_rows[row], PIO_OUTPUT_HIGH);
  40. pio_output_high (ledmat_rows[row]);
  41. }
  42.  
  43. //initialise each column
  44. for (col = 0; col < 5; col++) {
  45. pio_config_set (ledmat_cols[col], PIO_OUTPUT_HIGH);
  46. pio_output_high (ledmat_cols[col]);
  47. }
  48. }
  49.  
  50.  
  51. uint8_t ballPos(void)
  52. {
  53. ir_serial_init();
  54. uint8_t data;
  55. while(1) {
  56. pacer_wait();
  57. ir_serial_ret_t ret;
  58. ret = ir_serial_receive (&data);
  59. if (ret == IR_SERIAL_OK) {
  60. return (6 - data);
  61. }
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. void catch (uint8_t playerPosition)
  69. {
  70. pacer_init(PACER_RATE);
  71. ledmat_init();
  72.  
  73. uint8_t row = playerPosition;
  74. int col = 0; //was 4
  75. //ledmat_pixel_set (4, playerPosition, 1);
  76. uint64_t tick = 0;
  77. while (1) {
  78. pacer_wait ();
  79. ledmat_pixel_set (col, row, 0);
  80. //ledmat_pixel_set (0, playerPosition, 1); //col input was 4
  81. //was decrement the column
  82. col++;
  83. while(1) {
  84. pacer_wait();
  85. ledmat_pixel_set (col, row, 1);
  86. tick++;
  87. //keep the led on for some time using ticks
  88. if (tick >= 50) {
  89. tick = 0;
  90. break;
  91. }
  92. }
  93. //in the case that col = 0, hold it for some time using ticks
  94. //then turn it off and return to main
  95. if (col == 4) {
  96. while(1) {
  97. pacer_wait();
  98. ledmat_pixel_set (col, row, 1);
  99. tick++;
  100. if (tick >= 50) {
  101. tick = 0;
  102. break;
  103. }
  104. }
  105. ledmat_pixel_set (col, row, 0);
  106. navswitch_update();
  107. return;
  108. }
  109. }
  110. }
  111.  
  112. int main (void)
  113. {
  114. catch(2);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement