Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

114 righe
2.8 KiB

  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://projects.drogon.net/raspberry-pi/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. }