Wednesday, September 24, 2014

Random Numbers

Generating random numbers isn't the most straight forward. Here is the correct way to do it in C/C++:

Windows:

int randomNumber;

if (rand_s(&randomNumber))
{
    return randomNumber;

}
else
{
    //TODO error
}


Unix/Linux:

int randomNumber;
int fd = fopen("/dev/random", O_RDONLY));

if (fd > 0 && read(fd, &randomNumber, 4))
{
    return randomNumber;
}
else
{
    //TODO error
}

NOTE: On Linux don't call this too many times because the pool of numbers is limited.

Friday, September 19, 2014

Talk Like a Pirate Day

Friday, September 19 is International Talk Like a Pirate Day 2014.


Check out the Original Talk Like a Pirate Day website:

http://www.talklikeapirate.com/piratehome.html

I know this is one of my favorite holidays!

Thursday, September 18, 2014

Can you Depend on Cloud Solutions?

Cloud solutions are great. They usually are free, or pretty inexpensive for what you get. However you do give the company access to your data which is how they are making money most of the time. But my point of this post isn't too discuss the nuances of how the companies make money or whether you should give the companies your data for free in exchange for a useful tool. My point is should you even depend on them? Most of them are touting themselves as more reliable than your desktop and your own backup system. It's true that the best backup is the one that you do, but you also have to be able to get to your data. Obviously the choice depends on what you are putting into the cloud because of reliability of the service and the sensitive nature of the data.

If you are a student writing a term paper for instance, Google Drive, Apple iCloud, or Microsoft office 365 are all free and are very compelling. However OpenOffice and Apple Pages are also free, and if something goes wrong your term paper won't be bit dust.

What made me think of this is Apple just rolled out iOS 8, and in doing so upgraded iCloud to iCloud Drive. Brilliant because now it's more on par with Google Drive, however the fine print says that when upgrading to iCloud dries only iOS 8 and OS X Yosemite support iCloud Drive. Only developers are running Yosemite so this seems like a rather odd thing to do. So until Yosemite is out I guess I won't be using iCloud.

Apple Calendar Stuck in Unusable State after Changing Password

After changing my password for my CalDAV server I started getting this error: "The server responded with an error. The server did not recognize your user name or password for account "." Make sure you enter them correctly." Typing in the new password just displays the dialog again, but I was able to successfully fix this issue by resetting the Keychain. Go t0 > Utilities > Keychain Access > Preferences > Reset My Default Keychain It also works to remove all keychain entries for that specific server to be more selective about it. Note: I'm running Mavericks, but I've run into this with Lion and Mountain Lion.

Wednesday, September 17, 2014

I'm Still Here (and Alive)

I haven't posted in some time so a few readers (and friends) have contacted me to see if I'm still alive. I'm happy to report that I am alive and I'll be posting more often. The last few years have been, well they've been extremely busy and a lot of things have come up that have been, well, more important than this blog. But, that's about to change so stay tuned!

Monday, October 28, 2013

Disable Java 7 and 8 in the Browser Manually on Mac

To disable Java 7 and up in the browser on Mac open Terminal and run the following three commands: 1. Change directories to the Oracle Application support directory.

cd /Library/Application\ Support/Oracle/Java
2. Remove the Info.plist (ls -al should show that it points to the Enabled.plist)
sudo rm -r -f Info.plist
3. Create a new Info.plist that points to the Disabled.plist
sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Disabled.plist Info.plist
This will completely disable the Java plugin from running from within the browser. You can also do this from the Java Control Panel, but there are times when this is preferred. Note that this change will be remembered after upgrades because of how the installer works it will not modify this symlink, it will only create it if it does not exist and point it to the Enabled.plist.

Tuesday, May 21, 2013

Arduino

This last weekend I went to the Maker's Fair and got a free breadboard Arduino. What a swag score After making a blinky light I decided to order a few parts from SparkFun Electronics and decided to toy around with a 7-segment display (I now wished I had gotten something a bit more challenging, but hey, I haven't done electronics in a long time). I followed the tutorial here and noticed many Arduino examples don't use objects. So, here is a version of the SparkFun 7-segment display example using objects.

/* Serial 7-Segment Display Example Code
    Serial Mode Stopwatch
   by: Jim Lindblom
     SparkFun Electronics
   date: November 27, 2012
   license: This code is public domain.
   
   This example code shows how you could use software serial
   Arduino library to interface with a Serial 7-Segment Display.
   
   There are example functions for setting the display's
   brightness, decimals and clearing the display.
   
   The print function is used with the SoftwareSerial library
   to send display data to the S7S.
   
   Circuit:
   Arduino -------------- Serial 7-Segment
     5V   --------------------  VCC
     GND  --------------------  GND
      8   --------------------  RX
*/
#include 

class LED
{
  private:
    // These are the Arduino pins required to create a software seiral
    //  instance. We'll actually only use the TX pin.
    int softwareTx;
    int softwareRx;
    
    SoftwareSerial *s7s;
    
  public:
    LED()
    {
      s7s = NULL;
    }
    
    void initialize()
    {
        softwareTx = 8;
        softwareRx = 7;
        
        s7s = new SoftwareSerial(softwareRx, softwareTx);
        
        // Must begin s7s software serial at the correct baud rate.
        //  The default of the s7s is 9600.
        s7s->begin(9600);
        
        // Clear the display, and then turn on all segments and decimals
        clearDisplay();  // Clears display, resets cursor
        s7s->print("-HI-");  // Displays -HI- on all digits
        setDecimals(0b111111);  // Turn on all decimals, colon, apos
        
        // Flash brightness values at the beginning
        setBrightness(0);  // Lowest brightness
        delay(1500);
        setBrightness(127);  // Medium brightness
        delay(1500);
        setBrightness(255);  // High brightness
        delay(1500);
        
        // Clear the display before jumping into loop
        clearDisplay();
    }
    
    ~LED()
    {
      if (s7s != NULL)
        delete s7s;
    }
    
    void printDisplay(int value)
    {
        char tempString[10];  // Will be used with sprintf to create strings
        
        // Magical sprintf creates a string for us to send to the s7s.
        //  The %4d option creates a 4-digit integer.
        sprintf(tempString, "%4d", value);
        
        // This will output the tempString to the S7S
        s7s->print(tempString);
    }
    
    // Send the clear display command (0x76)
    //  This will clear the display and reset the cursor
    void clearDisplay()
    {
      s7s->write(0x76);  // Clear display command
    }
    
    // Set the displays brightness. Should receive byte with the value
    //  to set the brightness to
    //  dimmest------------->brightest
    //     0--------127--------255
    void setBrightness(byte value)
    {
      s7s->write(0x7A);  // Set brightness command byte
      s7s->write(value);  // brightness data byte
    }
    
    // Turn on any, none, or all of the decimals.
    //  The six lowest bits in the decimals parameter sets a decimal 
    //  (or colon, or apostrophe) on or off. A 1 indicates on, 0 off.
    //  [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
    //
    // Example: setDecimals(0b00000100);  // Sets digit 3 decimal on
    void setDecimals(byte decimals)
    {
      s7s->write(0x77);
      s7s->write(decimals);
    }
};

unsigned int counter = 0;  // This variable will count up to 65k
LED Led;

void setup()
{
  Led.initialize();
}

void loop()
{
  Led.printDisplay(counter);
  Led.setDecimals(0b00000100);  // Sets digit 3 decimal on
  counter++;  // Increment the counter
  delay(100);  // This will make the display update at 10Hz.
}

Sunday, September 30, 2012

JavaOne 2012

I'll be at the JavaFX booth and the talks about Java on the Mac so come up and say "hi!" if you're around!

Thursday, July 26, 2012

Make the Apple Magic Trackpad Ergonomic

I've been using the Apple Magic Trackpad and found that it is not terribly ergonomic because it slants up to match the keyboard angle which forces the wrist to slant upwards. The Magic Trackpad can be made much more ergonomic by simply turning it around (I got my directions from here). In order to turn it around the coordinate system must be reoriented. To do that, type this into the terminal:

defaults write com.apple.trackpad.orientation TrackpadOrientationMode 1

sudo defaults write com.apple.MultitouchSupport ForceAutoOrientation YES
NOTE: Both commands might not be required but I couldn't get it to work otherwise on Lion or Mountain Lion. Then disconnect the Magic Trackpad, shut it off, reboot the Mac, reorient the Magic Trackpad and until the system is booted. Then turn on the Magic Trackpad and rest 5 finger tips spread out (like you are playing piano) on the Magic Trackpad and move them away from you. Viola! I have also found the settings tap-to-click and 3-finger drag to be useful and can be enabled in the trackpad control panel. To Remove the settings type:
defaults remove com.apple.trackpad.orientation TrackpadOrientationMode
sudo defaults write com.apple.MultitouchSupport ForceAutoOrientation NO

Monday, June 25, 2012

Virtual Box VM to VMWare

I prefer either Parallels or VMWare over Virtual Box. Virtual Box is free though. But at 50 bucks, video acceleration is nice to have when working with graphics. So here are steps that work as of today to convert a Virtual Box VM to a VMWare VM. These steps are out there on the interwebs and they aren't difficult but I didn't find them in one place so hopefully this will help someone.


1. From Virtual Box select your VM and choose File | Export Appliance...
2. Download and install VMWare OVF Tool from http://www.vmware.com/support/developer/ovf/
3. Run the OVF Tool on you .ova file (I'm on a Mac):

"/Applications/VMware OVF Tool/ovftool" --lax image.ova /Users/[username]/Downloads
NOTE: Use the --lax option to get past some errors that will stop the conversion in their tracks. 4. Drag and drop into VMWare and before you do anything increase the available RAM and processor to at least 2 if you can. Otherwise the Vm will be super sluggish. 5. Run in VMWare!

Monday, June 18, 2012

JavaFX Native Packaging

One of the benefits of native programming languages has been fairly easy deployment and bundling. In many cases static libraries one single EXE. Programming languages that depend on a run-time such as C#, Java, Python, Perl, etc, all require that run-time to be pre installed and the total size (Application + run-time) is typically larger than a native application. Well, I think much of that is over with JDK 7u6 currently in beta. Rich client JavaFX applications can bundle the JRE and your application and provide a very easy installation. So easy in fact it's silly. I love making complicated things super easy! Check out this blog post by one of my colleagues for all the nitty gritty details: https://blogs.oracle.com/talkingjavadeployment/entry/native_packaging_for_javafx

Monday, May 14, 2012

JDK 7 Beans.setDesignTime/Beans.isDesignTime

JDK 7 introduces a change to Beans.setDesignTime(bool value) and Beans.isDesignTime() where the design-time state instead of application global is now a thread group local variable. It's so multiple frameworks can be used simultaneously and they can be either design-time or run-time as needed. Basically it is an IDE requirement. Needless to say this change is a bit unorthodox with a bizarre side effect and most will want to bring back the old behavior. The simplest way of doing this is to iterate over each thread group and set the design-time state.

  Runnable runnable = new Runnable()
  {
    public void run()
    {
      Beans.setDesignTime(true);
    }
  };

  ThreadGroup root = Thread.currentThread().getThreadGroup();
 
  while (root.getParent() != null)
    root = root.getParent();

  ThreadGroup[] threadGroups = new ThreadGroup[root.activeGroupCount()];
  // ThreadGroup.enumerate copies all ThreadGroup subgroups, not including the root ThreadGroup.
  root.enumerate(threadGroups, true);

  new Thread(root, runnable).start();

  for (ThreadGroup group : threadGroups)
    new Thread(group, runnable).start();
If any thread groups are created after this code is run then design-time will not be set. So the best practices are all thread groups need to have design-time set, or any calls to Beans.isDesignTime() need to be synchronized to the EDT.