Author Topic: Tranquilite's USB and Bluetooth Keyboard  (Read 4114 times)

0 Members and 1 Guest are viewing this topic.

Offline Tranquilite

  • Thread Starter
  • Posts: 144
Tranquilite's USB and Bluetooth Keyboard
« on: Thu, 08 May 2014, 16:41:23 »
For my computer engineering senior design project, I decided to make a keyboard, and now that I am graduated and have some time, I figure I'd share.

So for starters, I decided to go with 16x6 hand wired matrix (the 17th "column" was transposed into empty spots in other rows columns so there's a bit of funky wiring there"
64087-064089-164091-2
While the hand-wired matrix is pretty standard fare for do-it-yourself-keyboards, I feel this project brings a number of new and interesting features to the table, including:
  • 8MHz operation at 3.3V for reduced power consumption and better logic compatibility with the bluetooth module
  • USB or battery powered with 1N5817 Diodes to prevent current flowing in the wrong direction
  • Seamless switching between USB and Bluetooth via interrupts
  • 74HC4514 and 74HC251 logic ICs for reduced IO pin usage (8 pins to control up to a 16x8 matrix)
Running the Teensy at 3.3V AND with battery power was a bit tricky as I had to separate VBUS and VCC, and then run VBUS through the onboard regulator of the Bluetooth module, and then run the 3.3v from the Bluetooth module back into the teensy's VCC. This was the only way i could think of to have USB and battery power, AND also run the teensy at 3.3V because I needed diodes on the battery and on the USB VBUS connection so I could switch between these sources without worrying about current flowing the wrong way.

Also, I made use of the USB connection transition interrupt to switch between communicating via bluetooth or USB whenever the USB cable is plugged or unplugged.

Anothing thing to expound upon was using some basic logic ICs to reduce IO pin usage for controlling the switch matrix. The big pin-saver was the 74HC4514 4 to 16 decoder which I used to drive all 16 columns using just 4 IO pins. I'm definitely going to be using this chip in future keyboard projects because it is easy to use and saves a ton of IO pins. I also used a 74HC251 3 to 8 mux for selecting which row I wanted to poll. This one did not result in as big of savings because I used 4 IO pins (three select lines, and mux output) to poll 6 rows. I decided to use it anyway because this allowed me to use 8 IO pins to scan which meant i could do all matrix-related stuff using a single IO port on the teensy.

All-in-all this was a really fun project, but there is still room for improvement. While the Adafruit EZ-key is a pretty nice bluetooth module to work with, I find that there were a few things that were really annoying to deal with. The first thing has to do with getting keyboard status indicator information. There's no way manually requesting that information and when the EZ-key does send status indicators it does so in the form of a relatively long ASCII string that needs to be parsed. Also, the EZ-key has no good way of putting the module to sleep, as using the reset pin takes about 3 seconds for the EZ-key to come back online. I've ordered a RN42HID which I am pretty sure is more configurable, so we will see how things go from there.

Anyway, I've uploaded my code as well as some sparse documentation to github: https://github.com/Tranquilite0/hybrid-keyboard

Offline bcg

  • Posts: 112
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #1 on: Thu, 08 May 2014, 23:34:33 »
This is definitely awesome!

I came to very similar conclusions as you with my project (http://geekhack.org/index.php?topic=58015.0), but my strategy was to evade the USB/battery complications and just run off of a lipo battery all the time.  I was using an Arduino Micro which is a 5V/16MHz board so technically I was overclocking because I used the 3.7V from the battery directly for all the components.  I haven't had any problems with that so far but I'm using it as a converter not to process a whole matrix so I'm sure its not as intensive as your application.  I didn't even try to do USB switching because I didn't really have a ton of time to spend on it, I might revisit that and try your solution tho.

I'm interested in knowing how it works out with the RN42HID... I looked at the datasheet a while back and the protocol looked like it was the same as sending HID reports to Bluefruit so to a certain degree it might be a drop in replacement.  You'll need to add a 3.3V regulator though if you want to stick with a similar configuration.  Or, because of your use of shift registers you could probably use an Arduino Pro Micro 3.3V which is essentially the same thing as Teensy 2.0 but it comes as a 8Mhz board out of the box and has an onboard 3.3V regulator and it is about half the price of a Teensy, even if you get a genuine one from Sparkfun.  Even cheaper if you get one from China... but it is definitely cheaper in quality.

Great project!
:wq!

Offline SpAmRaY

  • NOT a Moderator
  • * Certified Spammer
  • Posts: 14667
  • Location: ¯\(°_o)/¯
  • because reasons.......
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #2 on: Thu, 08 May 2014, 23:38:12 »
Thanks for the write up, I gotta admit I'm a complete noob when it comes to electronics, but I'd love to see more teensy powered bluetooth keyboards!!

Offline Tranquilite

  • Thread Starter
  • Posts: 144
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #3 on: Fri, 09 May 2014, 02:04:38 »
I was looking at those pro micros, and it definitely seems to be the right choice, especially because I went through the trouble to save IO pins. Assuming I don't re-use a teensy for my next project, I will probably pick up one of those. Eventually I would like to design a PCB for a bluetooth oriented keyboard, but that will be a project for another time.

Offline Smasher816

  • HHKB Master
  • Posts: 538
  • Location: return STATE_MISSOURI;
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #4 on: Thu, 22 May 2014, 17:04:30 »
Very interesting project. I have seen other bluetooth mods before but never one that managed to take care of switching between bluetooth and usb.

How difficult do you think this would be to adopt to a different keyboard? I already have a teensy wired up to my HHKB's matrix :)

Because the case is fairly small I think I would also use a LiPo battery and charger instead. Hopefully the ideas carry over fine.

I really wish a company would come out with a bluetooth controller that could handle arbitrary sized hid reports (nkro). Ohh well, 6 keys is good enough.

Offline Tranquilite

  • Thread Starter
  • Posts: 144
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #5 on: Sun, 25 May 2014, 17:47:51 »
How difficult do you think this would be to adopt to a different keyboard? I already have a teensy wired up to my HHKB's matrix :)
If you want to use the firmware I wrote, then you merely have to rewrite the matrix scanning function to work with the way you have your matrix setup. If you want to add USB connection transition detection to a different firmware which utilizes the atmega32u4 (and similar AVR's too I imagine), then the process is fairly simple.

1. Set the VBUSTE flag in the USBCON register:
Code: [Select]
USBCON |= (1<<VBUSTE)Note that if you are using the PJRC stack you will need to modify the USB_CONFIG() and USB_FREEZE() to have the VBUSTE flag or'd in.

2. Place the following code inside of the USB_gen_vect ISR subroutine:
Code: [Select]
//USB bus connection transition detection
if(USBINT & (1<<VBUSTI)) //Check flag
{
USBINT &= ~(1<<VBUSTI); //Clear flag
//Service interupt
if(USBSTA & (1<<VBUS)) //Check connection state
{
//just plugged in
usb_init(); // Enable USB
bluefruit_disable( ); //Disable bluefruit
}
else
{
//just unplugged
usb_disable();
bluefruit_init(); //enable bluefruit
}
}

Then whenever you decide to send a keyboard report, you can use whichever system is currently active.
If you don't want to worry about interrupt programming, you could also just poll the VBUS flag inside of the USBSTA register to determine the USB connection state. I particularly like the interrupt version of checking USB connection state though because you can disable your microcontroller's USB for fairly significant power savings while running on battery.

Offline Nickelbawker

  • Posts: 12
Re: Tranquilite's USB and Bluetooth Keyboard
« Reply #6 on: Sat, 13 September 2014, 17:09:36 »
Oh man this keyboard is amazing. makes me want to make a wooden case keyboard similar to Poker, Pure, Poker II, or Filco Minila air. That's so sweet. Wonder how hard it would be to make a function layer instead of having arrow keys and stuff.