Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlessandroSpallina/25dbebbacf0032edf4b8 to your computer and use it in GitHub Desktop.
Save AlessandroSpallina/25dbebbacf0032edf4b8 to your computer and use it in GitHub Desktop.
listati necessari per l'arduino mouse
/****************************************************************************
* Copyright © 2015 Alessandro Spallina
* email: alessandrospallina1@gmail.com
* website: aleksnote.altervista.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
****************************************************************************/
const int buttons[]={6,5,4,3,2}; //*click-sx-su-giu-dx*
const int potentiometer=A0;
const char *names[]={ "click", "sx", "su", "giu", "dx"};
int pixelsmovement=10;
int states[]={0,0,0,0,0};
void printFinalMessage(const char *button)
{
int value;
value=analogRead(potentiometer);
Serial.print(button);
Serial.print(" ");
Serial.println(pixelsmovement);
delay(value);
}
void getPixelsMovement(void)
{
if(Serial.available()>0)
pixelsmovement=Serial.parseInt();
}
void setup() {
int i;
for(i=0;i<5;i++)
{
pinMode(buttons[i], INPUT);
}
pinMode(potentiometer, INPUT);
Serial.begin(9600);
}
void loop() {
int i;
getPixelsMovement();
for(i=0;i<5;i++)
{
states[i]=digitalRead(buttons[i]);
if(states[i]==1)
printFinalMessage(names[i]);
}
}
/****************************************************************************
* Copyright © 2015 Alessandro Spallina
* email: alessandrospallina1@gmail.com
* website: aleksnote.altervista.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SERIAL "9600"
void print_error(const char *msg)
{
fprintf(stderr, "\n\nERRORE: %s\n\n", msg);
exit(EXIT_FAILURE);
}
void verificaEsistenzaArduino(char *path)
{
char message[BUFSIZ];
FILE *fp=NULL;
fp=fopen(path, "r");
if(fp==NULL)
{
strcpy(message,"Non trovo: ");
strcat(message, path);
print_error(message);
}
fclose(fp);
}
void printUsage()
{
printf("Usage: amouses <<arduino tty path>>\nExample: \"amouses /dev/ttyUSB0\"\n");
exit(EXIT_FAILURE);
}
void usageController(int argc, char *path)
{
if(argc!=2)
printUsage();
verificaEsistenzaArduino(path);
printf("# UsageController: OK #\n");
}
int main(int argc, char *argv[])
{
FILE *fp=NULL;
FILE *pipe=NULL;
char buf[BUFSIZ];
int numpix;
int i;
int coord[2];
char temp[BUFSIZ];
usageController(argc, argv[1]);
strcpy(temp, "cu -l ");
strcat(temp, argv[1]);
strcat(temp, " -s 9600");
fp=popen(temp, "r");
while(fscanf(fp, "%s %d", buf, &numpix)==2)
{
printf("Letto: %s %d\n", buf, numpix);
pipe=popen("xdotool getmouselocation", "r");
fscanf(pipe, "x:%d y:%d",&coord[0],&coord[1]);
fclose(pipe);
printf("Coord attuali -> x: %d y: %d\n", coord[0], coord[1]);
if(strcmp(buf, "click")==0)
system("xdotool click 1");
if(strcmp(buf, "su")==0)
{
coord[1]-=numpix;
printf("Coord attuali -> x: %d y: %d\n", coord[0], coord[1]);
strcpy(buf, "xdotool mousemove ");
sprintf(temp, "%d", coord[0]);
strcat(buf, temp);
strcat(buf, " ");
sprintf(temp, "%d", coord[1]);
strcat(buf, temp);
system(buf);
}
if(strcmp(buf, "giu")==0)
{
coord[1]+=numpix;
printf("Coord attuali -> x: %d y: %d\n", coord[0], coord[1]);
strcpy(buf, "xdotool mousemove ");
sprintf(temp, "%d", coord[0]);
strcat(buf, temp);
strcat(buf, " ");
sprintf(temp, "%d", coord[1]);
strcat(buf, temp);
system(buf);
}
if(strcmp(buf, "sx")==0)
{
coord[0]-=numpix;
printf("Coord attuali -> x: %d y: %d\n", coord[0], coord[1]);
strcpy(buf, "xdotool mousemove ");
sprintf(temp, "%d", coord[0]);
strcat(buf, temp);
strcat(buf, " ");
sprintf(temp, "%d", coord[1]);
strcat(buf, temp);
system(buf);
}
if(strcmp(buf, "dx")==0)
{
coord[0]+=numpix;
printf("Coord attuali -> x: %d y: %d\n", coord[0], coord[1]);
strcpy(buf, "xdotool mousemove ");
sprintf(temp, "%d", coord[0]);
strcat(buf, temp);
strcat(buf, " ");
sprintf(temp, "%d", coord[1]);
strcat(buf, temp);
system(buf);
}
}
pclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment