myString = Serial.readStringUntil('\n'); // input eg:= pc_name,KeyToPress ie, pc,0x33
String device = getValue(myString, ',', 1); // extract 2nd value from myString ie 0x33
Serial.println("device="+device);
// some other if else code
char *key = device.c_str(); // converting string to char because lib i use can only accept char
Serial.print("extracted key:");
Serial.print(key);
Serial.println("");
keyboard.keyboard_mkbrk(key); // this function comes lib and accept char variable
output:-
input:pc,0x33
device=0x33
extracted key:0x33
60 // i put one serial.print inside lib function
to press 'h' , value of key must be PS2dev::H or 0x33
if i write this keyboard.keyboard_mkbrk(0x33); then its working fine
but if i pass the value then its not working
lib name is ps2dev [Preformatted text](https://github.com/Harvie/ps2dev/tree/master/src)
this is function inside lib
int PS2dev::keyboard_mkbrk(unsigned char code)
{
Serial.println(code); // 60
write(code);
write(0xF0);
write(code);
return 0;
}
all i want is to get comma separated text from serial connection
and pass it into keyboard function.
pls help m stuck here for 24 hrs+
my enitre code
#include <ps2dev.h>
PS2dev keyboard(6, 5); //clock, data
PS2dev mouse(3,2); // 2 data 3clock
unsigned long timecount = 0;
int scancodes_idx = 0;
char buttons[3] = {0,0,0}; // 0 left ,midde ,right
int delta_x = 0;
int delta_y = 0;
String myString = "Hello String";
//char key;
int enabled =1;
void setup() {
keyboard.keyboard_init();
Serial.begin(115200);
Serial.setTimeout(1);
unsigned char val;
Serial.println("start");
// send the mouse start up
while(mouse.write(0xAA)!=0);
while(mouse.write(0x00)!=0);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
//ack a host command
void ack() {
while(mouse.write(0xFA));
}
void write_packet() {
char overflowx =0;
char overflowy =0;
char data[3];
data[0] = ((overflowy & 1) << 7) |
( (overflowx & 1) << 6) |
( (((delta_y &0x100)>>8) & 1) << 5) |
( ( ((delta_x &0x100)>>8)& 1) << 4) |
( ( 1) << 3) |
( ( buttons[1] & 1) << 2) |
( ( buttons[2] & 1) << 1) |
( ( buttons[0] & 1) << 0) ;
data[1] = delta_x & 0xff;
data[2] = delta_y & 0xff;
mouse.write(data[0]);
mouse.write(data[1]);
mouse.write(data[2]);
delta_x = 0;
delta_y = 0;
}
int mousecommand(int command) {
unsigned char val;
//This implements enough mouse commands to get by, most of them are
//just acked without really doing anything
switch (command) {
case 0xFF: //reset
ack();
//the while loop lets us wait for the host to be ready
while(mouse.write(0xAA)!=0);
while(mouse.write(0x00)!=0);
break;
case 0xFE: //resend
ack();
break;
case 0xF6: //set defaults
//enter stream mode
ack();
break;
case 0xF5: //disable data reporting
//FM
ack();
break;
case 0xF4: //enable data reporting
//FM
enabled = 1;
ack();
break;
case 0xF3: //set sample rate
ack();
mouse.read(&val); // for now drop the new rate on the floor
// Serial.println(val,HEX);
ack();
break;
case 0xF2: //get device id
ack();
mouse.write(00);
break;
case 0xF0: //set remote mode
ack();
break;
case 0xEE: //set wrap mode
ack();
break;
case 0xEC: //reset wrap mode
ack();
break;
case 0xEB: //read data
ack();
write_packet();
break;
case 0xEA: //set stream mode
ack();
break;
case 0xE9: //status request
ack();
// send_status();
break;
case 0xE8: //set resolution
ack();
mouse.read(&val);
// Serial.println(val,HEX);
ack();
break;
case 0xE7: //set scaling 2:1
ack();
break;
case 0xE6: //set scaling 1:1
ack();
break;
}
}
int xcenter ;
int ycenter;
int xsum = 0;
int ysum = 0;
void loop() {
unsigned char leds;
unsigned char c;
if(keyboard.keyboard_handle(&leds)) {
//Serial.print('LEDS');
//Serial.print(leds, HEX);
digitalWrite(LED_BUILTIN, leds);
}
if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW)) {
while(mouse.read(&c)) ;
mousecommand(c);
}
if (Serial.available() > 0) {
myString = Serial.readStringUntil('\n');
Serial.println("input:"+myString);
// input "mouse,x,y"
String device = getValue(myString, ',', 1);
Serial.println("device="+device);
if (device == "mouse")
{
delta_x = getValue(myString, ',', 2).toInt();
delta_y = getValue(myString, ',', 3).toInt();
Serial.print("x=");
Serial.print(delta_x);
Serial.print("y=");
Serial.print(delta_y);
Serial.println(" ");
write_packet();
}
else if (device == "lclick")
{
int elta_x = getValue(myString, ',', 2).toInt();
buttons[elta_x] = 1 ;
write_packet();
buttons[elta_x] = 0 ;
delay(39);
write_packet();
}
else
{
delay(500);
char *key = device.c_str();
Serial.print("extracted key:");
Serial.print(key);
Serial.println("");
keyboard.keyboard_mkbrk(key); // to press 'h' value must be PS2dev::H or 0x33
//keyboard.keyboard_mkbrk(0x33); // to press 'h' value must be PS2dev::H or 0x33
//keyboard.keyboard_press(key);
//keyboard.keyboard_release(key);
}
}
}