Author Topic: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?  (Read 14868 times)

0 Members and 1 Guest are viewing this topic.

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« on: Thu, 22 September 2016, 13:49:21 »
Right, so i don't have too much of a problem with the hardware and construction part of making my keyboard...
Its this little doodad that turned up in the post called a Teensy 3.2 thats got me scratching my head! :confused:

I know this part is going to take me the longest to get my head round so i wanted to get the basics of the teensy underway before i have the keyboard ready to go!

At the moment i'm still tweaking the layout to suit my needs, but as it stands i'm looking 95% finalised and ready to order the mounting plate:

Colours are to reference my proposed switch matrices only (all switches will have diodes)
148943-0
I may yet loose the whole F-key row and swap 'Tiddle' for 'Esc'


So anyway...
My programming is very limited by today's standards. I did a good bit of programming back in the day but we are talking BASIC most of which i have now forgotten anyway! (probably a good thing as it used up a lot of space, lol)

I have been searching around a lot about this prior to deciding to build a custom keyboard, and it all seemed to be about the teensy that i was needing.

However i cant seem to find any detailed instructions for using the Teensy as a usb keyboard controller (in the total noob sense) its along the lines of 'wire your matrix to the teensy' err yea ok.... which terminals?
Write your code or use something like 'kiibohd controller' compile your code... etc, etc.

Basically everything i have found so far regarding this kind of assumes you know what your doing with the Teensy to start with!
I was kind of hoping that someone may take pity on me and give me a noob style crash course step by step guide? :-*

Finished i'm looking to achieve:
Working keyboard with an Fn layer
Direct macro keys (M1-M9) programmable direct from the keyboard
Indirect macro keys programmable direct from the keyboard and accessed via a 'program' type key (something like HacKeyboard)
R,G,B Led control direct from the keyboard (again like HacKeyboard's led controls)
Perhaps even throw in a usb hub so i can daisy-chain peripherals

As i kinda have zero knowledge of programming in C i'm not expecting to jump straight in and write my own software or have all the above working in one go, but rather start with some of the open source keyboard controllers like the 'Kiibohld' or 'TMK Keyboard' and as i progress add the non essential extra bits to it!


Offline Charger

  • Posts: 168
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #1 on: Thu, 22 September 2016, 14:27:59 »
its kind of the wrong teensy since most things have been done around the 2.0 you can find walkthroughs for that or the pro micro and firmware for them but they use AVRs not ARM chips like the 3.2 does

Offline Fictiouz

  • That "White" Guy
  • Posts: 465
  • Location: Halifax, NS, CA
  • "You miss 100% of the shots you take." -S. Trooper
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #2 on: Thu, 22 September 2016, 14:28:37 »
its kind of the wrong teensy since most things have been done around the 2.0 you can find walkthroughs for that or the pro micro and firmware for them but they use AVRs not ARM chips like the 3.2 does

I didn't want to be the one to break it to him.
Halifax, NS Meetup | "I know you'll come back to me, I'm like a good kind of herpes, I'm impossible to get rid of." - 27
67g Zilent V2 M60-A | BKE Redux HHKB Professional 2 | 62g Silent Black Planck | 62g Vint Black Tex Yoda II | Bolt-Modded Model M 1391301

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #3 on: Thu, 22 September 2016, 15:18:59 »
Bugger!

I knew something felt wrong....


[Edit]righttt, try again! Teensy 2.0 now in the post!
Didnt realise lol, what a spanner! :'( [/edit]
« Last Edit: Thu, 22 September 2016, 15:43:23 by mark.wood_uk »

Offline SpAmRaY

  • NOT a Moderator
  • * Certified Spammer
  • Posts: 14667
  • Location: ¯\(°_o)/¯
  • because reasons.......
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #4 on: Thu, 22 September 2016, 15:44:06 »
Just to be clear no teensy alone is going to do what that custom board you referenced can do.

And the 3.2 can be used in a keyboard it just isn't as friendly and doesn't have nearly the history the teensy 2.0 has.

For instance a quick google search turned this up https://github.com/kiibohd/controller/wiki/Custom-Keyboard-using-a-Teensy-3.0-3.1-3.2
« Last Edit: Thu, 22 September 2016, 15:46:18 by SpAmRaY »

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #5 on: Sun, 25 September 2016, 12:09:23 »
For instance a quick google search turned this up https://github.com/kiibohd/controller/wiki/Custom-Keyboard-using-a-Teensy-3.0-3.1-3.2
Im already aware of that, hence why i said i would probably use an open source firmware for my controller such as kiibohd's.

I chose the teensy 3.2 over the 2.0 for 2 main reasons:
Firstly it has more pins available than the 2.0. (i thought the extra pins would be useful for using a multiple rgb LED strip with addressable LEDs) Like these:http://www.ebay.co.uk/itm/WS2812B-5050-RGB-LED-Flexible-Strip-1M-30-60-144-LED-Individual-Addressable-5V-/401158142782?var=&hash=item5d66e37b3e:m:mQ1FTXFkKWHyQEkIPre04rQ
Secondly i found the 3.2 considerably easier to source for some reason!

With my initial post whilst i didn't say it outright i was actually talking about the kiibohld repository as i have spent considerable time trying to understand it properly (or at least well enough to use it)!
For a complete novice i have found it particularly hard to follow as it carries you along under the initial assumption that you already have a basic understanding of what you're doing but it also skips around a lot with sub category links for specific parts of the main tutorial which don't explain at what point to transition back to the main tutorial but continue on a tangent!

That said i have now managed to complete sections 'Get the source code ready and compiling' and 'Create a new scan module'
atm i cant continue with the kiibohld firmware until my switch panel arrives and i can solder up the matrix and solder that to the teensy!

I am aware that i will need to add in the partial maps for the mentioned macros using kll which again i have been reading about through kiibohld's repository. However i'm still sketchy on how i should go about adding these additional layers into the main script!

I'm sorry im asking decidedly stupid, noobish questions that you beleive can be answered with the use of google!
Im not opposed to doing the graft myself and i'm not looking for a free ride! The clearly noticeable part there is the length and knowledge i have already shown. If i wanted a free ride and i simply couldnt be bothered to look for myself my post would have gone something like this
" DIY keyboard help needed.
I want to make a keyboard, what do need and how do i do it?
Thanks"
Not a multiple paragraph first post to a topic showing some knowledge of what im doing, my capabilities and explaining my shortcomings that i am looking for help with!
I'm in no way slating Kiibohld's work, its probably the most in depth article i've managed to find! Its probably my age i'm just finding it a little too in depth at times and struggling to make sense of what to do from step to step!
The age thing will more than likely get to you too one day when you're trying to learn something complex that you have never done before!

Offline 0100010

  • Posts: 1127
  • Location: DFW, TX, US
  • Not Sure
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #6 on: Sun, 25 September 2016, 21:17:44 »
I think you'll find most of us are better at dealing with hardware than software (I know I am, and that is the feeling I have inferred from a lot of other folks here // this is not to speak for everyone though, lot of talented sw folks here too).  If it was me, and I wanted more pins than the normal Teensy 2.0, I would go with the Teensy++ (simply due the greater availability of keyboard firmware for the AVR chip on the older Teensy's).

But - I wish you luck with the newer ARM version of the Teensy!
  Quoting me causes a posting error that you need to ignore.

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #7 on: Mon, 26 September 2016, 13:19:30 »
Firstly, since your reply i did read back my last post and i do have to apologise!
At the time of writing it i didnt notice, but thats borderline 'major frikin rant' :eek: which was Not what i was wanting it to sound like!

Well I did figure out what my initial balls up was that prompted me to write my op, and it kinda went fairly smoothly after that tbh, lol!
Made a right noob mistake with terminal!
I was following the instructions that kiibohld had written to the letter but i hadn't downloaded the source using terminal, i'd downloaded from the website to a specified location.
So i was trying to use the terminal commands as listed by kiibohld such as "cd controller/Scan" and i should have been using "cd diykb/source/controller/Scan"
And i was sitting there pulling my hair out, shouting at the screen
"WTF, why cant you find it!! Its right F'ing THERE!!!" and other such profanities.
Then realised i wasn't in the right dir :-[

However i do know that i will get myself stuck later probably when i get to defining the DefaultMap.kll and again when it comes to adding in the partial layer .kll's. (2 parts that i'm finding particularly awkward to understand) but i will hold off posting until i know exactly what i'm stuck with rather than being so general!

Thank you for the supportive pep talk  :thumb: much appreciated duder   ;D
« Last Edit: Mon, 26 September 2016, 13:23:10 by mark.wood_uk »

Offline wolfv

  • Posts: 269
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #8 on: Thu, 29 September 2016, 01:12:26 »
Teensy 3.2 thats got me scratching my head! :confused:

I know this part is going to take me the longest to get my head round so i wanted to get the basics of the teensy underway before i have the keyboard ready to go!

My programming is very limited

Hi Mark,

The keybrd library series of tutorials is the easiest way to learn keyboard firmware.
 https://github.com/wolfv6/keybrd displays the README file.
 https://github.com/wolfv6/keybrd/tree/master/tutorials is a list to tutorials; start with tutorial_0_introduction.md

The tutorials use a Teensy LC, which is a less powerful version of Teensy 3.2.
You can use the Teensy 3.2 as a drop in replacement for the Teensy LC and the tutorial examples should just work.

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #9 on: Thu, 29 September 2016, 01:30:22 »
Cool, thanks  :thumb:
I hadnt come across that one in my searching, much appreciated  :D

Offline chuckdee

  • * Destiny Supporter
  • Posts: 1308
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #10 on: Thu, 29 September 2016, 09:25:56 »
Teensy 3.2 thats got me scratching my head! :confused:

I know this part is going to take me the longest to get my head round so i wanted to get the basics of the teensy underway before i have the keyboard ready to go!

My programming is very limited

Hi Mark,

The keybrd library series of tutorials is the easiest way to learn keyboard firmware.
 https://github.com/wolfv6/keybrd displays the README file.
 https://github.com/wolfv6/keybrd/tree/master/tutorials is a list to tutorials; start with tutorial_0_introduction.md

The tutorials use a Teensy LC, which is a less powerful version of Teensy 3.2.
You can use the Teensy 3.2 as a drop in replacement for the Teensy LC and the tutorial examples should just work.

That looks to be an excellent reference!  Thanks for posting that, and for your work on it!

Offline wolfv

  • Posts: 269
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #11 on: Sun, 02 October 2016, 02:32:38 »
You're welcome.  I hope the tutorials help many people learn keyboard electronics and firmware.
« Last Edit: Sun, 02 October 2016, 03:55:44 by wolfv »

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #12 on: Thu, 13 October 2016, 05:00:00 »
Hey again peeps,

well got my matrix all soldered up, (all switches, diodes and completed matrix has been metered out and everything is good from the hardware side)
got the teensy wired up,
got the basic code compiled to a hex and loaded onto the teensy all good!

I decided to go with a modified Planck keyboard as my basic 'tester' keyboard as the switches are in a matrix layout making it easier (and tidier) to solder up + i'm only dealing with 60 switches as well (57 keys as the 2u caps use 2 switches) whilst ive soldered up all 60 switches i was just going to leave one of the double'd up switches unassigned in the default map!

So here are pics of my keyboard, soldered Matrix and teensy for scrutiny:
150382-0150384-1150386-2
The switches have been fitted as if they were to be illuminated so in the matrix photo the Space is to the top of the pic.

Teensy:
Pins 0-11 = Columns
Pins 15-19 = Rows
and the config matrix type is set to 'pull_down' resistors which is how the default script is set, i have also tried changing to 'pull up' with no joy either!
It says that i can use any of the pins throughout most of the tutorial, however at the start of defining columns/rows there is a comment that says i need to check that the pins im using are suitable to be used as GPIO pins but i can't seem to find any info regarding that on teensy, git or google!

However something doesn't seem to be right as i seem to get no input at all from the keyboard.
According to Kiibohd's git it should be functioning, albeit not correctly as i haven't edited the default map as yet!

I think im not getting the columns and rows setup right in the 'matrix.h' and wanted to check if that's where i'm going wrong atm?

So atm i have:
Quote
// Define Rows (Sense) and Columns (Strobes)
GPIO_Pin Matrix_cols[] = { PTB16(B,16), PTB17(B,17), PTD0(D,0), PTA12(A,12), PTA13(A,13), PTD7(D,7), PTD4(D,4), (PTD,2), PTD3(D,3), PTC3(C,3), PTC4(C,4), PTC6(C,6) };
GPIO_Pin Matrix_rows[] = { PTC0(C,0), PTB0(B,0), PTB1(B,1), PTB3(B,3), PTB2(B,2) };

EDIT: The above format gives compilation errors (so thats not right)

I initially used this (with the same lack of result):
Quote
// Define Rows (Sense) and Columns (Strobes)
GPIO_Pin Matrix_cols[] = { gpio(PTB,16), gpio(PTB,17), gpio(PTD,0), gpio(PTA,12)... etc, etc };
GPIO_Pin Matrix_rows[] = same format again here!
EDIT: this compiles but there is nothing happening

and also the default 'example' type
Quote
GPIO_Pin Matrix_cols[] = { gpio(B,16), gpio(B,17), gpio(D,0), gpio(A,12)... etc, etc };
EDIT: this also compiles but again nothing seems to happen



EDIT: So here is a copy and paste of the script if this is any help?
Quote
// ----- Matrix Definition -----

// Freescale ARM MK20's support GPIO PTA, PTB, PTC, PTD and PTE 0..31
// Not all chips have access to all of these pins (most don't have 160 pins :P)
//
// NOTE:
// Before using a pin, make sure it supports being a GPIO *and* doesn't have a default pull-up/pull-down
// Checking this is completely on the ownness of the user

// MD1
//
// Columns (Strobe)
//  PTB0..3,16,17
//  PTC4,5
//  PTD0
//
// Rows (Sense)
//  PTD1..7

// Define Rows (Sense) and Columns (Strobes)
GPIO_Pin Matrix_cols[] = { gpio(B,16), gpio(B,17), gpio(D,0), gpio(A,12), gpio(A,13), gpio(D,7), gpio(D,4), gpio(D,2), gpio(D,3), gpio(C,3), gpio(C,4), gpio(C,6) };
GPIO_Pin Matrix_rows[] = { gpio(C,0), gpio(B,0), gpio(B,1), gpio(D,4), gpio(B,1), gpio(B,3), gpio(B,2) };

// Define type of scan matrix
Config Matrix_type = Config_Pulldown;
[/i]
« Last Edit: Thu, 13 October 2016, 12:24:34 by mark.wood_uk »

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #13 on: Fri, 28 October 2016, 02:20:37 »
Hi mark,
I'm DIY  keyboard of my own too. And your layout is very similar to mine. :))
151406-0
How is your progress now ?
I'm now trying to draw the PCB.
 151408-1

And I like your design of the "Copy/Paste" key. Do you have  any idea of how to implement it ?

Kevin,

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #14 on: Fri, 28 October 2016, 02:45:53 »
Hi yeti,
I was just going to use macro's to set the copy/paste keys (ctrl+c/ctrl+v), however I do believe there are already keycodes for HID copy/paste but don't quote me on that!

However as far as using the Teensy 3.2 that has gone out of the window and I have ordered 2.0's instead as there is a lot more support online and on GH for using that controller.

My kb is also hardwired rather than using a PCB. It's looking pretty good though, I'm a noon also but there are plenty of more experienced people on here willing to help if/when you get stuck with something

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #15 on: Fri, 28 October 2016, 10:38:51 »
hi mark,
Teensy2.0 is a good controller since it has more infomation online. But 3.2's is also good cause it's very easy to learn. I've read both document of them.
Maybe I'll update my kb' layout by replacing two keys into COPY/PASTE  ;D
I'm using the MacOS and I want to make it worked on both Windows and MacOS. I am going to check is there any way to implement on a lower level of the system.
I'll follow your project and if I have any progress I'll reply to you too.
Also if I got stuck with I'll let you know. Anyway , really appreciate to that.  :thumb:

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #16 on: Tue, 01 November 2016, 05:33:42 »
hi mark,
I realy got stuck by the controller these days. After drawing the PCB, I can not find an extra space to place the controller because my special layout.
Normally the controller is putted under the space key, but my space key just has 3x length.
I dont want to use a hardwired controller.
So far I prefer to choose abandoning the leveling mechanism on the PCB rather than extending the PCB size. And I'll try to use the  plate-mounted  cherry stabilizer.
Do you have any idea of this problem?

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #17 on: Tue, 01 November 2016, 06:07:42 »
Most simple solution is to make the case a little taller depending what you are making it from! If it's wood or acrylic you could route out a space for the controller in the base plate!
Like this:
151807-0
« Last Edit: Tue, 01 November 2016, 06:15:32 by mark.wood_uk »

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #18 on: Tue, 01 November 2016, 09:00:11 »
AHA  :)) really a good solution. but I dont want the controller out of the PCB.

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #19 on: Tue, 01 November 2016, 14:45:05 »
I don't do any PCB work at all, other than using veraboard and full size components, so forgive me if this sounds nieve...

Option 1: Mount the Teensy seperate to the main PCB,
Re-design the PCB adding a ribbon socket allowing the Teensy to be removable from the PCB. This would keep the form factor of the PCB the same.
Plus if all else fails you have a Teensy that you can just remove from the keyboard (no de-soldering needed) and transfer to a new PCB (plug and play style)

Option 2: mount the Teensy using a breakout board,
Redesign the PCB adding the breakout socket for the controller, the form factor of the PCB will need to change to allow additional space, if you have a built in wrist rest then this should allow for extending the PCB below the space bar to accommodate the controller.
Downside to making it a breakout is that you will need at minimum 10mm more depth to the case !

Option 2a. As above but solder Teensy directly to the PCB using header pins with stand offs. Probably get away with around 5mm-6mm extra depth that way.

Sorry if these ideas are no use... PCB and smd work is kinda beyond my expertise !
 I make electric guitars! I can build complex preamps but need to use breadboard and through board components as they need to be easily repairable or modifiable! Smd components are too small and fiddly to allow out of workshop (back stage at a gig) repairs to be made!

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #20 on: Tue, 01 November 2016, 21:01:56 »
hi, mark,
Really appreciate to the solutions.
Option 1 sounds amazing. A plug and play control device ? I like it !  Maybe I'll try to build a new one using this later.
For now I just remove the holes around the space key and place my controller there.
And I'm going to print my PCB this weekend. That will take about one week time.
I'd post the picture once they are done.

BTW: I changed my controller to a kimera core which has 32 pins instead of Teensy. 

Offline validifyed

  • Posts: 28
  • Location: England
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #21 on: Thu, 03 November 2016, 15:33:28 »
The good news, if your still stuck, is that Teensy 3.1 supports Arduino's programming suite. (Basically plug and play simple programming in a cut down version of C).

PJRC (The creators of the Teensy) have a tutorial on setting things up over on their website: http://www.pjrc.com/teensy/tutorial.html


As for the code, that one's on you, but here's the code from my current keyboard to get you started. Its not that pretty but it does the job. (My C was pretty out of practice when I wrote this thing)

Code: [Select]
//Working Keyboard v.1.2 +Multiple Modifier key support +function layer -mediakeys
#include <String.h>

/* Maps Teensy 3 pins to row and collumn numbers in keyboard matrix
 * Top -> Bottom
 * Left -> Right
 *
 * Collumn Skips are;
 * 17: OOOOX
 * 8: OOOXX
 * 9: OOXOO
*/
const byte ROWS = 6;
const byte COLLUMNS = 15;
const byte rowPins[ROWS] = {0, 4, 3, 12, 14, 15};
const byte collumnPins[COLLUMNS] = {23, 22, 21, 20, 19, 18, 17, 16, 0, 6, 7, 8, 9, 10, 11};

/* Map of keys on keyboard
 *  Top row is function layer
 *  Includes modifier keys
 *  EU key codes are given in HEX values
 *  uses HID keycode standard for keyboard input
 */
const char keyMap[6][15] = {
  {0, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, 0, 0},
  {KEY_ESC, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS, KEY_EQUAL, KEY_BACKSPACE, KEY_DELETE},
  {KEY_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_LEFT_BRACE, KEY_RIGHT_BRACE, 0x64, KEY_PRINTSCREEN},
  {KEY_CAPS_LOCK, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON, KEY_QUOTE, NULL, KEY_ENTER, KEY_PAGE_UP},
  {MODIFIERKEY_LEFT_SHIFT, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA, KEY_PERIOD, KEY_SLASH, NULL, KEY_RIGHT_SHIFT, KEY_UP, KEY_PAGE_DOWN},
  {KEY_LEFT_CTRL, KEY_LEFT_GUI, 0, KEY_LEFT_ALT, KEY_RIGHT_ALT, KEY_SPACE, NULL, 0, 0, 0x32, KEY_RIGHT_CTRL, NULL, KEY_LEFT, KEY_DOWN, KEY_RIGHT}
};

/* Key modifier map
 *  True if keyMap[Position] is KEY_MODIFIER
 */
bool modifierMap[6][15] = {
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
  {1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0}
};


/* Key pressed map
 *  Stores if they key is technically "Pressed"
 */
bool pressedMap[6][15] = {
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

/* Stores current "active" keys in array
 */
const int KEY_BUFFER_LENGTH = 6;
int activeKeys[KEY_BUFFER_LENGTH] = {0, 0, 0, 0, 0, 0};
char modifierKeys = 0;






/* Sets pin modes
 *  Rows to output
 *  - Skips row 0 (reserved for function layer)
 *  Collumns to pullup resistor inputs
 */
void setup() {
  Serial.begin(9600);
  for (int i = 1; i < ROWS; i++) { pinMode(rowPins[i], OUTPUT); }
  for (int i = 0; i < COLLUMNS; i++) { pinMode(collumnPins[i], INPUT_PULLUP); }
}







/* Ensures drain on all pins
 * Ends by setting drain open on selected pin
 */
void setDrain(int pinIndex) {
  for (int i = 0; i < COLLUMNS; i++) {
    digitalWrite(rowPins[i], HIGH);
  }
  digitalWrite(rowPins[pinIndex], LOW);
}

/* Sets key slots to match activeKeys array
 */
void updateKeys() {
  Keyboard.set_key1(activeKeys[5]);
  Keyboard.set_key2(activeKeys[4]);
  Keyboard.set_key3(activeKeys[3]);
  Keyboard.set_key4(activeKeys[2]);
  Keyboard.set_key5(activeKeys[1]);
  Keyboard.set_key6(activeKeys[0]);
}

/* Gets key, checks ifPressed and activeKeys index
 *  - Paths out modifier keys to be handled seperately
 *  - Switches to function key row when function key is held down
 *  Removed key if not pressed
 *  Attempts to add key if pressed
 */
bool sendKey(int i, int j) {
  bool pressed = (digitalRead(collumnPins[j]) == LOW);
  if (pressed && modifierMap[i][j]) {
    //Keyboard.set_modifier(keyMap[i][j]);
    modifierKeys = modifierKeys + keyMap[i][j];
    return true;
  }
 
  //Function Layer Activator
  if ((pressedMap[5][2] || pressedMap[5][8]) && i == 1) { i = 0; }
 
  if (pressed && !pressedMap[i][j]) { keyPress(i, j); return true; }
  if (!pressed && pressedMap[i][j]) { keyRelease(i, j); return true; }
  return false;
}

void keyPress(int i, int j) {
  pressedMap[i][j] = 1;

  //Add key to list
  for (int k = 0; k < KEY_BUFFER_LENGTH; k++) {
    if (activeKeys[k] == NULL) { activeKeys[k] = keyMap[i][j]; break; }
  }
}

void keyRelease(int i, int j) {
  pressedMap[i][j] = 0;
  //remove key from list
  for (int k = 0; k < KEY_BUFFER_LENGTH; k++) {
    if (activeKeys[k] == keyMap[i][j]) { activeKeys[k] = NULL; break; }
  }
}

void loop() {
  Keyboard.set_modifier(0);
  modifierKeys = 0;
 
  for (int i = 0; i < ROWS; i++) {
    setDrain(i);
    for (int j = 0; j < COLLUMNS; j++) { sendKey(i, j); }
  }

  Keyboard.set_modifier(modifierKeys);
  updateKeys();
  Keyboard.send_now();
  delay(10);
}

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #22 on: Mon, 07 November 2016, 20:23:40 »
hi mark,
I'm stuck again. The key of my layout is not easy enough to be found. The row 2 and the row 3 has the different height.

Offline mark.wood_uk

  • Thread Starter
  • Posts: 34
  • Location: Aberdeenshire, UK
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #23 on: Tue, 08 November 2016, 12:48:55 »
The good news, if your still stuck, is that Teensy 3.1 supports Arduino's programming suite. (Basically plug and play simple programming in a cut down version of C).

PJRC (The creators of the Teensy) have a tutorial on setting things up over on their website: http://www.pjrc.com/teensy/tutorial.html


As for the code, that one's on you, but here's the code from my current keyboard to get you started. Its not that pretty but it does the job. (My C was pretty out of practice when I wrote this thing)

I had kinda given up on using the 3.2, i was having too many issues whilst trying to use Kibohd's controller from github (my lack of C knowledge and minimal knowledge of terminal was making it near impossible for me to debug where i was going wrong!). Very few people have used the 3.2 on GH for a keyboard controller and no one was able to give me any help so i went and ordered 2.0's instead!

I will definitely give your code a bash though as i do have enough components for a 3rd keyboard! There is nothing wrong with rough and ready, as long as it gets the job done  ;D
Thanks for sharing with me!  :thumb:

hi mark,
I'm stuck again. The key of my layout is not easy enough to be found. The row 2 and the row 3 has the different height.

Yep you will find that with custom layouts from time to time, i have a couple of keys which are from the wrong row etc... For example, my Quark keyboard layout:
152410-0
Esc - higher than the qwerty row
Space - 2.75u Right shift turned upside down to be more comfortable to use with my thumb
Del/backspace - lower than the qwerty row (came from insert, delete, home, end, pgu, pgd key block)
Enter - 1.5u Win key (thankfully this is the same height, but doesnt say enter!)
right Shift - 1u blank key
Up arrow - higher than the rest of the row but acts like a homing key!


You will either have to search for custom keys in the row that you need or use blank keys (blank keys are a lot easier to find!)
At a glance from your layout photo you will have height problems with the following keys: 
Pause/prt sc,
ins/del,
pgu/pgd,
home/end,
fn,
ctrl,
alt,
mac,
space,
M1,
M2
Arrow keys!
The arrow keys should work in your favour though (well they do for me) as the height difference makes them feel like homing keys!

Honestly i'm not even sure if you can get 3u keys to be honest, but don't take my word for it as i'm new to this!
You would be better asking around or looking in the classified section
« Last Edit: Tue, 08 November 2016, 13:00:42 by mark.wood_uk »

Offline yeti0210

  • Posts: 7
  • Location: Shanghai,China
Re: DIY Keyboard project - Teensy 3.2 arrived in the post.... Now what?
« Reply #24 on: Tue, 08 November 2016, 21:00:02 »
hi mark,
I will use DSA  keys which have the same height.You can find the difference between them through the url below.
http://keycapsdirect.com/key-caps.php

But the 3u space keys is really hard to be found.  :-[