Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. from lib_nrf24 import NRF24
  3. import spidev
  4. import pygame
  5. import time
  6. import array
  7.  
  8. # Define some colors
  9. BLACK = ( 0, 0, 0)
  10. WHITE = ( 255, 255, 255)
  11.  
  12. # This is a simple class that will help us print to the screen
  13. # It has nothing to do with the joysticks, just outputting the
  14. # information.
  15. class TextPrint:
  16. def __init__(self):
  17. self.reset()
  18. self.font = pygame.font.Font(None, 20)
  19.  
  20. def Print(self, screen, textString):
  21. textBitmap = self.font.render(textString, True, BLACK)
  22. screen.blit(textBitmap, [self.x, self.y])
  23. self.y += self.line_height
  24.  
  25. def reset(self):
  26. self.x = 10
  27. self.y = 10
  28. self.line_height = 15
  29.  
  30. def indent(self):
  31. self.x += 10
  32.  
  33. def unindent(self):
  34. self.x -= 10
  35.  
  36.  
  37. pygame.init()
  38.  
  39. # Initialize the GPIO interface
  40. GPIO.setmode(GPIO.BCM)
  41.  
  42. # Set the width and height of the screen [width,height]
  43. size = [500, 500]
  44. screen = pygame.display.set_mode(size)
  45.  
  46. pygame.display.set_caption("joystick test app")
  47.  
  48. #Loop until the user clicks the close button.
  49. done = False
  50.  
  51. # Used to manage how fast the screen updates
  52. clock = pygame.time.Clock()
  53.  
  54. # Initialize the joysticks
  55. pygame.joystick.init()
  56.  
  57. # Get ready to print
  58. textPrint = TextPrint()
  59.  
  60. # Make an ouput array
  61. output = list("test")
  62.  
  63. # Start the radio
  64. pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
  65.  
  66. radio = NRF24(GPIO, spidev.SpiDev())
  67. radio.begin(0, 17)
  68.  
  69. radio.setPayloadSize(32)
  70. radio.setChannel(0x76)
  71. radio.setDataRate(NRF24.BR_250KBPS)
  72. radio.setPALevel(NRF24.PA_MAX)
  73.  
  74. radio.setAutoAck(True)
  75. radio.enableDynamicPayloads()
  76. radio.enableAckPayload()
  77.  
  78. radio.openWritingPipe(pipes[0])
  79. radio.openReadingPipe(1, pipes[1])
  80. radio.printDetails()
  81.  
  82. # -------- Main Program Loop -----------
  83. while done==False:
  84. # create array for output
  85. # EVENT PROCESSING STEP
  86. for event in pygame.event.get(): # User did something
  87. if event.type == pygame.QUIT: # If user clicked close
  88. done=True # Flag that we are done so we exit this loop
  89.  
  90. # Possible joystick actions: JOYAXISMOTION JOYBALLMOTION JOYBUTTONDOWN JOYBUTTONUP JOYHATMOTION
  91. if event.type == pygame.JOYBUTTONDOWN:
  92. print("Joystick button pressed.")
  93. if event.type == pygame.JOYBUTTONUP:
  94. print("Joystick button released.")
  95.  
  96.  
  97. # DRAWING STEP
  98. # First, clear the screen to white. Don't put other drawing commands
  99. # above this, or they will be erased with this command.
  100. screen.fill(WHITE)
  101. textPrint.reset()
  102.  
  103. # Get count of joysticks
  104. joystick_count = pygame.joystick.get_count()
  105.  
  106. textPrint.Print(screen, "Number of joysticks: {}".format(joystick_count) )
  107. textPrint.indent()
  108.  
  109. # For each joystick:
  110. for i in range(joystick_count):
  111. joystick = pygame.joystick.Joystick(i)
  112. joystick.init()
  113.  
  114. textPrint.Print(screen, "Joystick {}".format(i) )
  115. textPrint.indent()
  116.  
  117. # Get the name from the OS for the controller/joystick
  118. name = joystick.get_name()
  119. textPrint.Print(screen, "Joystick name: {}".format(name) )
  120.  
  121. # Usually axis run in pairs, up/down for one, and left/right for
  122. # the other.
  123. axes = joystick.get_numaxes()
  124. textPrint.Print(screen, "Number of axes: {}".format(axes) )
  125. textPrint.indent()
  126. axisvals = []
  127.  
  128. for i in range( axes ):
  129. axis = joystick.get_axis( i )
  130. textPrint.Print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
  131. axis = (axis+1)/2
  132. axis = int(axis * 179)
  133. axisvals.append(axis)
  134. output.append(chr(axis))
  135.  
  136. textPrint.unindent()
  137.  
  138. buttons = joystick.get_numbuttons()
  139. textPrint.Print(screen, "Number of buttons: {}".format(buttons) )
  140. textPrint.indent()
  141. buttonvals = []
  142.  
  143. for i in range( buttons ):
  144. button = joystick.get_button( i )
  145. textPrint.Print(screen, "Button {:>2} value: {}".format(i,button) )
  146. buttonvals.append(button)
  147. # output.append(chr(button)) for now, im only sending the axis data
  148.  
  149. textPrint.unindent()
  150.  
  151. # Hat switch. All or nothing for direction, not like joysticks.
  152. # Value comes back in an array.
  153. hats = joystick.get_numhats()
  154. textPrint.Print(screen, "Number of hats: {}".format(hats) )
  155. textPrint.indent()
  156. hatvals = []
  157.  
  158. for i in range( hats ):
  159. hat = joystick.get_hat( i )
  160. textPrint.Print(screen, "Hat {} value: {}".format(i, str(hat)) )
  161. hat = str(hat)
  162. x,y=hat.split(",") #hats is given as a strign (x, y) so we split it
  163. if x.find("0")>=0: # only possible values are 1, 0, -1, so we search both halves
  164. hatvals.append(int(0))
  165. elif x.find("-")>=0:
  166. hatvals.append(int(-1))
  167. else:
  168. hatvals.append(int(1))
  169. if y.find("0")>=0:
  170. hatvals.append(int(0))
  171. elif y.find("-")>=0:
  172. hatvals.append(int(-1))
  173. else:
  174. hatvals.append(int(1))
  175. textPrint.unindent()
  176.  
  177. radio.flush_tx()
  178. while (len(output) < 32):
  179. output.append(0)
  180. radio.write(output)
  181. time.sleep(.005)
  182. # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
  183.  
  184. # Go ahead and update the screen with what we've drawn.
  185. pygame.display.flip()
  186.  
  187. # Limit to 20 frames per second
  188. clock.tick(20)
  189.  
  190. # Close the window and quit.
  191. # If you forget this line, the program will 'hang'
  192. # on exit if running from IDLE.
  193. pygame.quit ()
  194.  
  195. #include<SPI.h>
  196. #include<RF24.h>
  197.  
  198.  
  199. // CE, CSN pins
  200. RF24 radio(9, 10);
  201.  
  202. void setup(void){
  203. while(!Serial);
  204. Serial.begin(9600);
  205.  
  206. radio.begin();
  207. radio.setPALevel(RF24_PA_MAX);
  208. radio.setChannel(0x76);
  209. radio.openWritingPipe(0xF0F0F0F0E1LL);
  210. const uint64_t pipe = (0xE8E8F0F0E1LL);
  211. radio.openReadingPipe(1, pipe);
  212.  
  213. radio.enableDynamicPayloads();
  214. radio.powerUp();
  215.  
  216. }
  217.  
  218. void loop(void){
  219. radio.startListening();
  220. Serial.println("Starting loop. Radio on.");
  221. char receivedMessage[32] = {0};
  222. if(radio.available()){
  223. radio.read(receivedMessage, sizeof(receivedMessage));
  224. Serial.println(receivedMessage);
  225. } else {
  226. Serial.println("No data!");
  227. }
  228. delay(100);
  229.  
  230. }
  231.  
  232. Traceback (most recent call last):
  233.  
  234.  
  235. File "joystick_test.py", line 180, in <module>
  236. radio.write(output)
  237. File "/home/pi/Desktop/NRF24L01/lib_nrf24.py", line 452, in write
  238. self.startWrite(buf)
  239. File "/home/pi/Desktop/NRF24L01/lib_nrf24.py", line 485, in startWrite
  240. self.write_payload(buf)
  241. File "/home/pi/Desktop/NRF24L01/lib_nrf24.py", line 268, in write_payload
  242. return self.spidev.xfer2(txbuffer)
  243. OverflowError: Argument list size exceeds 4096 bytes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement