Moazzam's Ramblings


Ergodox: Make LEDs indicate the layer you are on

This article assumes that you are working with the source code downloaded from MassDrop.

Open the file lib /key-functions/public/basic.c and add the following line below after all the #define statements.

extern uint8_t keyboard_leds;

Then go to the push function of the layer you want the LEDs to light up on. Let's assume we want to turn on NumLock LED when layer 2 is active. In kbfun_layer_push_2 function, we add the following line:

keyboard_leds = 0x01;

So the function will look like this:

 * [name]
 *   Layer push #2
 * [description]
 *   Push a layer element containing the layer value specified in the keymap to
 *   the top of the stack, and record the id of that layer element
void kbfun_layer_push_2(void) {
	keyboard_leds = keyboard_leds | 0x01;
 * [name]
 *   Layer pop #2
 * [description]
 *   Pop the layer element created by the corresponding "layer push" function
 *   out of the layer stack (no matter where it is in the stack, without
 *   touching any other elements)
void kbfun_layer_pop_2(void) {
	keyboard_leds = keyboard_leds & ~(uint8_t)0x01;

You will notice that there is a keyboard_leds = keyboard_leds & 0x01 in the pop function for layer 2. This will turn the LED off when we exit the layer. For CapsLock LED set keyboard_leds to 0x02 and set it to 0x04 for scroll lock.

What if you want to turn on multiple LEDs when you are on a specific layer? If you want the NumLock and Scroll Lock LED to turn on when you are on layer 2, you would write the following:

keyboard_leds = keyboard_leds | 0x01 | 0x02; 
//To turn them off
keyboard_leds = keyboard_leds & ~(uint8_t)0x01 & ~(uint8_t)0x02;
Tagged as: 5 Comments

Ergodox – The customizable ergonomic split keyboard

A few months ago, I got a mechanical keyboard called Ergodox. It is an open source keyboard (which means its schematics are posted online for anyone who wants to build it.) that you need to build yourself. You cannot buy it pre-built. Although, recently people have been putting up Ergodox with Cherry MX blue switches.

It has 2 halves (like every other split keyboard) that are separated by a wire. And, you can put the two pieces as far or as close as you want. I usually have a 6-7 inch separation between them.

Why did I get it?

I hated the placement of the apple command key on regular keyboard and wanted it to be in a more convenient place so I set out to search for keyboards that have it placed more to the middle. Das seemed like a great choice for this as their alt key is located below the letter C (not below X where it usually is) but I still wasn't satisfied. So, after some research I found out about Ergodox.

I did quite a bit of research (read reviews, visited geekhack, etc) and decide to take a leap of faith and just get one for myself. Massdrop was doing a group buy and it was going for cheap so I joined the group buy and received it in about 2 months.

Ergodox's design is heavily influenced by Kinesis Advantage which is an amazing keyboard in its own right. I love that they (both Ergodox and Kinesis Advantage) have a thumb cluster (a set of keys intended for use by the thumb).

It uses Teensy to register key presses and you can make any key register anything you want. You can choose to have a qwerty, colemak, dvorak or a custom layout. For alphabets, I use qwerty layout but for other things I have it customized to help me program. You can find a link to my layout at the bottom of this post.

The Good

You can customize pretty much anything you want. You can have one layout or multiple layouts. Very few keyboards let you do that and those that do, have a limited selection of layouts you can use. With Ergodox, you can make your own layout, if you wanted.

If you know a little C you can even customize the LEDs. Usually the LEDs are used to indicate if caps lock or num lock is on (I don't think anyone uses or cares about scroll lock). I have never needed to use caps lock or num lock so I programmed my Ergodox to tell me which layer I am on (I use one layout for regular use and another for gaming and I have another layer for multimedia keys like volume control, etc).

The thing I love a lot about Ergodox is the thumb cluster. I have found it to be very useful. It has reduced my hand travel by quite a bit and this has, in turn, reduced the typing mistakes I make.

It is truly a split keyboard. You can have the two halves of Ergodox as far or close as you want. You can also detach the wire separating them for easy storage.

The Bad

There is a big learning curve initially - just like if you moved to Kinesis Advantage from a regular keyboard. So, you will have to be patient with it for the first month. I got fairly comfortable with it in a week but it took me about a month or two to treat it like an extension of my hand.

You have to wait about 2 months to get it after you order it (if you want it for a cheaper price). Or, spend over 500 dollars.

You cannot order it whenever you want if you want to get it for an affordable price. Massdrop, which is pretty much the only distributor that sells Ergodox for an affordable price, does a group buy every 2-3 months. So, if you miss the drop you have to wait 2-3 months to get it.

Ergodox cannot wake up your Mac from sleep. If your computer is in hybernation or sleep then hitting a key on the keyboard will wake it up. This is not the case with Ergodox. So, you will need to use your mouse to wake your computer up.

Although, if you connect your Ergodox to your computer through a USB hub then it will be able to wake the computer up (but not when Ergodox is connected to the computer directly).


I have been using Ergodox for over 3 months now and I don't want to use any other keyboard. I love it so much that I take it to work and back home since I have just one. Although, will be getting another one for work because I don't want to carry it everyday.

I have been tweaking it ever since I got it. Moulding it to my needs - making my life easier as I keep using it. I have had a complaint with all the keyboards I have ever used including Kinesis freestyle 2 and Kinesis Advantage. For the most part, you can't do anything about it but with Ergodox you can and there is a huge community of people at geekhack to help you.

It is, however, not for everyone. My friend tried it and said he preferred the staggered layout of a regular keyboard vs the matrix layout of Ergodox. I have found matrix layer to be better for me when typing. So, it is all subjective.

The keycaps that Massdrop sells for Ergodox make it hard to use the top row in the thumb cluster. However, Signature plastics sells DCS R5 keycaps on their website that make using those keys so much easier. Kinesis Advantage does the same thing.

If you can afford it, I suggest you try Ergodox out and if you don't like it you can sell it on Ebay for nearly its cost price and maybe even more than it.

Ergodox's website:
My layout (which keeps changing):

Filed under: Uncategorized No Comments

My mechanical keyboard

In my last post, I talked about mechanical keyboards and how great they are to type on. I figured I would show you guys what my keyboard looked like. I have a Corsair K60 with custom key caps (well some of them are custom). Corsair did a great job with the keyboard and the only thing I didn't like about this keyboard is that the right shift wasn't smooth to press if I were to press it on the edge. I, accidentally, solved this problem by changing the keycaps. So here is what my keyboard looks like:

Filed under: Uncategorized No Comments

Mechanical keyboards

Between work and personal projects, programmers tend to type 8 or more hours a day and it helps to have a keyboard you can type on comfortably. Something that wont kill your fingers or your hands over time. Mechanical keyboards fill this need. They have taken the gaming world by a storm and a lot of the hard core programmers I know don't like using anything else. Now that more and more companies have started to offer mechanical keyboards as part of their lineup, it is really easy to get your hands on one.

For those who don't know what mechanical keyboards are, you can read about them here: I got interested in them because my Dell keyboard (the one that comes free when you buy a PC) was starting to feel like I had to fight it to type anything. This made me start looking for an alternative. The first mechanical keyboard I tried was a DAS keyboard and now I don't ever want to use a rubber dome keyboard ever again (except Kinesis freestyle 2).

Mechanical keyboards are a treat to type on and come in many shapes and sizes. If your fingers hurt when you type on a regular keyboard I highly recommend that you get a mechanical one. You don't have to press the keys all the way to the bottom for the computer to know you pressed a key. This means your fingers face less resistance when typing. You will, however, have to practice not bottoming-out the keys (pressing the keys all the way to the bottom).

If this has made interested in mechanical keyboards and you want to get one. Here are some I recommend looking into:

  • Filco Ninja or Majestouch 2 keyboards. Filco is, hands down, the best keyboard maker. Their keyboards are not backlit and don't have macro support but their build quality is the best. Amazon is the only reseller that sells these in USA.
  • Ducky Shine. Ducky is more of the higher end mechanical keyboards and they have the best backlit keyboards.
  • DAS keyboard. DAS has been making keyboards for quite some time now and I love their design. Their keyboards, like Filco, are also not backlit
  • Ergodox. This is an open source keyboard you will have to make yourself but it is very ergonomic and you get the satisfaction of having made your own keyboard.
  • Fully mechanical keyboards by Corsair. Corsair has some very original designs for their keyboards. Their keyboards have no bezel making it really easy to clean them. Starting from K70, they are backlit.
  • Filed under: Uncategorized No Comments

Corsair k60 OSX issues

I got a Corsair K60 a few months ago and fell in love with it. It is a mechanical keyboard (my first mechanical keyboard) with Cherry MX red switches. There is, however, one problem with it. The caps lock LED is always on and the LEDs for num lock and scroll lock are always off.

This affects all models of Corsair keyboards as far as I know. Why they don't just add support for Macs? I don't know. It can't hurt. It is just another HID device.

The keyboard will toggle caps lock when you press it but it won't change the state of the LED. So, like any sensible person I set out to fix the damn LEDs. And, let me tell you it was not an easy task (considering I have not programmed a single line in objective C before this or written any hardware drivers in any language).

After spending the whole weekend on it, I was able to write a program that will listen for key strokes from k60. Now, I have to figure out how to toggle the LED when caps lock is pressed and I will be able to have a normal functioning keyboard.


You can make PHP extensions in a PHP like language

I recently came across an article which spoke about a language called Zephir. It was written with the goal of letting PHP programmers write PHP extensions. I was really happy to read this. For quite some time I have dreamed of creating PHP extensions but the fact that I would have to learn C kept me away from it (although I would have eventually done it). And, I have been jealous of Lisp which can be extended using Lisp itself (unlike PHP which requires you to know C to extend it)

Zephir looks almost like PHP. There are a few differences but if you are a PHP developer it will probably take you mere hours to get the hang of it. The same guys who wrote the Phalcon framework have written Zephir. For those of you who don't know what Phalcon is, it is a framework written as an extension of PHP. It is the only framework of its kind (as far as I know). All other frameworks are written in PHP.

Imagine the speed boost something like this can give your application. Binary code executes much faster than interpreted code. You can shift all your intense computing code to an extension and improve the performance of your application by quite a bit.

You can learn more about Zephir here:

Filed under: PHP No Comments

Technology is a means to an end and not the end itself

Last Friday, the CEO of the company I work for said something interesting: "Technology is not the end. It is a means to an end." He, then, elaborated that by saying that we are making applications to make people's lives easier. The code doesn't have to be perfect. If it works for 90-95% of the cases, it is fine. We can go back to it later and refine it.

This caused an internal debate within me. I have always been (and still am) an advocate of beautiful code but I have (occasionally) let myself get slowed by trying to make code more beautiful (and better organized). What I should have done is made something work then refactored it afterwards. This way, I don't delay a release and I get time to make things better later.

Obviously, this assumes I will get time later on to make the code better organized or more understandable. Depending on the industry you are working in, you may not have the time to make things better or refactor the code. So how does one deal with this? You have to strike a balance (which may not be easy at times) between trying to make everything 100% perfect and finishing the project on time.

I don't want anyone to think that it is okay to write sloppy code and use this reason an excuse. I have had to deal with the aftermath of such code way too many times. Let me tell you, it is not pleasant (although sometimes it can be interesting). It is, however, important that we don't get bogged down by writing that perfect piece of code that we lose sight of what is most important - getting the application in the hands of the end-users who will have feedback of their own.

Filed under: Uncategorized No Comments

Selenium Chrome driver nuances

When writing test, you generally tend to run the test in one browser and expect it to work on all the rest (except maybe Internet Explorer). I had ran my tests on Firefox and they succeeded but not on Chrome.

In Firefox, if you click on an element that is not currently visible on the screen, the browser will click allow it and not cause any issues. However, Chrome will complain saying that the click will be caught by another element (depending on how you structured your HTML). So, you have to move to the element before you can click on it.


Testing multiple browsers with Selenium2 in PHPUnit

You used to be able to run test on multiple browsers with PHPUnit Selenium test class by specifying a $browsers property. You can still do the same with PHPUnit's Selenium 2 class made for web driver. Here is how you do it:

class SomeTest extends PHPUnit_Extensions_Selenium2TestCase
	 * Variable to specify which browsers to run the tests on
	 * @var array
	public static $browsers = [
		['browserName' => 'firefox'],
		['browserName' => 'chrome']	
	public funciton __construct()
	public function setUp()
	 * This is just a test that will open a website in chrome and firefox
	public function testOpenSite()

Fastest way to traverse an associative array

PHP has array_walk() which can be used to traverse an array and modify it in place. I wanted to find out if array_walk() would be faster than a loop written in PHP (for, foreach). I mean, PHP's code is written in C/C++ and traversing anything has to be faster in C than in PHP.

So, I wrote a script to traverse an array of about 100,000 elements. Below are the results (formatted for better readability):

new-host-2:exp moz$ php for.php
Method          Time taken to traverse 100,000 elements
for		0.030004024505615
foreach		0.0066280364990234
array_walk	0.019280910491943
new-host-2:exp moz$ php for.php
Method          Time taken to traverse 100,000 elements
for		0.041378974914551
foreach		0.0075538158416748
array_walk	0.02002215385437
new-host-2:exp moz$ php for.php
Method          Time taken to traverse 100,000 elements
for		0.029618978500366
foreach		0.0064899921417236
array_walk	0.018718957901001
new-host-2:exp moz$ php for.php
Method          Time taken to traverse 100,000 elements
for		0.041105985641479
foreach		0.007500171661377
array_walk	0.020205974578857

foreach traverses an associative array faster than array_walk() and for loop. In fact, it was much faster than both. I had expected array_walk to be faster but I was wrong. You will notice that the while loop isnt in there. I figured that a while loop would take about the same time as a for loop. Here is the script I ran for my experiment:

echo 'Method          Time taken to traverse 100,000 elements', PHP_EOL,
     . '========================================================' . PHP_EOL;
$assocArray = [];
for ($i=0; $i < 100000; ++$i) {
	$assocArray['key' . $i] = 'hi ' . $i;
// Let's begin the tests
$keys = array_keys($assocArray);
for ($i=0, $n=count($keys); $i<$n; ++$i) {
	$tmp = $assocArray[$keys[$i]];
foreach ($assocArray as $key => $val) {
	$tmp = $val;
array_walk($assocArray, function ($item, $key) {
	$tmp = $item;	
class Timer
	protected static $start;
	protected static $stop;
	public static function start()
		self::$start = microtime(true);
	public static function stop()
		self::$stop = microtime(true);
	public static function diff($label)
		print $label . "\t\t" . (self::$stop - self::$start) . PHP_EOL;
Tagged as: No Comments