Intel® Makers
Intel® Edison, Intel® Joule™, Intel® Curie™, Intel® Galileo
Announcements
Welcome - This is a Peer-to-Peer Forum only. Intel has discontinued these products but you may find support from other customers on this Forum
9868 Discussions

Control Blinking LED Using USB Peripherals On Edison

MMora15
Novice
725 Views

Hello.

 

I've been messing around with USB peripherals (mouse, keyboard, ect.) using the native USB port on the Intel Edison Arduino Expansion Board and can't seem to figure out how to utilize HID inputs in my Arduino scripts to control things even as simple as the blinking LED.

 

Something as simple as a keypress/left-click to turn on the LED would certainly serve as an adequate proof of concept.

 

Would anyone be able to assist me with a basic example script to do this or let me know if this is even possible?

 

Thanks in advance.

1 Solution
Luis_at_Intel
Moderator
374 Views

Hi @MMora15​ ,

 

Thanks for reaching out. What you are trying to do should be definitely possible using Python*, I have put together a quick script that should get you started. I have used MRAA library (should be installed by default on your board) to trigger the on-board LED and a 3rd party library to read the mouse events (to install run "pip install evdev", for more info visit here).

 

Here is the sample code, you may need to modify it to meet your needs but should be enough to get you started, please let me know if you have any other questions.

import mraa from evdev import InputDevice, categorize, ecodes # see tutorial here https://python-evdev.readthedocs.io/en/latest/tutorial.html dev = InputDevice('/dev/input/event2') # mouse # initialize on-board led as output led = mraa.Gpio(13) led.dir(mraa.DIR_OUT) print(dev) # device info for event in dev.read_loop(): if event.type == ecodes.EV_KEY: key = categorize(event) print(key) if event.code == 272: # left click led.write(1) # turn on led elif event.code == 273: # right click led.write(0) # turn off led elif event.code == 274: # scroll wheel led.write(0) print("Done") exit(0) # end program

 

Regards,

Luis

 

View solution in original post

8 Replies
Luis_at_Intel
Moderator
375 Views

Hi @MMora15​ ,

 

Thanks for reaching out. What you are trying to do should be definitely possible using Python*, I have put together a quick script that should get you started. I have used MRAA library (should be installed by default on your board) to trigger the on-board LED and a 3rd party library to read the mouse events (to install run "pip install evdev", for more info visit here).

 

Here is the sample code, you may need to modify it to meet your needs but should be enough to get you started, please let me know if you have any other questions.

import mraa from evdev import InputDevice, categorize, ecodes # see tutorial here https://python-evdev.readthedocs.io/en/latest/tutorial.html dev = InputDevice('/dev/input/event2') # mouse # initialize on-board led as output led = mraa.Gpio(13) led.dir(mraa.DIR_OUT) print(dev) # device info for event in dev.read_loop(): if event.type == ecodes.EV_KEY: key = categorize(event) print(key) if event.code == 272: # left click led.write(1) # turn on led elif event.code == 273: # right click led.write(0) # turn off led elif event.code == 274: # scroll wheel led.write(0) print("Done") exit(0) # end program

 

Regards,

Luis

 

View solution in original post

MMora15
Novice
374 Views

Hi Luis.

 

Thanks for getting back to me. I'll definitely look into this python script and test it out. The LED was just an example of interacting with physical hardware on/attached to the board from peripherals.

 

Would it be possible to implement a similar solution with the mraa library in C or C++ for use in an Arduino script?

 

Thanks.

 

Matthew

Luis_at_Intel
Moderator
374 Views

Hi @MMora15​ ,

 

Take a look at this guide on how to use MRAA library in Arduino https://software.intel.com/en-us/articles/mraa-and-upm-basics-in-arduino-create. It should be possible to implement a similar solution in an Arduino script. Let me know if you have any additional questions.

 

Regards,

@Luis_at_Intel​ 

MMora15
Novice
374 Views

Hi @Luis_at_Intel​ ,

 

I've been having some problems with installing the MRAA libraries on my Edison board.

 

I attempted to compile your Python script using the default Python installation that ships with the Edision (version 2.7.3), but received the following terminal output:

root@edison:~/programs# python led.py Traceback (most recent call last): File "led.py", line 1, in <module> import mraa ImportError: No module named mraa

I also attempted to compile some of the example C scripts provided in the MRAA repository but received similar errors regarding the MRAA preprocessor directives.

 

Any advice on how I could install libmraa to get these to compile?

Luis_at_Intel
Moderator
374 Views

Hi @MMora15​ ,

 

I apologize for the delay in my response. You can try using AlexT repo's instructions here https://alextgalileo.altervista.org/edison-package-repo-configuration-instructions.html and run "opkg install mraa" to install the mraa library. The board must be connected to the WiFi so it can download the required files.

 

I can confirm that the version downloaded from the repository works with the code. You can check the version of mraa with the command as shown below.

root@edison:~# opkg list-installed mraa

mraa - 1.9.0

 

Hope this helps, let me know if you have any other questions.

 

 

Regards,

@Luis_at_Intel​ 

 

MMora15
Novice
374 Views

Hi @Luis_at_Intel​ 

 

Thanks for your continuous support with my Edison questions.

 

I've been working a lot with the mraa libraries lately, but I was wondering if there is a way to utilize/interact with the native USB host port on the Edison using the Arduino scripting interface with the Arduino IDE (so without using MRAA).

 

I noticed after installing Intel Edison i686 board support to my Arduino IDE, some Edison-specific example scripts were also added, including one called USBHost(Edison) which includes both KeboardController.ino and MouseController.ino examples, but I haven't had any luck getting either of them to work.

 

If you or anyone else has any examples on how to use these examples or implement another Arduino solution for the USB port, I would much appreciate it. Thanks in advance.

Aroop_B_Intel
Employee
374 Views

Hi @MMora15​ ,

 

Can you upload the KeyboardController.ino and MouseController.ino examples to your board? If not, what errors are you getting?

 

The following links list functions you can use in the Arduino IDE relating to Keyboard and Mouse respectively:

Keyboard: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

Mouse: https://www.arduino.cc/reference/en/language/functions/usb/mouse/

Regards,

@Aroop_at_Intel​ 

FerryT
Valued Contributor I
374 Views

Not sure if your question is about controlling the LED or about handling keyboard/mouse. I think you already found the blink-led script from the original Edison image here.

 

If you are using a newer image like thud a fixed version is here. But current kernel (5.0) also allow handling the LED by the kernel itself by creating an ACPI table. By default this will let the kernel take the LED and blink it with 'heart beat', a LED 'trigger' that blinks depending on the CPU load. There are quite a few others available too. By default we build ledtrig-heartbeat, ledtrig-oneshot, ledtrig-pattern, ledtrig-timer.ko.

 

Of course loading the led.aml table makes the kernel own the LED and user space needs to go through the sysfs interface to interact, in this case through /sys/class/leds/heartbeat.

 

There is also the good old pwr-button-handler.c which responds to the power button, depending on the length of the press. Uses same input event mechanism as in the mraa example. Note: currently systemd-logind is handling the power button, so the pwr-button-handler is never called. This needs to be disabled in logind configuration. I have a patch that takes care of this automatically, which I will push shortly. But if you rewrite this to read mouse events that is not an issue.

Reply