Sending signals from microcontroller to linux terminal

Hi everyone. I want to know if it is possible to use my kl25z in order to control my linux command line. I’m looking for information on how to do this. For example, receiving a push button signal with a digital in and executing an mp3 file with the terminal.
If anyone has any information on how to do it i would really appreciate it! Thanks.

Hello Carlos,

You can emulate a USB keyboard with your kl25z.
For example:

/*
 * -------
 *        |
 * KL25Z  |
 *        |
 *        |
 *     D5 |-------
 *        |       |
 * -------        |
 *                o
 *                 /
 *                o
 *                |
 *               ---
 *               GND
 */

#include "mbed.h"
#include "USBKeyboard.h"

#define ENTER_KEY       10
#define BOUNCING_TIME   300*1000    // 300 ms

USBKeyboard key;
InterruptIn startMP3Player(D5, PullUp);
Thread      t;
EventQueue  queue;
Timeout     btnDebouncer;
bool        btnIsBouncing = false;

void bouncingOver(void)
{
    btnIsBouncing = false;
}

void startMP3Player_handler(void)
{
    if (btnIsBouncing == false) {
        key.printf("start mp3 player"); // replace this text with the terminal command starting MP3 player
        key.key_code(ENTER_KEY);        // send ENTER key pressed
        btnIsBouncing = true;
        btnDebouncer.attach_us(callback(bouncingOver), BOUNCING_TIME);
    }
}

int main(void)
{
    t.start(callback(&queue, &EventQueue::dispatch_forever));
    startMP3Player.fall(queue.event(startMP3Player_handler));
}

The emulated USB keyboard works in parallel with the regular keyboard connected to the Linux machine.
Because the keyboard output is sent to the topmost (active) window open a terminal window on the Linux machine before pushing the button on your kl25z.

1 Like

Thank you very much. This is very useful for my project.

Excuse me, do you know why this code doesn’t work??

#include “mbed.h”
#include “USBKeyboard.h”

InterruptIn windOfChange(PTD3);
USBKeyboard key;

void playSong()
{
key.printf(“vlc windofchange.mp3”);
}

int main(void)
{

windOfChange.mode(PullUp);
wait(0.2);
windOfChange.fall(&playSong);
while (1) {

}

}

Could you please spell out what you mean by “doesn’t work”?
Was the text “vlc windofchange.mp3” printed to the terminal window when you pressed the switch connected to the “PTD3” pin? If yes, then try to add:

...
#define ENTER_KEY       10
...
void playSong()
{
    key.printf("vlc windofchange.mp3");
    key.key_code(ENTER_KEY);
}
1 Like

Hi again. THanks for the reply. I don’t know what is the problem but whenever I connect my KL25Z to my computer through the USB connection (not SDA) and use the push button, it doesn’t work.
As a side note, I already tried the hello word program for the USBKeyboard class and it works pefectly. It seems that whenever I try to send an outside signal to my KL25Z, it does nothing.

This is the Hello world code for USBKeyboard.

#include "mbed.h"
#include "USBKeyboard.h"

USBKeyboard key;

int main(void)
{
  while (1) {
      key.printf("Hello World\r\n");
      wait(1.0);
  }
}

Thank you very much.

Update: this what it does. It just prints “v” forever until y press down the reset button on my microcontroller.
ezgif.com-video-to-gif

Since I do not have a KL25Z board I have tested both codes (the code I initially proposed and the code you said does not work) on a NUCLEO-F767ZI.
The first code worked fine but the other one resulted in a runtime error:

++ MbedOS Error Info ++
Error Status: 0x8001012F Code: 303 Module: 1
Error Message: Error - writing to a file in an ISR or critical section

Location: 0x8006249
Error Value: 0x3
Current Thread: main Id: 0x20005578 Entry: 0x8006A3F StackSize: 0x1000 StackMem: 0x20006448 SP: 0x2007FCF8 
For more info, visit: https://mbed.com/s/error?error=0x8001012F&tgt=NUCLEO_F767ZI
-- MbedOS Error Info --

= System will be rebooted due to a fatal error =
= Reboot count(=1) reached maximum, system will halt after rebooting

I think it’s because of calling printf in ISR:

void playSong()
{
    key.printf(“vlc windofchange.mp3”);
}

Does the code I initially proposed work on your KL25Z?
It should, because the execution of the fall callback function is deferred from an interrupt context (ISR) to an event loop’s thread.

The code you proposed couldn’t compile because Thread is undefined, maybe there’s a library I forgot to include in my program (?). However, the code I sent had no errors while compiling.
About the printf in ISR, I don’t know any other way of ‘printing’ a string.
Also, the library I imported was USBDevice.

I have published my initial code here.

  • Open the link and then click on the “Import into Compiler” button (in top-right corner).
  • Select “FRDM-KL25Z” as target and compile.
1 Like