USB Host - Keyboard to ASCII Converter code
Luis_P
Posts: 246
Any help highly appreciated.
I have one of this USB host that I want to use with the Stamp2:
http://www.hobbytronics.co.uk/usb-host/usb-host-keyboard
Problem? I could not figure out the code. The website offers only an Arduino but guest what? I have no idea of how to convert it.
I used this but I can only received one key entry from the keyboard:
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600 CON 84
#ENDSELECT
letter VAR Byte
Main:
'SEROUT 1,84, ["KEYBOARD",0] 'send keyboard country code
DO
SERIN 0,84, [letter]
DEBUG SHEX letter ' display on screen
PAUSE 1000 ' wait one second
LOOP ' repeat forever
END
'
Now this is the ARDUINO code:
Example Arduino program to Read Using I2C
/*
** Wire Master Reader of Hobbytronics USB Host Keyboard
** Created 24 Oct 2013
**
** This example code is in the public domain.
** www.hobbytronics.co.uk
*/
#include <Wire.h>
const int adc_address=41; // I2C Address
char keyboard_data[80]; // Array to store keyboard values
void setup()
{
//Send settings to ADC_I2C device (optional)
Wire.begin(); // join i2c bus (address optional for master)
//Start Serial port
Serial.begin(9600); // start serial for output
}
void loop()
{
unsigned char i;
char keyboard_chars;
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(1); // ask how many characters are in buffer
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, 1); // request 1 bytes from slave device
while(Wire.available())
{
keyboard_chars = Wire.read();
}
Serial.println(keyboard_chars, DEC); // print number of buffer characters
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(0); // get keyboard characters
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, keyboard_chars); // request characters from slave device
i=0;
while(Wire.available())
{
keyboard_data[i++] = Wire.read();
keyboard_data = '\0';
}
Serial.println(keyboard_data); // print the character
keyboard_data[0]='\0';
delay(5000); // Wait 5 seconds
}
PLEASE HELP!
I have one of this USB host that I want to use with the Stamp2:
http://www.hobbytronics.co.uk/usb-host/usb-host-keyboard
Problem? I could not figure out the code. The website offers only an Arduino but guest what? I have no idea of how to convert it.
I used this but I can only received one key entry from the keyboard:
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600 CON 84
#ENDSELECT
letter VAR Byte
Main:
'SEROUT 1,84, ["KEYBOARD",0] 'send keyboard country code
DO
SERIN 0,84, [letter]
DEBUG SHEX letter ' display on screen
PAUSE 1000 ' wait one second
LOOP ' repeat forever
END
'
Now this is the ARDUINO code:
Example Arduino program to Read Using I2C
/*
** Wire Master Reader of Hobbytronics USB Host Keyboard
** Created 24 Oct 2013
**
** This example code is in the public domain.
** www.hobbytronics.co.uk
*/
#include <Wire.h>
const int adc_address=41; // I2C Address
char keyboard_data[80]; // Array to store keyboard values
void setup()
{
//Send settings to ADC_I2C device (optional)
Wire.begin(); // join i2c bus (address optional for master)
//Start Serial port
Serial.begin(9600); // start serial for output
}
void loop()
{
unsigned char i;
char keyboard_chars;
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(1); // ask how many characters are in buffer
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, 1); // request 1 bytes from slave device
while(Wire.available())
{
keyboard_chars = Wire.read();
}
Serial.println(keyboard_chars, DEC); // print number of buffer characters
Wire.beginTransmission(adc_address); // transmit to device
Wire.write(0); // get keyboard characters
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(adc_address, keyboard_chars); // request characters from slave device
i=0;
while(Wire.available())
{
keyboard_data[i++] = Wire.read();
keyboard_data = '\0';
}
Serial.println(keyboard_data); // print the character
keyboard_data[0]='\0';
delay(5000); // Wait 5 seconds
}
PLEASE HELP!
Comments