mob-i-l
Posts: 315
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
Contact: Website Facebook Google+ Twitter YouTube

Scratch-program in Adventures in Raspberry Pi: marchmallow

Mon Mar 02, 2015 2:20 am

In the book Adventures in Raspberry Pi there is a Scratch-program in Adventure 8 that can be downloaded from the books homepage http://www.wiley.com/go/adventuresinrp: marshmallow.sb. Unfortunately it doesn't work now and this might be due to the current Scratch or the uinput-driver. I have made a new version that works and it's attached:
marchmallow2.gif
marchmallow2.gif (10.92 KiB) Viewed 1496 times
Before the key a was polled in a loop.

I also used the latest python-uinput-0.10.2.tar.gz. In order to build and install that I had to add a symlink using:

Code: Select all

sudo ln -s /lib/arm-linux-gnueabihf/libudev.so.0.13.0  /lib/arm-linux-gnueabihf/libudev.so
I installed it for both Python 2 and Python 3:

Code: Select all

python setup.py build
sudo python setup.py install
python3 setup.py build
sudo python3 setup.py install
Now it works both using python and python3.
Have Pi0&1A&1B&1B+&2B&3B&4B w/ Raspbian. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch-program in Adventures in Raspberry Pi: marchmall

Mon Mar 02, 2015 6:42 am

Hello,

when executing this program (original version from the download) with scratch-1.4.-windows and with raspbian new scratch, both result in same behavior: the costumes are changing for 20 secs, and within this time when I press 'a' then the drum is hit.

There is a bug in new scratch https://github.com/raspberrypi/scratch/issues/70 which affects the sound output of these synthethic sounds. Which works with scratch.old (try this from a console, start scratch.old and retry the code).

What I do not see is the need for python in this sketch, as there are no external sensors attached. Same for uinput. Could you give some more information on how this code was broken in your system ?

Regards,
Gerhard

mob-i-l
Posts: 315
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
Contact: Website Facebook Google+ Twitter YouTube

Re: Scratch-program in Adventures in Raspberry Pi: marchmall

Mon Mar 02, 2015 2:01 pm

Yes, both Scratch-programs work on my computer when I press a on the keybord, and sound works too using new Scratch. But only my version works when I use uinput to generate keypresses from Python 2 or 3. The purpose of this Scratch-program is to squeeze a marchmallow as many times as possible during the game by connecting GPIO BCM 2 (i.e. pin 3) to GND (e.g. pin 6) via a marshmallow to and generate keypresses using a Python-program. You have to load the driver after each boot:

Code: Select all

sudo modprobe uinput
It seems the driver uinput doesn't work as a keyboard when one polls the keyboard in Scratch, but it does work when one uses it to trigger keyboard events. Maybe the original program worked with the old Scratch. Otherwise, uinput seems like a simple way to get real world events into Scratch. I will test, but I can't run my browser (Iceape) and Scratch at the same time due to low RAM on this Raspberry Pi A with 32 MB GPU-RAM.

Edit: Now I tested, and the original version didn't respond to simulated keypresses in scratch.old.
Have Pi0&1A&1B&1B+&2B&3B&4B w/ Raspbian. Started w/ BASIC on ABC80&ZX81 then Forth, Z80… https://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://twitter.com/mobluse/ https://YouTube.com/MOBiL4u/

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Scratch-program in Adventures in Raspberry Pi: marchmall

Mon Mar 02, 2015 9:18 pm

Hello,

interesting way to implement inter-process-communication. Tried it out, with a sample python code to send key-events. With leafpad in foreground, the events are arriving and the text is 'printed' on the screen. And confirming your observation, scratch is only receiving events, but no key presses.

Usually I solve things with my scratchClient-framework. Took the time to add a configuration named 'marshmallow' to scratchClient.

Code: Select all

<?xml version='1.0' encoding='utf-8' ?>
<config version='1.0'
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="config.xsd" >
    <!-- license GPL2    -->
    <description>
        Scratch-program in Adventures in Raspberry Pi: marshmallow.
        A button, formed by a marshmallow, between GPIO2 (3) and GND (6).
        sends event 'squeeze' to scratch 
    </description>
    
    <adapter class='adapter.gpio.GpioButtonInput' name='button_gpio2'>
        <description>marshmallow button</description>
        <gpio port='GPIO02'>
            <default dir='IN' pull='PUD_UP'  />
            <active dir='IN' pull='PUD_UP'/>
        </gpio>
        
        <output name='button_pressed'>
            <broadcast name='squeeze'/>
        </output>
        <output name='button_released'>
            <broadcast name='released'/>
        </output>
        
        <parameter name='poll.interval' value='0.05' />
        <parameter name='value.inverse' value='true' />
    </adapter>
</config>
start with (but install first)

Code: Select all

cd ~/scratchClient
sudo python src/scratchClient.py -c marshmallow
Sends events 'squeeze' when pressed. And enable remote sensor communications first. And works even with scratch not having focus.

Regards,
Gerhard

scratchClient download: http://heppg.de/ikg/wordpress/?page_id=6

timrowledge
Posts: 1346
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Scratch-program in Adventures in Raspberry Pi: marchmall

Fri Mar 06, 2015 12:19 am

So do marshmallows squeeze better than a jelly baby?
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

Return to “Scratch”