Author Topic: USB:DIGISPARK KEYBOARD : How to send strig ?  (Read 16868 times)

bobricius

  • Newbie
  • *
  • Posts: 49
USB:DIGISPARK KEYBOARD : How to send strig ?
« 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")

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #1 on: February 07, 2013, 05:13:12 am »
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

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #2 on: February 07, 2013, 02:04:05 pm »
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);
}
« Last Edit: February 08, 2013, 06:25:02 am by semicolo »

lstep

  • Newbie
  • *
  • Posts: 2
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #3 on: February 08, 2013, 02:14:09 am »
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)
« Last Edit: February 08, 2013, 02:17:45 am by lstep »

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #4 on: February 08, 2013, 06:22:39 am »
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.
« Last Edit: February 08, 2013, 06:27:30 am by semicolo »

bobricius

  • Newbie
  • *
  • Posts: 49
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #5 on: February 11, 2013, 03:53:34 am »
Thanks. It working great !

stappers

  • Newbie
  • *
  • Posts: 8
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #6 on: March 16, 2013, 11:31:19 am »
Got them today !!!

Anyway this works

#include "DigiKeyboard.h"

void setup() {

}

void print(char *str) {
   .....

The empty setup()  looks strange

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #7 on: March 16, 2013, 02:42:06 pm »
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 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.
« Last Edit: March 16, 2013, 02:47:42 pm by Bluebie »

stappers

  • Newbie
  • *
  • Posts: 8
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #8 on: March 16, 2013, 03:14:35 pm »
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.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #9 on: March 16, 2013, 04:41:01 pm »
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?

stappers

  • Newbie
  • *
  • Posts: 8
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #10 on: March 23, 2013, 05:22:56 am »
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


Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: USB:DIGISPARK KEYBOARD : How to send strig ?
« Reply #11 on: March 23, 2013, 07:13:50 am »
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