Author Topic: DigiKeyboard and Bluetooth.  (Read 14018 times)

FigNewton

  • Newbie
  • *
  • Posts: 8
DigiKeyboard and Bluetooth.
« on: January 26, 2016, 05:25:30 pm »
(Dr. Nick Riveria voice) Hey Evelybody..... (/Dr. Nick Riveria voice)

So I am trying to connect a bluetooth HC-05 to a Digispark. I have tested and set up the HC-05 with an Uno, and wrote an Android app to connect to the bluetooth and then send a string to the bluetooth, which then printed out over the serial connection to my computer screen. What I am wanting to do with the Digispark is to receive this bluetooth string, and then DigiKeyboard.print() it. However I cannot get the code to compile. I know the HC-05 is working and I know my Digispark is working as I loaded the blink sketch to it and it's happy. Back to the code. I am looking all around and I think there is some kind of issue I am running into with the DigiKeyboard and the SoftSerial libraries but for the life of me cannot figure this out. Here is my code, and please forgive me as this is not even in a beta state yet. No comments and I have stripped out anything that didn't have anything to do with this simple pass of a string.

Code: [Select]
#include <TinyPinChange.h>
#include <SoftSerial.h>
#include "DigiKeyboard.h"

SoftSerial BTSerial(2, 3); // RX, TX

String blueString = "";
boolean blueSent = false;

void setup() {
  BTSerial.begin(38400);
  BTSerial.print("AT\r\n"); //Check Status
  delay(500);
  while (BTSerial.available()) {
    //Serial.write(BTSerial.read());
  }
}

void loop() {
  delay(10);
  DigiKeyboard.update();
  while (BTSerial.available()) {
    blueString = BTSerial.read();
  }
  if (blueString.length() > 0) {
    //BTSerial.flush(); //Not sure if this will be needed or not.
    DigiKeyboard.print(blueString);
    blueString = "";
  }


}

And now, here is the error I am getting compiling. I am including the warnings in case they actually are something I should pay attention to, but I think the meat of my issue is below that, but for the life of me, I can't figure out where __vector_2 is.

Code: [Select]
Arduino: 1.6.6 (Linux), Board: "Digispark (Default - 16.5mhz)"

Warning: platform.txt from core 'Digistump AVR Boards' contains deprecated recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{build.path}/{archive_file}" "{object_file}", automatically converted to recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{archive_file_path}" "{object_file}". Consider upgrading this core.
Warning: platform.txt from core 'Digistump AVR Boards' contains deprecated recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm, automatically converted to recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{archive_file_path}" "-L{build.path}" -lm. Consider upgrading this core.
WARNING: Category '' in library SPI is not valid. Setting to 'Uncategorized'
libraries/DigisparkTinyPinChange/TinyPinChange.cpp.o: In function `__vector_2':
/root/.arduino15/packages/digistump/hardware/avr/1.6.5/libraries/DigisparkTinyPinChange/TinyPinChange.cpp:57: multiple definition of `__vector_2'
libraries/DigisparkKeyboard/usbdrvasm.S.o:/root/.arduino15/packages/digistump/hardware/avr/1.6.5/libraries/DigisparkKeyboard/usbdrvasm165.inc:41: first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

If it would help if I post the verbose compile message, I will, but didn't want to chew up an entire screen on my very first post here. Thanks in advance for any help. I plan to post this all as a project when I get it running.

EDIT: So if I comment out the DigiKeyboard include and the corresponding lines, it will compile. Definitely a conflict here. And I have looked at line 57 of  TinyPinChange.cpp, and line 41 of usbdrvasm165.inc and cannot see anything similar. I am lost at this point.
« Last Edit: January 26, 2016, 05:48:43 pm by FigNewton »

FigNewton

  • Newbie
  • *
  • Posts: 8
Re: DigiKeyboard and Bluetooth.
« Reply #1 on: January 26, 2016, 10:43:44 pm »
Great news.  ;D  I was able to get this to work using this library and instructions on this page:

https://github.com/J-Rios/Digispark_DigiUSB-SoftSerial

In the interest of sharing, here is my code. I am testing it now, and so far it's working beautifully.

Code: [Select]
//DigiKey_BlueTooth_PushPaste
//By FigNewton
//Jan 27, 2016
//This takes the input from the connected bluetooth module and
//re-types it to the computer the DigiSpark is connected to.

#include <TinyPinChange.h>
#include <SoftSerial.h>
#include <DigiKeyboard.h>

SoftSerial BTSerial(0, 1); // RX, TX

void setup() {
  DigiKeyboard.update();
  BTSerial.begin(38400);
}

void loop() {
  delay(100);
  DigiKeyboard.update();
  while (BTSerial.available()) {
    DigiKeyboard.write(BTSerial.read());
  }
}

I am pretty pumped. I can type into my Android app that's connected to the bluetooth module, and the DigiSpark re-types it onto the computer. Thank you J-Rios!

FigNewton

  • Newbie
  • *
  • Posts: 8
Re: DigiKeyboard and Bluetooth.
« Reply #2 on: January 29, 2016, 09:08:21 am »
While that is disappointing to hear, I don't have any other ideas to make this work. If anyone is willing to work on this, I would be happy to test it out. It might be an ugly hack, but it seems to be working for me. I don't know enough about the underlying library to make it pretty.

Any chance that we could get the official digistump libraries updated with these fixes? I agree with you, I would much rather have this just work with the official library and not have to kludge this together.

Side note: I even managed to get it running without having to run it as root. The compile log I pasted above I was still having to use root to get the DigiSpark updated. It's working fine now without root for me after adding the udev rules per the Linux Troubleshooting page and then rebooting.
« Last Edit: January 29, 2016, 09:14:19 am by FigNewton »

FigNewton

  • Newbie
  • *
  • Posts: 8
Re: DigiKeyboard and Bluetooth.
« Reply #3 on: January 29, 2016, 12:43:12 pm »
For further use of the Digispark USB libs you shoud reinstall the original ones again. Prior to that you might use the export function of the Arduino IDE, which gives you the hex file of your sketch so that you may program another Digispark with the hex file without the need of compiling again.

I will definitely do that. But this puts my plan to document this little project into a questionable state. I would hate to publish a project that has the potential for messing up someone's ide by overwriting the current library. What I have is an Android app that you connect to a bluetooth module (I am using an HC-05). Once connected you can put text into a textbox on the app and click a 'send' button. The bluetooth input is read by the digispark and is then typed to the computer. What I will be using this for is KeePassDroid. I can then plug this into my computer and when a password is required I can open my phone, drop the password into the textbox and click send and it will be typed in on the computer. This means that if I use another computer I can have my password typed out without fear of someone having a keystroke logger attached to the keyboard, or having them watch me type it. Also reduces the risk of me fat fingering the keyboard. I can also just keep and maintain my passwords on my phone and know that I can pass them over to any computer with the digispark plugged in. It's really a neat process and I think could make security a lot easier for people.

But I don't want to put out a project that requires people to hack up a library. I think with the proliferation of cheap bluetooth modules, it might be best for this to get sorted out on a default library level. Would it be feasible to have an addition to the DigiKeyboard library, something like DigiKeyboardInt2, or maybe the SoftSerial and TinyPinChange to SoftSerialInt2, TinyPinChangeInt2? That way we could use the same library, but pick which interrupt we wanted to move which piece off to? I don't know how J-Rios did it as I haven't had time to study it, but since it's working, it's obviously possible. The question is, how do we get the fixes merged in and playing right with the default library?

It looks like digistump (Erik) on github has the main default library (https://github.com/digistump/DigistumpArduino). Is he still providing support for this? The other option is to open an issue on github with J-Rios' library and ask him politely to please rename so that both libraries can co-mingle.
« Last Edit: January 29, 2016, 12:45:11 pm by FigNewton »

JRios

  • Newbie
  • *
  • Posts: 9
Re: DigiKeyboard and Bluetooth.
« Reply #4 on: February 05, 2016, 02:02:52 pm »
Hi,

I just found this post the other day and I am happy to see that DigiUSB-SoftSerial (digistump modification) was helpful for someone.

Ralph you are so right, that "hack" was too ugly  :P

It is too annoying fight with the PCINT conflict of the digiUSB and SoftSerial libraries. The conclusion of my research was that, all the PCINTx pins (corresponding to an unique PCINT0, port B) are handled by just one vector ('vector__2'), so if that two libraries have declared the PCINT the only thing that we can do is to change the behaviour of any of them. The best thing that blow in my mind was to use the hardware INT0, but maybe there is a good way to get the same functionality of any of the libraries without interrupts.

Thanks to yours comments (FigNewton & Ralph), I have developed another (I think better) solution, a SoftSerial-INT0 library. With this one, you don't have to modify the digistump library and, of course, forget the P2-P3 shortcircuit.

SoftSerial-INT0: https://github.com/J-Rios/Digispark_SoftSerial-INT0

JRios

  • Newbie
  • *
  • Posts: 9
Re: DigiKeyboard and Bluetooth.
« Reply #5 on: February 05, 2016, 03:31:48 pm »
Yes, it works  :D

However it's true that the INT0 have a higher priority. I think that for the most of the applications it will works well, note that the UART just need the interrupt in the reception pin and it is not all the time getting data. The only thing we have to take care, is from sending a lot of data continuosly at higher speed without control in the controller system ;)

FigNewton

  • Newbie
  • *
  • Posts: 8
Re: DigiKeyboard and Bluetooth.
« Reply #6 on: February 06, 2016, 07:33:28 pm »
JRios,
That is awesome! I will grab the new library and test it out. Right now I have had your old library working just fine on my digispark+bluetooth module. It's been working since I posted this as I test it quite a bit. I have some updates running on my laptop and don't want to make any changes at the moment, but will grab the new library and recompile my code.
Again, thanks!
Fig

hmeijdam

  • Newbie
  • *
  • Posts: 3
Re: DigiKeyboard and Bluetooth.
« Reply #7 on: February 07, 2016, 10:30:04 am »
This looks interesting. I am attempting to read 4 PWM channels via the RCPulseIn library and then use DigiJoystick to translate that into a HID game controller.
Reading works fine, Joystick works fine but .....just found out that combined together is resulting in the Vector_2 clash.  :-[

So probably I will have to resolder and swap PB2 and PB3 connections on the board or I am one input pin short. Or is that no longer needed with the Digispark_SoftSerial-INT0 library update.

hmeijdam

  • Newbie
  • *
  • Posts: 3
Re: DigiKeyboard and Bluetooth.
« Reply #8 on: February 07, 2016, 11:23:27 am »
Just tested the new Digispark_SoftSerial-INT0 library from JRios, but that did not give me any luck.

His old solution however worked for me and gave me 3 input channels and a HID joystick. I lost my 4th channel as PB2 and PB3 had to be connected together.

I had to go back to a portable installation of Arduino 1.5.8 to install it, but now the code compiles without issues and it works fine. I guess I need to swap PB2 and PB3 on the Digistump through cutting the traces and soldering two cross wires and hope to have my lost channel back.

Thanks JRios !

FYI the code I used
Code: [Select]
/*
4 channel Digispark Flightsimulator HID Joystick, based on RC receiver PWM input.
Required Hacks to make it work:
Compiles with patched Arduino 1.5.8 portable installation.
Needs the patch to use different interrupt https://github.com/J-Rios/Digispark_DigiUSB-SoftSerial
Needs Digispark with disabled reset pin PB5 to allow for 4th channel.
to-do:  figure out how to get the 4th channel working again, now commented out.
Hans
email me : http://scr.im/hmeijdam
*/

//Non-Blocking servo library SoftRc
#include <SoftRcPulseIn.h>
#include <RcRxPop.h>
#include <TinyPinChange.h> /* Needed for <SoftRcPulseIn> library */

// digistump joystick library
#include "DigiJoystick.h"

//declaration of pins
#define CHANNEL1_PIN   0
#define CHANNEL2_PIN   1
//#define CHANNEL3_PIN   2
#define CHANNEL4_PIN   5

//Create the SoftRCpulseInOut library objects
SoftRcPulseIn  Channel_1_Pulse;
SoftRcPulseIn  Channel_2_Pulse;
//SoftRcPulseIn  Channel_3_Pulse;
SoftRcPulseIn  Channel_4_Pulse;

// RC channel position values
byte chan1val;
byte chan2val;
//byte chan3val;
byte chan4val;

void setup()
{
  Channel_1_Pulse.attach(CHANNEL1_PIN);
  Channel_2_Pulse.attach(CHANNEL2_PIN);
//  Channel_3_Pulse.attach(CHANNEL3_PIN);
  Channel_4_Pulse.attach(CHANNEL4_PIN);
}

void loop()
{
chan1val=map(Channel_1_Pulse.width_us(), 700, 2300, 0, 255);
chan2val=map(Channel_2_Pulse.width_us(), 700, 2300, 0, 255);
//chan3val=map(Channel_3_Pulse.width_us(), 700, 2300, 0, 255);
chan4val=map(Channel_4_Pulse.width_us(), 700, 2300, 0, 255);
 
  DigiJoystick.setX((byte) (chan1val));
  DigiJoystick.setY((byte) (chan2val));
//  DigiJoystick.setXROT((byte) (chan3val));
  DigiJoystick.setYROT((byte) (chan4val));

  // Call Joystick.move
  DigiJoystick.delay(10); // wait 10 milliseconds (tbd investigate if this is blocking)
}

« Last Edit: February 07, 2016, 11:31:46 am by hmeijdam »

JRios

  • Newbie
  • *
  • Posts: 9
Re: DigiKeyboard and Bluetooth.
« Reply #9 on: February 07, 2016, 11:46:40 am »
Hi hmeijdam,

Yes, the last solution is just for turn compatibles the Software Serial library with the HID functionality. What you need is a SoftRcPulseIn_INT0 library  :-\  maybe in the future...

Of course, the older solution "Digispark_DigiUSB-SoftSerial", give you the way to do what you want  ;)

hmeijdam

  • Newbie
  • *
  • Posts: 3
Re: DigiKeyboard and Bluetooth.
« Reply #10 on: February 07, 2016, 01:42:41 pm »
No worries, this is just playing around with these tinies.
Btw,......Swapping the PB2 and PB3 around as per test in below image, where I fold the pins up for the cross wires it did not bring back my fourth channel. PB2 and PB3 really need to kept tied together.
I thought PB3 was not used now by USB anymore, but as soon as I try to use PB3 for something else the USB device does not get recognized.


FigNewton

  • Newbie
  • *
  • Posts: 8
Re: DigiKeyboard and Bluetooth.
« Reply #11 on: February 08, 2016, 10:26:24 am »
JRios,
I just wanted to update. I put in the new SoftSerial_UINT0 library, and modified my code to include it, and also modified the rx/tx to match your example. I had to remove the jumper from pins 2 and 3, and plugged in the appropriate pins to 1 and 2, and voila. It's working like a charm. Thanks for the update. This is really some cool technology that is going to make my life a lot easier.

With my bluetooth connected to the digispark, and the Android app I wrote, I now have hot keys that I can punch on my phone screen, or a tablet it I wanted, that will type in common commands I use. Some of these I type in a lot during work, and with this it will mean that I can just pull up a little command window and push a button.  ;D
Again thanks!!

JRios

  • Newbie
  • *
  • Posts: 9
Re: DigiKeyboard and Bluetooth.
« Reply #12 on: February 08, 2016, 12:40:49 pm »
It's working like a charm. Thanks for the update.

You are welcome Fig, I am glad to hear it.

I think that bring wireless connectivity to digispark is great to complement the HID-USB functionality.

Regards.

WhamBham

  • Newbie
  • *
  • Posts: 1
Re: DigiKeyboard and Bluetooth.
« Reply #13 on: April 28, 2017, 03:15:18 am »
Sorry to bring back a dead thread , but am i correct in assuming SoftSerial_INT0.h library will only work with DigiSpark and not the pro as i get an error when i compile with the pro but not with Digispark

GIMSK' was not declared in this scope

GIMSK &= ~(1 << INT0);     // Disable INT0

JRios

  • Newbie
  • *
  • Posts: 9
Re: DigiKeyboard and Bluetooth.
« Reply #14 on: April 28, 2017, 07:26:49 am »
Hi, long time ago :D

Yes, you are right, "SoftSerial_INT0" library is for standard Digispark (Attiny85), It need to be modified to be use in Digispark Pro (Attiny167), because "General Interrupt Mask Register" (GIMR) and "MCU Control Register" (MCUCR) does not exists in Attiny167 microcontroller, instead, you have to use " External Interrupt Mask Register" (EIMR) and "External Interrupt Control Register A" (EICRA), you can check all of this in the Datasheets...

However, I think that use this library in Digispark Pro does not make too much sense, because Pro has more pins that can be used by normal "SoftwareSerial" library.

Regards.