/ ROB_Control / code.py
code.py
 1  # SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  # Nintendo R.O.B. control with Accelerometer, code in CircuitPython
 6  # Using an Adafruit Circuit Playground Express board with an IR LED
 7  # Anne Barela for Adafruit Industries, MIT License, May, 2018
 8  # Acknowledgement to info at http://atariage.com/forums/topic/177286
 9  # -any-interest-in-nes-rob-homebrews/ and Limor Ladyada Fried
10  
11  import time
12  import gc
13  from digitalio import DigitalInOut, Direction
14  from adafruit_circuitplayground.express import cpx
15  import board
16  
17  # Commands, each 8 bit command is preceded by the 5 bit Init sequence
18  Init = [0, 0, 0, 1, 0]            # This must precede any command
19  Up = [1, 0, 1, 1, 1, 0, 1, 1]     # Move arms/body down
20  Down = [1, 1, 1, 1, 1, 0, 1, 1]   # Move arms/body up
21  Left = [1, 0, 1, 1, 1, 0, 1, 0]   # Twist body left
22  Right = [1, 1, 1, 0, 1, 0, 1, 0]  # Twist body right
23  Close = [1, 0, 1, 1, 1, 1, 1, 0]  # Close arms
24  Open = [1, 1, 1, 0, 1, 1, 1, 0]   # Open arms
25  Test = [1, 1, 1, 0, 1, 0, 1, 1]   # Turns R.O.B. head LED on
26  
27  print("R.O.B. Start")
28  
29  # Circuit Playground Express IR LED Setup
30  IRled = DigitalInOut(board.REMOTEOUT)
31  IRled.direction = Direction.OUTPUT
32  
33  # This function performs the LED flashing of a passed command
34  # Note timing is very tight. Machine instructions are 2-5 ms or so
35  # so that is why the time for cycles doing work is < 16.66667 ms (1/60)
36  # Each pulse/space is 1.5 milliseconds out of 16.7 total ms (NTSC)
37  
38  def IR_Command(cmd):
39      gc.collect()                     # collect memory now
40      # Output initialization and then command cmd
41      for val in Init+cmd:             # For each value in initial+command
42          if val:                      # if it's a one, flash the IR LED
43              IRled.value = True       # Turn IR LED turn on for 1.5 ms
44              time.sleep(0.0015)       # 1.5 ms on
45              IRled.value = False      # Turn IR LED off for 15 ms
46              time.sleep(0.0150)       # 15 ms
47          else:
48              time.sleep(0.0167)       # 1 cycle turn off
49  
50  while True:                          # Main Loop poll switches, do commands
51      x, y, z = cpx.acceleration       # Read accelerometer
52      print((x, y, z))                 # Print for debug
53      if x > 5 and y > -8:             # Clockwise from back
54          IR_Command(Right)            # Turn Right
55      if x < -5 and y > -8:            # Counterclockwise from back
56          IR_Command(Left)             # Turn Left
57      if x > 1 and y < -10:            # Move direction of USB
58          IR_Command(Up)               # Body up
59      if x < -1 and y < -10:           # Move in direction of battery con
60          IR_Command(Down)             # Body Down
61      if cpx.button_a:
62          IR_Command(Open)             # Button A opens arms
63      if cpx.button_b:
64          IR_Command(Close)            # Button B closes arms
65      if cpx.switch:                   # Toggle switch turns "test" on
66          IR_Command(Test)             # which is the LED on R.O.B.s head
67      time.sleep(0.1)