I want to do some introductory programming with Digispark's RGB Shield (not LED Shield), so it's a great introduction to programming from Scratch (ScratchX) to Digispark To operate. ** * When you actually do it, please do it at your own risk. ** **
Set up a local server at http: // localhost: 9000 with + bottle.
Digispark
libusb USB library Windows(Win7)
libusb-win32
with zadig. * * It would be awkward to rewrite the driver of another device, so be careful ... *OSX(El Capitan 10.11)
brew install libusb
Linux(Ubuntu14.04)
sudo apt-get install libusb-dev
Python
** * The version was 3.5.0
under any environment **
easy_install pip
pip install bottle pyusb
With user-local Python on Linux, I got a permission error when accessing USB using pyusb, so This post on StackOverflow -pyusb-libusb-require-root-sudo-permissions-on-linux) and USB Devices --Google Chrome. (Looking at the Chrome page, it feels like it's not limited to pyusb.)
In my case, create a file called 50 ー digispark.rules
in /etc/udev/rules.d/
and
SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", MODE="0664", GROUP="plugdev"
And restarted.
ScratchX runs on Flash Player, so any browser that runs Flash Player should work.
app.py
Build a + bottle local server and receive RGB data from javascript running on ScratchX.
#!python
#-*-coding:utf-8-*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
# for bottle
from bottle import route, run, template, request, response, static_file
# for scratchx
import usb
import sys
sys.path.append("..")
from arduino.usbdevice import ArduinoUsbDevice
theDevice = None
@route("/")
def top():
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="http://scratchx.org/?url=http://localhost:9000/main.js">ScratchX</a>
</body>
</html>
'''
@route("/<filepath:path>")
def server_static(filepath):
return static_file(filepath, root="./")
@route("/blink")
def blink():
theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df)
red = request.query.get('red')
green = request.query.get('green')
blue = request.query.get('blue')
print("red:{}, green:{}, blue:{}".format(red, green, blue))
red = int(mapping(red))
green = int(mapping(green))
blue = int(mapping(blue))
theDevice.write(ord("s"))
theDevice.write(red)
theDevice.write(green)
theDevice.write(blue)
print("mred:{}, mgreen:{}, mblue:{}".format(red, green, blue))
def mapping(arg):
arg = float(arg)
if arg < 0:
return 0
elif arg > 100:
return 255
else:
return arg * 0.01 * 255
if __name__ == '__main__':
run(host="localhost", port=9000, debug=True, reloader=True)
main.js
(function(ext){
var device = null;
ext._deviceConnected = function(dev){
if(device) return;
device = dev;
console.log(device);
device.open();
};
ext._deviceRemoved = function(dev){
if(device != dev) return;
device = null;
};
ext._shutdown = function(){
if(device) device.close();
device = null;
};
ext._getStatus = function(){
if(!device) return {status: 1, msg: 'digiUSB disconnected'};
return {status: 2, msg: 'digiUSB connectd'};
};
ext.blink = function(r, g, b){
$.ajax({
type: "GET",
url: "http://localhost:9000/blink",
dataType: "script",
data: {
red: r,
green: g,
blue: b
}
});
};
var descriptor = {
blocks: [
["", "red: %n, green: %n, blue: %Shine with n", "blink",
"100", "100", "100"]
],
menus: {},
url: 'http://localhost:9000'
};
var hid_info = {type: 'hid', vendor: 0x16c0, product: 0x05df};
console.log(ScratchExtensions.register('DigiUSB', descriptor, ext, hid_info));
})({});
and
main.js` in the same directory., you will also need [Library created by Digispark](https://github.com/digistump/DigisparkExamplePrograms/tree/master/Python/DigiBlink/source). .. If you enter the ʻarduino
directory of the above link into the directory containing ʻapp.py and
main.js`, it's OK.Directory
|- arduino/
|- app.py
|- main.js
python app.py
http: // localhost: 9000
with a web browser. There is a link called "ScratchX", so if you jump to the link destination, the ScratchX page where main.js
in the same hierarchy is loaded will open.If you look at the ScratchX Wiki, you should be able to send data directly from javascript to the USB-HID, but it didn't work, so this time It has become a roundabout shape like ... Looking at the code on the above page, it feels like I'm using this area.
If you leave the remnants of main.js
and insert the Scratch Extensions Browser Plugin plugin, "Other" on ScratchX Signal is connected green instead of unconnected red. (Check only on OSX and Chrome)
I used RGB Shield this time, but it seems to be out of stock now, so I'm ordering LED Shiled that can probably do the same. Looking at the Wiki, it feels like an Adafruit NeoPixel clone. (ambiguous)
As mentioned above, ScratchX runs on Flash Player. However, if you look at LLK's github, you can see that although it is a Player, Scratch's HTML5 version repository is also I'm looking forward to the full HTML5 in the future. I don't know if it can be done.
The programming threshold is really low (technically and economically). Physical computing You can do a lot of things that are wrong. I want you to talk about "further lowering the age of programming".
and
main.js` are placed here.Recommended Posts