Last-Fi

23
Last-FI Ross McKinlay 2013

description

Last-Fi - an F# powered Raspberry Pi internet radio

Transcript of Last-Fi

Page 1: Last-Fi

Last-FI

Ross McKinlay 2013

Page 2: Last-Fi

Me

@pezi_pink

www.pinksquirrellabs.com

Page 3: Last-Fi

fsharp.org

Page 4: Last-Fi
Page 5: Last-Fi
Page 6: Last-Fi

Last-Fi!

Page 7: Last-Fi

Raspberry Pi & F#

• You want the SOFT float version of the Raspbian Linux distro! Save yourself lots of headaches!

• Fully update your Pi (apt-get update, apt-get upgrade)

• Ensure the SSH server starts when your Pi boots• apt-get mono-complete• Develop your projects on

another machine, and use WinSCP / PuTTY (or similar) to transfer

Page 8: Last-Fi

Access to the GPIO pins

http://www.airspayce.com/mikem/bcm2835/# tar -zxf bcm2835-1.3.tar.gz # cd bcm2835-1.3/src # make libbcm2835.a # cc -shared bcm2835.o -o libbcm2835.so

• You will need to download and build the bcm2835 library on the Pi which will enable you to access the pins

• In order to interop with .NET / Mono you must use a shared object (so) against a statically linked binary.

• You are now able to reference the .so file like any normal .dll file for interop using the [<DllImport>] attribute

Page 9: Last-Fi

LCD Screen!

• Almost all these displays use the Hitachi HD44780 LCD Controller

• Can be operated in 8 bit or 4 bit mode and either way they take up a ton of IO pins !

• After the initialization, the general usage is to shift a bunch of bits onto the data lines that will instruct the LCD what do to, then pulse the E line to indicate it should look at and act on the data.

• Reading and writing data is almost identical except you hold the RS line high between writing text in ASCII binary

Page 10: Last-Fi

LCD Screen!

Page 11: Last-Fi

LCD Screen!

• Using agents to isolate state and provide timing. It’s a self contained async system that can only be changed via safe messaging

• Messing about with low-level timing and bit shifting

• Cunning use of TryReceive to provide overrideable temporary text and text scrolling

Page 12: Last-Fi
Page 13: Last-Fi
Page 14: Last-Fi

NES Pad!• A NES pad is nothing more than a 8-bit parallel to serial shift

register!• Simply hold LATCH low (Write/Shift in this diagram) and then each

successive pulse on the CLOCK line will shift the state of the next button onto the DATA line (Q in this diagram)

Page 15: Last-Fi

NES Pad!

• Low-Level signal manipulation• Reading values using functional techniques such as list

folding• Isolating state and providing safe asynchronous

operation for both manual and interval polling via message passing

• Use of active patterns to determine internal state• Using first-class events to trigger on patterns• Use of active patterns to provide the consumer an

elegant way of expressing the matching of button depressing, depress durations and history sequence

Page 16: Last-Fi

Last.Fm API

• Unfortunately does not have any metadata associated with it such as a WSDL or an Odata endpoint which means I cannot use a type provider ! :’( Boo, Hiss

• There are .NET libraries consisting of large object hierarchies to access Last.Fm but I’m only interested in small portion of the API and don’t really want to use one of these…

Page 17: Last-Fi

MPC / MPD and the “Player”

• Music Player Deamon is the linux software I am using to play the music. It is capable of streaming from an internet address.

• MPC is the command-line interface to the deamon which allows control via the command line

• The “Player” object provides access to the user’s Last.Fm station via MPC and raises events to show stuff that’s happening

Page 18: Last-Fi

The core program…

• ServiceStack is used to host a webservice that can provide information about what’s playing and perform some control functions, via the Player object

• NES / LCD /Player objects are initalized and various events hooked up between them

• GO!! Everything at this point is async, the main program thread just sits there doing nothing (and more importantly, not blockng anything!)

Page 19: Last-Fi

The Website!

• The website uses Funscript, which is a F# to JavaScript compiler and a TypeScript type provider that allows direct access to JS libraries with F# code

• Funscript also hosts its own webserver, so I don’t need to mess about with ASP.NET linux equivalents

Page 20: Last-Fi
Page 21: Last-Fi
Page 22: Last-Fi
Page 23: Last-Fi