Sunday, January 31, 2016

Zcast, audio streaming in Twitter

A new app for streaming live content as appeared in the Apple AppStore: Zcast.  It's basically Periscope but in audio format only.

This is an interesting app as you may want to stream live content but do not want to show yourself or do not have time to setup a recording studio in your house.  Groups can be created and your audience can interact live with your streaming and participate as guests.


Zcast is brand new so there is not currently a lot of content, as I write this post.  I will give it a try later today.  We'll see how this new podcasting media will workout.  Personally, I have an interest of streaming live while I do practice my guitar every now and then.

Search for "Zcast" in the AppStore for the iPhone.  For now, an Android version is still not available.

Have a great day!


Monday, January 25, 2016

What is a null value?

A while ago, my kids were asking me about my work as a software programmer.  They were trying to understand what I was doing exactly all day long, "playing" on a computer with multiple displays.

They were understanding the concept of creating a software by writing coded instructions  but were actually baffled about the concept of variables.  It took a lot of explanations and examples to describe what was a variable but I finally was able to translate my mumbo-jumbo into bags of candies.  

The example was that at any moment, I wanted to keep count of how many laps a racing car had completed around the race track.  Each time the car does a lap, we add another candy into the bag.  The variable is the bag and the value is the amount of candies in the bag.  Technically, we have an Integer, meaning that this bag can only represents whole numbers.  

As we were discussing variable types (integer, strings, double,...), I mentioned that any variable must be initialized before using it or else, we would have a "null" value...  At that moment, I had lost them completely.  One of them replied that a null value is zero (0) so why have a name for a zero value?

The problem here is that I was explaining the concept of variables using quantities.  Saying that the bag had no candies in it or that the bag had zero candies in it, was the same thing.  They were right. I then used a pen and a piece of paper to explain the null value.

I wrote down the number 23 and asked them of many laps was represented.  They all answered twenty-three.  Then I erased the value and wrote 0 (zero).  Again I asked them how many laps and some answered zero, others said none.

Then I erased the number zero and left the piece of paper blank. 
- How many laps now?
- We don't know, you haven't written anything...
- That is a NULL value...

Then they started arguing that "zero" and "blank" was the same thing.  

- Wrong!  Writing "0" means that I had check how many laps were completed and counted no laps so the number of laps completed was "0".  This is an identifiable value.  When nothing was written, it meant that I did not count in any way the number of laps.  No value was available thus leaving the piece of paper blank.

I would have continued explaining the memory allocation or the joy of arrays but that was already too much information for them and they considered that my job must be very dull and complex.

At last, now, they do not think that I am gaming all day long...

Have a great week!

Friday, January 22, 2016

Transform your Pebble Watch into a mouse

Here's a neat little project that I created for my Pebble smartwatch:  MouseRock.

No it's not about Rock'n Roll but about using the Pebble as a mouse for a computer.  I must admit, it's not a very efficient way to interact with a computer but it was fun to experiment with.

The Pebble App

Use CloudPebble.net to create the watch app in Javascript.  Nothing fancy but the following source code was created using Pebble.js

var UI = require('ui');
var serverIP = "http://192.168.0.121:8000";
var Accel = require('ui/accel');

serverIP = localStorage.getItem("serverip");
Accel.config({
  rate:10,
  samples: 1,
  subscribe:true
});
Accel.on('data', function(e) {
  var x = 0;
  var y = 0;
  if (e.accel.x  > 200) x = e.accel.x / 50;
  if (e.accel.y  > 200) y = e.accel.y / 50 *-1;
  if (e.accel.x  < -200) x = e.accel.x / 50;
  if (e.accel.y  < -200) y = e.accel.y / 50 *-1;
  if (x !== 0 || y !== 0){
    //console.log("Z: " + e.accel.z);
    getXML(serverIP + "/?mousex="+x+"&mousey="+y);
  }

});
function getXML(url) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
var main = new UI.Card({
  title: 'PebbleRock',
  icon: 'images/menu_icon.png',
  subtitle: 'Current server',
  body: serverIP,
  subtitleColor: 'indigo', // Named colors
  bodyColor: '#9a0036' // Hex colors
});
main.show();
main.on("click","up",function(){
  getXML(serverIP + "/?mouseclick=1");
});
main.on("click","down",function(){
  getXML(serverIP + "/?mouserightclick=1");
});
main.on("click","select",function(){

});
The basic idea is to retrieve the data from the accelerometer and send the information to the computer via the HTTP protocol.  Some tweaking has been included for a better mouse handling.  Left and Right buttons have been integrated using the "click" event...

The MouseRock Server

Now that you've created the watch app, you need to create the computer server to handle the data from the Pebble.  Again, nothing fancy, just a simple HTTP server built in Java.  Here's the source code...

package pebblemouse;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
/**
 *
 * @author patrick
 */
public class MouseRock {
    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(null); // creates a default executor
        System.out.println("MouseRock Server:");
        System.out.println("Server Address: http://" + InetAddress.getLocalHost().getHostName() + ":" + 8000);
        System.out.println("----------------------------");
        System.out.println("Enter this URL into your MouseRock configuration page.");
        server.start();
    }
    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            try {
                java.awt.Robot robot = new java.awt.Robot();
                int currentX = (int) java.awt.MouseInfo.getPointerInfo().getLocation().getX();
                int currentY = (int) java.awt.MouseInfo.getPointerInfo().getLocation().getY();
                String response = "Command received...";
                if (t.getRequestURI().getQuery() != null) {
                    Map<String, String> maps = queryToMap(t.getRequestURI().getQuery());
                    for (String param : maps.keySet()) {
                        String value = maps.get(param);
                        switch (param) {
                            case "mousex":
                                int x = new Double(value).intValue();
                                currentX += x;
                                robot.mouseMove(currentX, currentY);
                                //System.out.println("Mouse X");
                                break;
                            case "mousey":
                                int y = new Double(value).intValue();
                                currentY += y;
                                robot.mouseMove(currentX, currentY);
                                //System.out.println("Mouse Y");
                                break;
                            case "mouseclick":
                                robot.mousePress(InputEvent.BUTTON1_MASK);
                                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                System.out.println("Mouse Clicked");
                                break;
                            case "mouserightclick":
                                robot.mousePress(InputEvent.BUTTON3_MASK);
                                robot.mouseRelease(InputEvent.BUTTON3_MASK);
                                System.out.println("Mouse Right Clicked");
                                break;
                        }
                    }
                }
                t.sendResponseHeaders(200, response.length());
                OutputStream os = t.getResponseBody();
                os.write(response.getBytes());
                os.close();
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    public static Map<String, String> queryToMap(String query) {
        Map<String, String> result = new HashMap<>();
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            if (pair.length > 1) {
                result.put(pair[0], pair[1]);
            } else {
                result.put(pair[0], "");
            }
        }
        return result;
    }
}
What the server does is get the Mouse X, Mouse Y values sent from the Pebble smartwatch and uses the Robot class to move the mouse.  The server is moving the mouse by adding/subtracting a value to the current mouse location.  Since the Pebble watch will send a lot of data, those values must be small increments.  Buttons were also handled by activating them when the "Up" and "Down" were pressed on the Pebble.

Since the server was coded in Java, it should work on any operating system: Linux, Windows or OSX.

The Result

This hack won't replace a real mouse as it is a bit cumbersome to use.  But it's a fun experiment to create a motion activated mouse.

Here's a video of the experiment:



Sunday, January 10, 2016

The new Apple TV 4, is it worth it?

For Christmas, we bought the new Apple TV 4 as a family gift.  We already had the previous model, the Apple TV 3 but having the possibility to play a few games on the big screen was appealing.

The main usage in our home for an Apple TV is to watch some movies over Netflix, explore the newest YouTube videos and rent a movie on iTunes, once in a while.  Nothing fancy in general.  We do have a NAS drive in the network where we keep our old DVD movies (ripped into MP4 files) as some of them are starting to degrade with time.  Of course, some home movies and pictures are also part of the collection.

As for gaming, we are casual gamers.  We've been considering having a gaming console in the living room for a while but investing 400$ into a device that we would rarely use was a major issue.  And considering the price of a brand new game at 80$ was out of the question.  What we wanted was something simple, not too pricy and fun.  For our family, the Apple TV 4 was the perfect fit. 



We selected the entry model (32 gigs) as it was on sale at 179$.  

The initial setup was easy to execute and in a matter of minutes, we were already browsing the AppStore for something cool to try out.  You probably already know about the technical details of this new device so I won't go over them again.  It's slick, it's cool and it works!

The pros:

- The remote is connected with Bluetooth so no more pointing to the device.  This is quite handy as we can hide the Apple TV 4 behind the TV set, having less stuff on the tv furniture.  
- The AppStore already has tons of apps and games available for free so you already have a major entertainment value out-of-the-box.
- Apps and games are at the same price as those for your iPhone/iPad.  The priciest games we could find was around 12$ which is less than a used Xbox game.
- Some apps/games that you already bought for your iPhone/iPad are available for free has the purchase is universal.  This is a major advantage as we had already a few games available without shelling out more money.
- Navigation is intuitive and easy to use.  Even our 7 years old got the hang of it in just a few minutes.
- The iOS Remote app is compatible meaning that you can still navigate on your Apple TV while the main remote controller is charging.
- Plex and a few other multimedia apps are available to play your content from your NAS drive. (Finally!)
- Bluetooth headset can be used thus letting someone watch a movie without disturbing the others.
- Some games do support multiplayer mode, relying on iOS devices as remote controllers.  Instead of having kids blinded by their iPod, they were all playing together in the living room having fun and interacting with each other.

The cons:

- You need to buy dedicate gaming controllers for iOS and they are not easy to find at your local store. (At least in my home town)
- The in-app purchases in some games are annoying and it's easy to slip with the touch remote control  when selecting a new game.
- Major titles are still missing in the AppStore. (Probably it will take a few months to get on par with the iOS AppStore).
- Apps cannot be organized into sub-folders like on iOS.  It can get messy as you add more and more apps or games on the main screen.
- Also, you cannot have multiple pages of your main screen as on iOS.  You end up with a really long list of apps.
- Siri is not available in all country or languages.  This is a bit annoying for it is probably a matter of months. (We speak French Canadian at home, supported on iOS but not on tvOS)
- Text entry is really annoying and Bluetooth keyboard is not compatible.  The best option for now is to use the Remote app on an iOS device.
- Multi-users for the Game Center do not seem to be supported, meaning that achievements are all under my personal account.

Overall?

If you're not into gaming but looking for an all-in-one entertainment system, the Apple TV 4 is a sure bet.  Some features are still lacking but should be available eventually as the new ecosystem will mature.  Having an extra (more classic) controller is optional but strongly recommended in some games.

For the initial price and considering the cost of the games, it's way cheaper than an Xbox, PS4 or a WII-U.  No CD/DVD to handle, completely integrated into the Apple ecosystem and much smaller than any devices connectors to your TV set (beside the Chromecast, of course).

If your are considering purchasing the Apple TV 4, go for it.  Especially if you already have iPhones or iPads.