Wednesday, April 17, 2019

Integer to it's binary string representation in C

My oldest son is studying mathematics and programming in college and wrote me asking for a way to convert an integer to it's binary representation in C. My C string handling is a bit rusty, but I accepted the challenge. This was the result, maybe it can be useful for someone.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char *int_to_binary_string(int);

#define BITS_REQUIRED (sizeof(value_to_convert) * 8)

const char * int_to_binary_string(int value_to_convert) {
  char *result_string = malloc(BITS_REQUIRED + 1);

  for (int bit_counter = BITS_REQUIRED - 1; bit_counter >= 0; bit_counter--) {
    int current_bit = value_to_convert >> bit_counter;
    strcat(result_stringcurrent_bit & 1 ? "1" : "0");
  }

  return strchr(result_string, '1');
}

int main(void) {
  int value_to_convert = 55;
  printf("%i in binary is %s\n", c, int_to_binary_string(value_to_convert));
}

And the output:

{21:32}[2.5.0]~/Desktop ➭ gcc test.c
{21:39}[2.5.0]~/Desktop ➭ ./a.out   
55 in binary is 110111

Wednesday, March 6, 2019

Potentiometer value codes

Just received a potentiometer kit I ordered from Ebay. Off course it all comes in unlabeled small ziplock bags:




But as with most electronic component, potentiometers have small marking or value code on them:


The following is a table that will help you identify the actual value of the said potentiometers:

Code Value
100 10 Ω
200 20 Ω
500 50 Ω
101 100 Ω
201 200 Ω
501 500 Ω
102 1 kΩ
202 2 kΩ
502 5 kΩ
103 10 kΩ
203 20 kΩ
253 25 kΩ
503 50kΩ
104 100 kΩ
204 200 kΩ
254 250 kΩ
504 500 kΩ
105 1 MΩ
205 2 MΩ
505 5 MΩ

Wednesday, January 2, 2019

Electronic devices + password + death = pain

A few months ago my wife's mother died. Being the "teksavvy" guy I had to deal with the disconnection of all-the-things electronics, gather devices that needed to be returned to the ISP,...

Good thing she only had a Windows 10 laptop, no cell phone, no tablet.

But my wife wanted to access what was on the laptop before we wiped it and re-purposed it to a family member.

Only thing was that the person (not me) who set it up for her had configured it with a Microsoft LiveID account. We had a bunch of hand written note with passwords, account ID but could not find a password or PIN that worked to login into her Windows profile.

I had done Windows password reset in the past using a Live CD but it was in the Windows NT era...

The first ISO I used was Hiren’s BootCD PE (I'll spare the details that the laptop is UEFI and does not have a built-in CD/DVD drive and I could not get it to boot on USB). Unfortunately it was not able to reset or remove LiveID account password. But I was able to clear the local Administrator account password and this gaining access to the machine. If all fails we could always at least recuperate the files from there. But I wanted full account access.

After a few Google search I found PC Unlocker. Being a FLOSS advocate I tend to dismiss closed source commercial software. But I did not have much more time to spend on this. So I bought a copy for 32.7$CAD and give it a spin.

Sure enough I was able to remove the user account password. I then rebooted, Windows in all it's glory decided to do a HDD scan and repair (why I don't know), but I was then able to login the account without any passwords.

I would have preferred an open-source free utility, but when time is limited it's sometimes worth it to spend 30$ instead of fiddling for hours.

If you know of any FLOSS tool that can reset/clear LiveID account password on Windows let me know !

As a side note, having many electronic devices, accounts and passwords I'll have to evaluate how to deal with this to ease the way for my relatives when (if I ever lol) pass away.


Sunday, December 9, 2018

Day-O: A customizable macOS clock replacement

At work we use week number milestones. Finding which week we are in is always a pain... Until I found Day-O (https://shauninman.com/archive/2016/10/20/day_o_2_mac_menu_bar_clock).

This little piece of software can replace the standard macOS clock and is fully customizable. To install it simply run:

    brew cask install day-o

Then simply start it to get:


You can then click on this new clock and select "Preferences". This is where it gets interesting:
Day-O has multiple placeholders that can be added, all very well documented here: https://shauninman.com/day-o/datetime.html

In my case I simply want to add the week number so I used the following format:
This gives the following clock display:


Next you can remove the standard macOS clock from the control panel.

If you want to move the Day-O clock right in the tool bar simply click on it holding the 'Command' key and slide it in the position you want. For me the final result is:


Hope you find it as useful as I did.


Thursday, November 29, 2018

Helping a STEM student Arduino homework

A few weeks ago I responded on LinkedIn to someone who was looking for someone Arduino savvy. Turned out to be someone looking for help for her daughter that had trouble with a STEM course assignment.

Now I don’t usually do these kind of help. Most of the time it’s lazy students looking for someone to do their homework for them for free of 10$... I have been burned before.

But this time it was different. First it was the parent asking and after a few exchange I could feel that they were willing to put in the effort.

The task at hand was pretty easy. The student needed to reproduce an Instructable project based on an Arduino Uno, a 16x4 LCD and an analog joystick. The Instructable project was this one: https://www.instructables.com/id/Arduino-Looong-Snake-Game-With-LCD-16x4/

I first discovered that they and bought the wrong LCD and did not have the joystick part. I help them source it and during the time they waited for the parts I went on to build it myself. The hardware assembly was pretty straightforward and I had all the parts. Here is the Fritzing diagram:



Next I went on on compiling and uploading the code provided in the Instructable article. It did not work. The code was supposed to wait for you to select a level, but it would instead go straight to the game. Joystick did not work to control the snake… Then when I had that “Oh great, what did I get myself into” moment.

After a 30-40m adding debug print to the serial console and analyzing the code I figured out that the bugs in the code and just fixed it quickly. At least now the code was doing what it was basically designed to do. There are still some bugs in it where the snake jumps sideways, but the plan was never to fix it, just to get it working.

I did adjust the code writing style to match what I use so that it can be somewhat more readable. I did not add any comments or change the code structure.

You can download the fixed source code here: https://gitlab.com/IndianaTux/arduino-snake-game

Now the parent and her daughter got the parts but the male header is not soldered on the LCD and they have yet to find someone to do it. I’ll update this post with issues they went through as I help them out.


Friday, November 16, 2018

Adding shadow to images from the command-line (macos/linux)

When doing documentation, adding a shadow to images or screen grab makes the document come aline and helps break text from images.

For example the following image:


Is pretty "normal" and does not stand-out. However with a single command you can turn it into this:


You will need the ImageMagick package installed: https://www.imagemagick.org/script/download.php

On macOS if you have brew installed it's as simple as running: brew install imagemagick

Then to add a shadow as shown above you simply have to run:

my_image="pi.png" ; cp ${my_image} ${my_image}_original ; convert ${my_image} \( -clone 0 -background gray -shadow 80x3+10+10 \) \( -clone 0 -background gray -shadow 80x3-5-5 \) -reverse -background none -layers merge +repage ${my_image}

Just change my_image="pi.png" to reflact the filename of the image to shich you want to add the shadow to. The above command will create a file with _original which is your original unmodified image.

Wednesday, November 14, 2018

Montreal Advanced Design and Manufacturing Expo 2018

So I attended the the Montreal Advanced Design and Manufacturing Expo at Palais des congrès today.


Was not expecting much, attended out of curiosity. It was really aimed more at the factory automation and packaging business but managed to get to know a couple of sensors manufacturers as well as a Fab (PCB assembly) house that accepts prototype level quantities.

But what I was most disappointed about was the Palais des congrès wifi... On the expo floor I could not connect to the free wifi network from my iPhone of MacBook. It would simply not give me any IP address... The only wifi network that was working was a paid one, so I used one of my 5h of free Fido internet and tethered...

On the floor outside the expo area I managed to connect to the free wifi and get the portal page, but could not get pass it:




So I went over to one of the VanHoutte but it's wifi was locked and I was sitting down with my laptop so did not feel like going to the counter to ask for the password and leave my stuff unattended.

Luckily there was a TimHorton just in front and I was able to connect from there, but my PPTP (yeah I know) would not work...

Integer to it's binary string representation in C

My oldest son is studying mathematics and programming in college and wrote me asking for a way to convert an integer to it's binary repr...