Awesome
<h1 align="center"> <a href="https://github.com/musabkilic/MicroBike">MicroBike</a> </h1> <p align="center"> <img src="https://img.shields.io/github/license/musabkilic/microbike.svg"/> <img src="https://img.shields.io/github/stars/musabkilic/microbike.svg"/> <a href="https://github.com/carlosperate/awesome-microbit"><img src="https://awesome.re/mentioned-badge.svg"/></a> </p>Turn your micro:bit into a Game Controller.
- [10 December 2017] :uk: Github Page: https://github.com/musabkilic/MicroBike/
- [27 December 2018] :uk: Project Page on My Website: https://musab.netlify.com/projects/MicroBike
The websites listed under are NOT controlled by me and I'm NOT responsible for the changes that may happen in the future
- [14 November 2018] :uk: Featured on official Micro:Bit Ideas page: https://microbit.org/ideas/
- [9 January 2019] :tr: SAMM Blog Tarafından Türkçe Yazı: https://blog.samm.com/microbit-projeleri-microbike/
- [19 January 2019]: :uk: Featured on "10 Amazing micro:bit Projects", Micro:Mag Issue 3: https://micromag.cc/issuethree/
- [7 February 2019] :poland: Featured on RoboCap, Warsztaty programowania i robotyki dla dzieci: https://robocap.org/microbit/index01.html#/1
- [20 March 2019] :uk: Featured on "16 Top BBC micro:bit Projects": https://www.itpro.co.uk/desktop-hardware/26289/13-top-bbc-micro-bit-projects
- [25 March 2019] :uk: Featured on "Meet the BBC micro:bit": https://www.diogomakes.com/blog/meet-the-bbc-microbit/
- [13 April 2019] :cn: Chinese Translation by 江宗諭: https://vmaker.tw/archives/32557
- [16 April 2019] :de: CalliMoto, a MicroBike Inspired Controller: https://www.hackster.io/thorsten-kimmeskamp/callimoto-50a10d
- [18 April 2019] :estonia: Tallinna Polütehnikum, Introduction to Micro:Bit: http://nelus.tpt.edu.ee/txt/J4i8X-microbit.txt
- [10 December 2019] :uk: Featured on "Top 25 Micro:Bit Projects You Must Try 2019!": https://www.seeedstudio.com/blog/2019/12/10/top-25-microbit-projects-for-beginners-2019/
What is it?
This project allows you to control PC games using a BBC micro:bit as the game controller. To get the code to work, you'll need a couple of extra Python modules installed onto your local machine:
- Pynput, "Monitor and control user input devices"
- David Whale's bitio library, which "allows you to run code in Python on a PC/Mac/Linux/Raspberry Pi and interact directly with the micro:bit"
Installation
Setting up your PC
You'll need to set up the device on which the game will be played first.
- If you don't already have Python, download Python 2.7 from this link
- If you don't already have Pip, install it by following these instructions. Pip is a "package manager" for Python, and makes getting set up with Python packages really easy.
- Get the MicroBike folder and install the required modules.
Using command line
Open a new command line window. This is called 'Terminal' on a Mac, 'Command Prompt' on Windows, and 'shell' or 'terminal' on Linux. Type the following:
git clone https://github.com/musabkilic/MicroBike
This gets the latest MicroBike code from this Git repository.
Navigate to the MicroBike folder in your command line window using the 'cd' command - you may need to change the path, depending on how you've configured git on your computer:
cd MicroBike
Next, install the required modules:
pip install -U -q -r requirements.txt
Click below to see a demonstration of this:
Setting up the bitio library
See this to set up David Whale's bitio library.
Setting up your micro:bit
Connect your micro:bit to your computer. Get the latest bitio.hex from the bitio repository, and drag this hex file to your micro:bit to 'flash' it to the device.
If you're on Windows, you'll also need to install the Windows serial driver on your computer.
Done! You can use MicroBike by typing python controller.py
in your computer's command line.
How does it work?
Let's review the code for controller.py to understand how this works.
import microbit
import time
from pynput.keyboard import Key, Controller
We need to import the modules to use them later. We will use 3 modules; microbit module for controlling and reading data from the micro:bit, time module for waiting for a specific time step and pynput module to control the keyboard(and the game of course).
#Function for Changing a Key
def changeKeyState(key, value):
global keyboard_keys
#Change Only Neccessary
if value!=keyboard_keys.get(key, False):
if value:
keyboard.press(key)
else:
keyboard.release(key)
keyboard_keys[key] = value
changeKeyState
is a function, it will help us to control the keyboard keys - for example if the handlebar goes left, it will press the left arrow key.
#Specify Keyboard
keyboard = Controller()
#Set Accelerometer Values
previous_values = microbit.accelerometer.get_values()
keyboard_keys = {}
#Set Images
stable = microbit.Image("00000:00000:99999:00000:00000")
images = {"N": microbit.Image.ARROW_N,
"NE": microbit.Image.ARROW_NE,
"NW": microbit.Image.ARROW_NW,
"E": microbit.Image.ARROW_E,
"W": microbit.Image.ARROW_W,
"": stable}
We will define some variables to use them later.
#Wait for User to Press a Button
while 1:
#Blink
microbit.display.show(microbit.Image.ARROW_W)
time.sleep(0.5)
microbit.display.clear()
#Start the Program if a Button is Pressed
if microbit.button_a.was_pressed() or microbit.button_b.was_pressed():
break
time.sleep(0.5)
This is the first loop. It will keep blinking until the user presses the A or B button. After pressing the button controller will start running.
#Start the Loop
while 1:
#Get Accelerometer Values
accelerometer_values = microbit.accelerometer.get_values()
x,y,z = accelerometer_values
#Calculate Avarege Motion in X,Y,Z Directions
motion = sum(map(lambda x:abs(accelerometer_values[x]-previous_values[x]),range(3)))/3
This is the main loop. We will start by getting required values and calculating the motion.
#Change Direction
changeKeyState(Key.up, y>400)
changeKeyState(Key.right, x>60)
changeKeyState(Key.left, x<-60)
changeKeyState(Key.shift, motion>500)
#Set Direction to Show
direction = ""
if y>400:
direction += "N"
if x>60:
direction += "E"
elif x<-60:
direction += "W"
#Show the Direction
microbit.display.show(images[direction])
#Set Current Accelerometer Values to Previous
previous_values = accelerometer_values
Then we will use the information we get before to control the game. Keyboard keys will trigger when the microbit turns right or left higher than a specific value.
We will use same information for changing the direction on the microbit.
<p align="center"> Musab Kılıç<br/> @musabkilic & @musabkilic0<br/> <a href="https://github.com/musabkilic/MicroBike/blob/master/LICENSE">Licensed under the GNU General Public License v3.0</a> </p>