AirMouse – control mouse pointer in Linux using one switch or by blowing on microphone

Article under construction!

To install xdotool:

sudo apt install xdotool

Bash script (save as “airmouse” and “chmod 755 airmouse” to make it executable):

#!/bin/bash

# Run these commands first in terminal (as root)
#
#    stty -F /dev/ttyUSB0 raw
#    cat /dev/ttyUSB0 | ./airmouse
#

# Process input characters from arduino (u,d,l,r,c)
while true;
do
    read -n 1 INPUT
    if [ "$INPUT" = "u" ]; then
        xdotool mousemove_relative -- 0 -10
    elif [ "$INPUT" = "d" ]; then
        xdotool mousemove_relative -- 0 10
    elif [ "$INPUT" = "l" ]; then
        xdotool mousemove_relative -- -10 0
    elif [ "$INPUT" = "r" ]; then
        xdotool mousemove_relative -- 10 0
    elif [ "$INPUT" = "c" ]; then
        xdotool click 1
    fi
done

Arduino code:

//
// AirMouse - Ted Burke - 2/4/2019
//

void setup()
{
  pinMode(2, OUTPUT);

  Serial.begin(9600);

  set_state(1);
}

int state = 1;
unsigned long t;
int direction = 0; // 0:up, 1:down, 2:left, 3:right

void set_state(int n)
{
  state = n;
  t = millis();
}

void loop()
{
  int v;
  unsigned long elapsed;

  delay(50);
  
  v = analogRead(7);
  elapsed = millis() - t; // time elapsed since entering current state

  if (state == 1) // STATE 1: WAIT FOR INPUT
  {
    digitalWrite(2, LOW);

    if (v > 512) set_state(2);
  }
  else if (state == 2) // STATE 2: IS THIS A CLICK OR A MOVE?
  {
    digitalWrite(2, HIGH);

    if (v <= 512) set_state(3);
    if (elapsed >= 200) set_state(4);
  }
  else if (state == 3) // STATE 3: SEND A CLICK
  {
    digitalWrite(2, LOW);
    Serial.print("c");
    delay(250);
    set_state(1);
  }
  else if (state == 4) // STATE 4: MOVE MOUSE
  {
    digitalWrite(2, HIGH);

    if (v <= 512) set_state(5);
    else if (direction == 0) Serial.print("u");
    else if (direction == 1) Serial.print("d");
    else if (direction == 2) Serial.print("l");
    else if (direction == 3) Serial.print("r");
  }
  else if (state == 5) // STATE 5: CHANGE MOUSE DIRECTION
  {
    digitalWrite(2, LOW);
    
    direction = (direction + 1)%4;

    set_state(1);
  }
}
Advertisements
This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s