Tuesday, October 16, 2012

Unit Testing with Structural Coverage


I don't love spending time testing, so I try to make the best use of my time. I write Unit Tests when I can, and automate any other testing if possible. Sometimes I try to even make writing tests easier on myself by using some techniques I picked up from The Pragmatic Programmer: From Journeyman to Master
Side Note: If you haven't read this book, stop what you're doing, go buy it, rent it from a library, borrow it from a friend I don't care, READ IT, LEARN IT. It's one of the best books I've read about software development hands down, lots of good ideas to improve your craft.

Unit Testing

Unit Testing code is helpful for looking at code you've written in a very controlled environment, you know the inputs and outputs, and being able to duplicate your results should be pretty consistent and easy. Often times this testing puts a single function or procedure through the paces, but depending on what level you are will determine what number of units will be involved.

Primer

After writing some code a few months back, I created what I thought were some good Unit Tests for it. They all passed in the end (and before they did, we found some issues when they weren't passing) which is great. The change got closed and merged into the baseline. I thought I had made a good attempt at ensuring that the software was solid. For the data I used I agree it is solid. Then a bug was found. I hadn't tested every case (even though I thought I had). What would have been a huge help is to have simply combined Structural Coverage with it.

Structural Coverage

Structural coverage is attaching special code to test for running through each path of your code. This is helpful for simply ensuring you've tested each route. This isn't a substitute for proper testing. I'll say it again. A structurally covered program is NOT a bug free program. It's simply a program that has been proven to work as expected for a (pseudo) normal scenario on each path encountered.

Combined Testing

By combining Unit Testing and Structural Coverage, you can prove that the Unit Tests at least are checking each case once. I have since re-ran the unit tests with Structural Coverage, and guess what? I missed a few paths, this didn't explain my particular problem, however it made it easier to see other possible problems I may have in the future. I have since added some unit tests and have made sure that my Unit Testing at the minimum covers each case.No program is bug free, but all we can do is analyze and pick the Unit Tests that we as Engineers believe will cover what's necessary.


If you don't do Unit Testing where you work, I highly recommending starting to do it on your own. From the start make it as easy to setup and run as possible, this will make sure that you will use it, and that your colleagues will use it. Additionally, look for opportunities to make the unit testing automated. If your unit tests run on the system everytime a build is ran, any bugs introduced in those pieces of code can be found quickly. This keeps you from violating the DRY principle, forcing yourself to repeat yourself testing it over and over. OR trying to fix the same thing multiple times because it's not caught at the right time.


Good luck!

Monday, October 8, 2012

Christmas Lights

Description
I finally got fed up. We bought a Christmas light "timer". This thing would do essentially 3 functions, the first was simply turning the outlet on, the second to use a photo sensor and turn off when it's light outside, and the final was a set number of hours (there being anywhere from 1 to 8 hours).  
The Problem
The problem with the hours one is that you have to manually turn it to off, then back to the number of hours you would like. This is frustrating. The photo sensor would seem like a neat idea, however when you don't get good lighting where the photo sensor is, you end up with Christmas lights on all the time. I (and in part due to my wifes request) decided this had to be rectified. 
Attack Number 1
My first run at it was to simply solder onto the point where the timer is on, and try to set and clear the 4 hour timer (but since the system needed to go back to off, then back to on, this failed miserably). 
Attack Number 2
Then I realized that the photo sensor should handle the turning off and on automatically, and I wouldn't need to turn it all the way off. I decided to remove the photo sensor, and put a pair of wires in there and a relay instead. Now the fun part. I have a PIC16F684 that I decided to control this with. With a multimeter I tested putting the photo sensor under the light in my room, when it was under direct light, the resistance went up really high, when I covered it, it seemed to reduce to a low amount of resistance. My guess is that I can put a resistor in line with the relay and whenever there is a high amount of resistance it turns the system off. When the resistance is low it turns the system on. Essentially what I needed was my microcontroller to activate my relay for 4 (or so) hours then turn it off. Then 24 - 4 hours later, it would attempt to turn on the relay again. If I can get this working on a short run, I.E. 10 minutes on, then 2 hours off, then 10 minutes on. I may consider putting in a pseudo random number into there, so the lights are on somewhere between 3 and 7 hours.
Unfortunately I plugged it in, tried shorting the pins for the photosensor (hoping it would activate the lights), well it didn't seem to matter whether the wires were shorted or not, the lights just stayed on.
Final Solution
I finally actually did what any smart engineer* should do. I took a look at what was already there. I saw 2 chips the first was a LM393 a dual voltage comparator. And the second was a programmable timer. JACKPOT. (Here it is for those of you interested: cd4541be). Looking at the datasheet I see there is a master reset line. I tested it out by turning the system on for 1 hour, then when the lights turned off, I simply tapped 3.5 volts to the MR line and bam lights were on again. I now have my "in".

On to the Microcontroller
I decided to use my Pic 16F684, I have a smaller one (can't recall the number at the moment), but I didn't have time to make sure my code ported smoothly as well as verify the timing. I did a couple tests using a couple nested for loops for a wait (I know, I know, I could have used a timer interrupt, and in fact i would like to go fix this, but I didn't have a lot of time to mess around with this and well, hey it was a hack)
So after some timing, I simply put an initial wait in there to get it to hold off to about 7pm from when I plugged it in, and then set the timer to 4 hours. The lights turned on for 4 hours, then a few hours later 7pm hit, and bam we have light.

However over the long term the timing was off for sure, when I got back from vacation it was definately wrong, I'll have to play with it this christmas again!

*Smart engineers keep things simple, and just using a reset line is MUCH simpler then the previous methods I tried.

Monday, September 3, 2012

Syncing ZSNES game saves across ubuntu systems

I was hoping to sync my ZSNES game saves across all my pc's. It turns out it take very little work.

First I made a folder in dropbox for my game saves and made a copy of my current saves out there.

Next I removed the contents of the zsnes folder located at
/home//.zsnes.

Steps:
(Make sure you've saved the files from .zsnes folder below first
cd ~/.zsnes
rm * (this gets rid of the files)
cd ..
rmdir .zsnes

Finally I setup a simlink from my dropbox to the folder so I always had my game saves synced.

Steps:
cd /home/
ln -s ~/Dropbox/zsnes .zsnes

Substitute the path with your path and bam you're done!

Good luck



Saturday, September 1, 2012

Environment Tips

This tip comes from Phil otherwise known as @civissmith on the interwebs.

Often times you have to swap between screens, I usually use alt/tab or some combination of swapping between document/editor in order to read and update. A neat tip Phil alluded to was that if you set your command line to semi transparent, you can still read any commands or notes while typing your code or commands over. This is a great tip as it can save time on having to constantly swap between (possibly losing your train of thought).

Thanks Phil and any other tips you pass along I'll be happy to post!

Wednesday, July 18, 2012

Energia Setup

There is a link in the "issues" section of the github site for energia.

Setup
Extract to a folder on your computer, inside that folder go to hardware/tools/mspgcc-4.6


Swap to super user. (may not be necessary but for sure need to use SUDO).

Then finally run bash ./build-mspgcc
A while later when it's done, you can navigate up a level to the same folder as mspdebug.

Make sure to set the execute bit on this file. This will require use of CHMOD. I just used 777 to be quick, but be careful as this isn't always the best choice of permissions.

Plug your launchpad board to the computer.

Now navigate to the main folder and run (at a minimum sudo) ./energia as super user.

This is the screen you get when you first start.

Ensure your board is communicating:
Select tools->serial port make sure something is there and selected.

Select tools->Board make sure your launchpad is listed and selected.

Select tools -> Programmer mine says AVRISP mkII not 100% if yours should be but these should already be selected automatically.

Test Program A.K.A Hello World
Select file-> examples -> 1. Basics -> Blink


Finally you get the above. Now if you hover over the second button from the left, you see it says Upload.

Click this button your board's led2 should be blinking (this is the led near the bottom of the board, opposite the USB cable, near a button and pair of jumpers).

You should be good to go, have fun and good luck, oh and,

HELLO BACK

Monday, July 9, 2012

IAMHERE 2.0

Well I finally released an update, and IAMHERE has been improved. I added categories of texts, as well as a share button.

There are a few categories and if you have any suggestions let me know and I may add them to my list of updates to include.

If you have any suggestions let me know and I'll definitely look into them.

Thanks and have fun.

https://play.google.com/store/apps/details?id=com.onaclovtech.iamhere

Wednesday, April 25, 2012

Introducing Proof of Concept Music Glove

Well the day I never thought would come has arrived. I have finished a proof of concept of my android music control using an external (in this case glove) device.

It was a long and slow journey, I've often times got so many projects in the pipeline that it's really exciting when I get to see one finish up.

This evening I finished up wiring my device, and asked @civissmith over to help with the video, so Thanks I really appreciate it, trying to do what I was showing with one hand would have been, painful to say the least.

Basic concept, Buttons attached to arduino send characters to android app, when music is running, pressing a button allows control of the music app.

Shot of Glove
Shot of Glove


Button inside glove


Without further words, I think the video will be proof enough.




If you'd like to follow me on twitter I can be found here: @onaclov2000

Monday, April 16, 2012

Causing the next song to play

With the android music player you can change songs using the following broadcast. It's not guaranteed to work with any other music players, or even with any updates they make, however it works for what I need right now (I have no interest in making a music player), If you have a music app and have an intent to change songs,etc, please let me know and I'd be happy to support if possible. I found this across the interwebs somewhere probably nearly a year ago now, I can't remember where I found it, or who wrote it, so if you are the one responsible I would love to give you credit (i'm sure it came from stackoverflow at one point, but after a lot of searching recently I can't seem to find anything about it).
Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "next"); sendBroadcast(i);
Good luck.

Modifying Bluetooth Chat

I started out trying to pull the android bluetooth chat application from the developer website....BAD IDEA. I spent quite a bit of time commenting this out, adding a toast here and a if then there. That one is built for a different sdk version than I'm using, so my recommendation would be to pull it from the examples folder in the sdk download.

Well I finally grabbed the bluetooth chat application from the sdk examples folder (when you download from google). After about 5 minutes I tried loading the default app on my phone and touchpad, it worked just fine. I then changed ONE LINE of code and my arduino can talk to my android devices with bluetooth. What line say you? This one:

In the BluetoothChatService.java
From
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

To
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


That's it that's all it took. Apparently that UUID from what I've been able to gather is for the bluetooth serial interface, so that's all I needed.

Here is video proof of the application in action.





http://youtu.be/Zof0IYUU71Q

Good luck and let me know if you have any questions.

Monday, April 9, 2012

Ada Withs

Eclipse has a really nice feature which allows through a keyboard shortcut the ability to update the imports required for your code.

I liked that, but didn't know of anything that did the same thing for Ada source code.

I have began creating a script which does this, and you can use programs to run it, (perhaps port it to a plugin for a program).

I called it Ada-Withs (I know it sounds pretty boring)

It's not complete, but right now the script will pull out all packages used in the program.

If you want to give it a try go for it. If you want to mess with it, pull and let me know if you have any improvements.

The link above does go to it but here is the full url as well:
https://github.com/onaclov2000/Ada-Withs

Wednesday, April 4, 2012

One Language to Rule Them All

Do I think that there is one language that is best? No. I can say that with a high degree of confidence. Each language has it's own idiosyncrasies. If the first language that was created was the best, we wouldn't be using anything else. But we all know that there are issues with each language, and each language was often created to make an aspect of another language easier/better.

Last week I recommended not attempting to solve a particular problem with python. The solution was already partially coded up in perl and it just didn't make sense to re-solve the problem (simple data parsing) all over again. It got me to thinking, was this the right suggestion? I think the final result of the script should have been in perl, however learning to do it in python was a good idea. It would improve their toolbox.

I realized I should encourage writing things in a variety of languages. Why? So you know which tool to use. I came to realize that parsing data was easy to do in perl 90% of the time. If I knew python better I would probably feel the same way about python. The issue isn't about python vs perl vs java vs c++ vs c#, the issue is knowing what to use when.

I know that if I want to make a simple fast to build gui application I can use Visual Basic. Joe Blow will use Java, and Jim Bo will use Perl. Are any of them wrong? No, what you use, depends on your tools available. If you know how to do the same thing in 5 different languages, you'll start using a default for one set of problems, another for another, and mixing them if necessary.

I can solve the same problem in every single language, but the issue is, which is easier to do it with? Do some research, if you find that dragging and dropping a button onto a form is easier then writing the back end required to create you idea. DO IT! There is nothing more encouraging then building something and actually seeing it working, then spending tons of time building something and never seeing it come to fruition.

Good Luck.

Monday, March 12, 2012

New Apps

New Apps Released!
Well it was a busy couple of weeks, I have released 2 apps on the market in the last few weeks.  Pizza Area, and I Am Here.

Pizza Area
This calculates the square inches of a pizza for you (really works for any size circle with a diameter), Also if you have a price it'll calculate the price per square inch, as well as the estimated cost at that rate for the pizza size smaller and larger that the app contains. Give it a try and let me know if you have any suggestions, and as always a positive rating on the appstore would be appreciated.


I Am Here
This app will allow you to select a friend from your contacts (or type the number in) and when you hit the icon it'll send a message. This app is very similar to another one on the Apple Market, I was a little disappointed I hadn't released this sooner, my buddy civissmith had come up with the idea originally,  and I was just way to slow to implement. Well give it a try either way, I think mine has a little more spunk then the Apple Market version.

That's all for now, I have so many projects in the works at all time, I just have to finish a few so I can write about them.

Thursday, February 16, 2012

GraphViz

I decided to start playing around with GraphViz, here are a few of my notes:
To call a file use the following command (works for me).

dot .gv | dot -Tpng >.png
This generates a png file.

Next up what the .gv file contains:

digraph G {Hello->World; World->Earth; World->Mars; World->Jupiter}

It looks like if you call it a digraph, and give it each of the start/finish points, it'll graph them for you.

I'll be back with more as I discover more.

Friday, February 10, 2012

TrueCrypt

I was having problems getting truecrypt to work on my ubuntu machine. Once I downloaded it and unzipped it there was no extension and for some reason my machine didn't know what to do with it. I simply renamed it with a .sh at the end and re-ran the file, everything worked as advertised at that point.
I.E. FROM: truecrypt-7.1a-setup-x86
      TO: truecrypt-7.1a-setup-x86.sh


Good Luck

Tuesday, January 10, 2012

Android Like SQL Statement, sqllite sql software

While using sqllite sql software for android, I was runing into problems. I was able to do exact matches, but for some reason I couldn't get LIKE matches to work. I was hunting around like crazy trying to figure out how to write a LIKE statement in Android. I had a working example of '=' and figured it would be pretty simple to use the LIKE command. After many variations I just couldn't figure it out. I almost posted to stackoverflow but decided to read the information on the "rawQuery" parameters of the database methods.

After doing a little reading it turns out that if you enter in a ? in your query it pulls the selection arguments and stuffs them in where that ? is. My first example didn't exactly make it clear that ? was replaced with the arguments, so now that I knew that it led me to my next train of thought.

As silly as this might sound my original query that pulled exact matches in used this:
cursor = db.query(DBHelper.TABLE, null, DBHelper.C_TEXT + " =?", whereArgs, null, null,null);

I finally tried this:
cursor = db.query(DBHelper.TABLE, null, DBHelper.C_TEXT + " LIKE ?", whereArgs, null, null,null);

Guess what? The whereArgs get stuffed in the ? position.

As much as I could have sworn I tried this, apparently I tried various iterations. Be sure if you're going to  use this, make sure you have "column" space "LIKE" space "?" and then your arguments as the next parameter. If you notice that the first example I was using had no space between the front, between the = and ? as well as behind, this is part of what tripped me up, LIKE is a word so you have to be sure to separate them using the right spaces.

If you're not sure what a column is take a look at W3 Schools SQL Like Tutorial. It doesn't explain the ? and how that works with respect to android. But after reading to this point you should be able to use the LIKE command in android.

I hope this helps others, so long and short of it, I can now query my db with a like operator, and it works.

Note* whereArgs contains only one element which contains % on either side in my case (%string%). Perhaps in the future I'll poke around at the options when I have more then one string in that field.

Monday, October 17, 2011

Music Glove Part 1

Well after much heartburn (and once again being burned by pull down resistors). I have managed to make some progress.

I now have half of my Music Glove prototype complete. This is the hardware side. I paired a bluetooth module from sparkfun (something that does uart). With an arduino to handle the button presses. I can now receive an A and a B from my button presses on my phone using the BlueTerm application.

Once I am able to handle receiving the bluetooth data on the phone side, I will better package the whole assembly.

Here is a shot of the circuit:




Here is the code which does all the work:

/*
  What you will need to do is wire up your bluetooth module hooking the RX and TX up to the arduino, then

 */

// A is IO 2
int A = 2;              // Switch connected to digital pin 2
int AState = 0;              // Switch connected to digital pin 2
int AStatePrev = 0;              // Switch connected to digital pin 2
// B is IO 3
int B = 3;              // Switch connected to digital pin 3
int BState = 0;              // Switch connected to digital pin 3
int BStatePrev = 0;              // Switch connected to digital pin 3


int buttonState = 0;


void setup()                    // run once, when the sketch starts
{
  Serial.begin(115200);           // set up Serial library at 115200 bps
  pinMode(A, INPUT);    // sets the digital pin as input to read switch
  pinMode(B, INPUT);    // sets the digital pin as input to read switch
}


void loop()                     // run over and over again
{
  AStatePrev = digitalRead(A);              // Switch connected to digital pin 2
  BStatePrev = digitalRead(B);              // Switch connected to digital pin 3
  delay(60);
  BState = digitalRead(B);              // Switch connected to digital pin 3
  AState = digitalRead(A);              // Switch connected to digital pin 2

  // Handle button "2" presses 
  if (BStatePrev==BState)
  {
    if (BState == HIGH)
    {
      Serial.println("B");
      delay(60);
    }
  }
  // Handle button "1" presses
  if (AStatePrev==AState)
  {
    if (AState == HIGH)
    {
      Serial.println("A");
      delay(60);
    }
  }

}

Tuesday, August 16, 2011

Folder Permissions

I've been having problems with folder permissions on ubuntu, oddly enough, trying chmod 777 on stuff wasn't really working well, basically I just did gksu nautilus then I modeled the "permissions" tab to the same as a folder I wanted permissions to be the same as. Apply to all files/folders within and viola it worked.

Hope that helps others.

Originally found the command from http://ubuntuforums.org/showthread.php?t=1086624 although I have heard of it before, just haven't used it much.

Tuesday, August 9, 2011

Boxee Update DEB

I was hoping to download boxee for my recently updated media server.

Turns out that boxee has some issues with the package downloadable from here:
http://www.boxee.tv/make#content

The original site I built the script to mimic is located in the source code.

The file which did the job for me can be found here:
Boxee Deb Fix


Good luck!

Friday, July 29, 2011

getc/getchar why doesn't it wait?

So getc is basically the same as getchar but to get the same effect
getc(stdin) = getchar basically.
From the above link:
Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced by one character to point to the next character.

What this means is that this is not a "wait for input" type operation, it literally will grab the next character from the stdin stream unless you find a way to prevent it,


Example function from code to be used:

char getMathSymbol()
{
while (getchar() == '\n') // use this or getchar will not wait for input.
{
// temp varaible
char userSymbol;
cout << "please enter yoru math symbol" << endl;
// need getchar to get a character from the console
      userSymbol = getchar();
      
//cout << "getchar done" << endl; // debug code
// return your symbol
return userSymbol;
}
}

For Example you entered in the following:


Enter your first number
1
Enter your second number
2
please enter yoru math symbol
+
3


Lets look at this again, with all (hidden) characters visible
Enter your first number\n
1\n
Enter your second number\n
2\n
please enter yoru math symbol\n
+\n
3



When you hit getMathSymbol
you are at this point:
Enter your first number\n
1\n
Enter your second number\n
2\n
please enter yoru math symbol\n

When you get the char, the \n is grabbed (which is why the "while loop works").

So what I suspect happens is that we hit that \n and since it's a valid character (not end of line or end of file) we grab the character and exit the function. (when the while loop is gone).

What happens if we read the last character to increment to the next char (which is EOF or EOL)?

char getMathSymbol()
{
//while (getchar() == '\n') // use this or getchar will not wait for input.
{
// temp varaible
char userSymbol;
cout << "please enter yoru math symbol" << endl;
// need getchar to get a character from the console
           userSymbol = getchar();
           userSymbol = getchar();
      
//cout << "getchar done" << endl; // debug code
// return your symbol
return userSymbol;
}
}

After compiling, it looks like it works, so what happens is that if there is a valid character the getchar returns immediately, if the next char isn't valid, then it waits until a valid char occurs.

Make sense?