User avatar
ReadiesCards
Posts: 40
Joined: Sun May 13, 2012 5:22 am

How to detect insertion of usb media card?

Wed Nov 07, 2012 7:54 am

I wish to create 'Granny's photo viewer'.

i.e. a RaspberryPi connected to a TV that scrolls through photos on its SD card BUT when Granny receives a new SD card in the post she inserts it into the USB slot via an adapter. The RPi then stops showing the pictures on its SD card but instead shows the pictures on the USB SD card and copies them to its internal SD card. When the USB SD card is removed the RPi now shows all the pictures (both new and old).

Simple in concept.

But I am stuck at how to get started. I am a C/C++ programmer so am considering writing an application with a thread to show the pictures and a thread to monitor the USB state. BUT as a very novice Linux user (let alone programmer) I would like to know if there is an easier way.

Is it simple to detect the USB media being present or not present in C/C++?
Is it simple to detect the USB media being present or not present in Python?

Many thanks for any tips
Paul

xoxox
Posts: 1
Joined: Mon Nov 12, 2012 2:53 pm

Re: How to detect insertion of usb media card?

Mon Nov 12, 2012 2:55 pm

Python is perfect for such tasks.
Have a look at http://pyinotify.sourceforge.net/

From the homepage:
Pyinotify is a Python module for monitoring filesystems changes. Pyinotify relies on a Linux Kernel feature (merged in kernel 2.6.13) called inotify. inotify is an event-driven notifier, its notifications are exported from kernel space to user space through three system calls. pyinotify binds these system calls and provides an implementation on top of them offering a generic and abstract way to manipulate those functionalities.

saintdev
Posts: 39
Joined: Mon Jun 18, 2012 10:56 pm

Re: How to detect insertion of usb media card?

Mon Nov 12, 2012 10:20 pm

xoxox wrote:Python is perfect for such tasks.
Have a look at http://pyinotify.sourceforge.net/

From the homepage:
Pyinotify is a Python module for monitoring filesystems changes. Pyinotify relies on a Linux Kernel feature (merged in kernel 2.6.13) called inotify. inotify is an event-driven notifier, its notifications are exported from kernel space to user space through three system calls. pyinotify binds these system calls and provides an implementation on top of them offering a generic and abstract way to manipulate those functionalities.
Inotify is for monitoring filesystem changes. File creation, deletion, modification. It doesn't monitor insertion and removal of block devices. You could probably monitor /dev for new files, but this would be an exceptionally poor way of doing it.

OP would probably be best looking at udisks*. udisks is implemented as a daemon that provides a D-Bus interface for monitoring storage devices. There are D-Bus bindings for most languages/toolkits available (C, C++, python, Qt, Glib...see D-Bus Bindings).

* I'm fairly sure most distros have transitioned over to udisks now. Some may still be using HAL, which has a similar, but depreciated, D-Bus interface for monitoring hardware changes.
SELECT `signature` FROM `users` WHERE `username`='saintdev';
Empty set (0.00 sec)

Return to “Graphics, sound and multimedia”