Digistump Forums
The Digispark => Digispark Project Ideas => Topic started by: bobricius on February 06, 2013, 10:12:00 pm
-
Hi all, How I can send string or number to computer with Digikeyboard library? Somethink like
DigiKeyboard.write("Hello world")
-
You can't, I don't have my digispark yet so this code is untested for now but here's what I had in mind
void print(char *str, int strlen) {
for (int i=0; i<strlen; i++) {
if (str==' ')
DigiKeyboard.sendKeyStroke(KEY_SPACE);
if (str>='A' && str<='Z')
DigiKeyboard.sendKeyStroke(KEY_A+(str-'A'), MOD_SHIFT_LEFT);
if (str>='a' && str<='z')
DigiKeyboard.sendKeyStroke(KEY_A+(str-'a'));
if (str=='0')
DigiKeyboard.sendKeyStroke(KEY_0);
if (str>='1' && str<='9')
DigiKeyboard.sendKeyStroke(KEY_1+(str-'1'));
}
}
Add DigiKeyboard.sendKeyStroke(KEY_ENTER); at the end to have a println
-
Got them today !!!
Anyway this works
#include "DigiKeyboard.h"
void setup() {
}
void print(char *str) {
char c = str[0];
byte i = 0;
DigiKeyboard.update();
DigiKeyboard.sendKeyStroke(0); //this is generally not necessary but with some older systems it seems to prevent missing the first character after a delay
while (c != 0) {
if (c==' ')
DigiKeyboard.sendKeyStroke(KEY_SPACE);
if (c>='A' && c<='Z')
DigiKeyboard.sendKeyStroke(KEY_A+(c-'A'), MOD_SHIFT_LEFT);
if (c>='a' && c<='z')
DigiKeyboard.sendKeyStroke(KEY_A+(c-'a'));
if (c=='0')
DigiKeyboard.sendKeyStroke(KEY_0);
if (c>='1' && c<='9')
DigiKeyboard.sendKeyStroke(KEY_1+(c-'1'));
i++;
c = str[ i ];
}
}
void println (char *str) {
print(str);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
}
void loop() {
println ("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789");
delay(5000);
}
-
There seems to be an error on line 24 (c = str)
I think you meant to write c = str\[i\], right?
(I added '\' because the braces are automatically removed by the website)
-
Yes, don't know where it went when I pasted the code.
Edit:
Actually when I try to edit the post to correct the code, I see it correctly, it is probably a reserved markup, I'll try adding spaces
Edit2:
Yep that's ok now you can't type [ i ] (without spaces) on these forums, there was an [ \i ] at the end of the code too.
-
Thanks. It working great !
-
Got them today !!!
Anyway this works
#include "DigiKeyboard.h"
void setup() {
}
void print(char *str) {
.....
The empty setup() looks strange
-
In the next Digispark Arduino App update, you can just DigiKeyboard.write, DigiKeyboard.print and DigiKeyboard.println the same as a serial port or DigiUSB. It works via a lookup table and assumes your keyboard is configured as a US Keyboard. If you want to use it right now you can download the Digispark app mods at https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries (https://github.com/digistump/DigisparkArduinoIntegration/tree/master/libraries) and copy in the newer version of DigisparkKeyboard in to the arduino installation. Digistump are releasing a new version of the Digispark software soon though so if you can wait a day or two, just download the update with all the new goodies inside. It's going to be a pretty great update!
The write/print/println support in the new DigiKeyboard version support everything in ASCII which creates a visible character, and also horizontal tab, new line (LF), and backspace. Extended ASCII are not supported at all because keycodes to enter extended ascii vary wildly between different computer platforms and keyboard mappings. Other control characters are effectively skipped.
-
In the next Digispark Arduino App update, you can just DigiKeyboard.write, DigiKeyboard.print and DigiKeyboard.println the same as a serial port or DigiUSB.
......
Is a Digikeyboard.init() or another funciton for inclusion in setup() available?
What is the name of the USB initalisation (or setup) function?
I'm asking because I'm using the "deuxvis bootloader", which doesn't USB Init.
-
No digispark bootloader varient does anything to initialize the USB communications, and the code which boots up the digispark is the same in both versions so there should be no difference. It isn't working for you? Can you still upload sketches to it (with d5 connected to ground of course) to make sure the USB connection is working correctly?
-
I think that I just was lucky that my sketch did work with the original bootloader.
The vary same .hex did only work partly with the deuxvis bootloader.
It did crash some where near DigiKeyboard.sendKeyStroke
In setup() have I now DigiKeyboard.delay(100);
And in loop() I call DigiKeyboard.update();
Now I have a sketch with USB interaction that works with both bootloaders.
Groeten
Stappers
-
Oh yes, calling DigiKeyboard.update() or DigiKeyboard.delay() is not optional. Either of them should be called no less frequently than every 50 milliseconds! delay effectively calls update in a tight loop until the requested length of time has elapsed.
By the way, the new version of the DigisparkKeyboard library has been included in the now current release of the Digispark IDE (version 1.0.4), so go and print and println your strings! To heck with all the annoying functions in this thread ^_^
Check out the built in DigisparkKeyboard > Keyboard example for an updated much simpler way to send a hello world! Hopefully in the next update that whole sendKeyStroke(0) business will be handled behind the scenes too, making things even nicer. I'd like to see digispark include some mods to the
Digispark keeps getting less annoying and more useful <3