25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * piNes.c:
  3. * Driver for the NES Joystick controller on the Raspberry Pi
  4. * Copyright (c) 2012 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://github.com/WiringPi/WiringPi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <wiringPi.h>
  25. #include "piNes.h"
  26. #define MAX_NES_JOYSTICKS 8
  27. #define NES_RIGHT 0x01
  28. #define NES_LEFT 0x02
  29. #define NES_DOWN 0x04
  30. #define NES_UP 0x08
  31. #define NES_START 0x10
  32. #define NES_SELECT 0x20
  33. #define NES_B 0x40
  34. #define NES_A 0x80
  35. #define PULSE_TIME 25
  36. // Data to store the pins for each controller
  37. struct nesPinsStruct
  38. {
  39. unsigned int cPin, dPin, lPin ;
  40. } ;
  41. static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ;
  42. static int joysticks = 0 ;
  43. /*
  44. * setupNesJoystick:
  45. * Create a new NES joystick interface, program the pins, etc.
  46. *********************************************************************************
  47. */
  48. int setupNesJoystick (int dPin, int cPin, int lPin)
  49. {
  50. if (joysticks == MAX_NES_JOYSTICKS)
  51. return -1 ;
  52. nesPins [joysticks].dPin = dPin ;
  53. nesPins [joysticks].cPin = cPin ;
  54. nesPins [joysticks].lPin = lPin ;
  55. digitalWrite (lPin, LOW) ;
  56. digitalWrite (cPin, LOW) ;
  57. pinMode (lPin, OUTPUT) ;
  58. pinMode (cPin, OUTPUT) ;
  59. pinMode (dPin, INPUT) ;
  60. return joysticks++ ;
  61. }
  62. /*
  63. * readNesJoystick:
  64. * Do a single scan of the NES Joystick.
  65. *********************************************************************************
  66. */
  67. unsigned int readNesJoystick (int joystick)
  68. {
  69. unsigned int value = 0 ;
  70. int i ;
  71. struct nesPinsStruct *pins = &nesPins [joystick] ;
  72. // Toggle Latch - which presents the first bit
  73. digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
  74. digitalWrite (pins->lPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
  75. // Read first bit
  76. value = digitalRead (pins->dPin) ;
  77. // Now get the next 7 bits with the clock
  78. for (i = 0 ; i < 7 ; ++i)
  79. {
  80. digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
  81. digitalWrite (pins->cPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
  82. value = (value << 1) | digitalRead (pins->dPin) ;
  83. }
  84. return value ^ 0xFF ;
  85. }