Wear Mandelbrot – Smooth Colouring

Colouring the Mandelbrot Set is relatively easy, you choose a different colour for each iteration value. This gives the visual effect where the Mandelbrot Set has solid bands of colour, like so:

Mandelbrot Set coloured using iteration value

Now this looks okay, but it’s possible to produce a more aesthetically pleasing image by using an algorithm to produce a smooth gradient, like in this picture:

Same portion of the set with Smooth Colouring

Comparing the two, the later is definitely preferable! (NOTE: back when these fractals were first being rendered there weren’t computers with 16 million colour palettes, typically restricted to just 16 often garish options, so this kind of smoothing wasn’t feasible)

We achieve this by seeing “how far” the x-y pair is into the colour band, in essence calculating a fractional part for the iteration count. For our Android Wear app we’ve  used a algorithm called the “Normalized Iteration Count” which uses the number of iterations to calculate a value between 0 and 1. This value can then be used to interpolate the colours across the band, creating a colour gradient.

The equation to calculate this value is such: nu = iter -  log(log(sqrt(x*x + y*y)/ log(2))/log(2)

The quickest way to grab the fractional part of the value in java is just to cast the double to an int and subtract it from the original value, i.e. double nu_frac =  nu - (int)nu. Now you have a number between 0 and 1 which you can use to interpolate between the colour for the iteration value and the iteration value + 1, like so:

int r = (int)(Color.red(colours[iter]) * (1 - nu_frac) + Color.red(colours[(iter + 1)]) * nu_frac);
int g = (int)(Color.green(colours[iter]) * (1 - nu_frac) + Color.green(colours[(iter + 1)]) * nu_frac);
int b = (int)(Color.blue(colours[iter]) * (1 - nu_frac) + Color.blue(colours[(iter + 1)]) * nu_frac);

That’s all you need to get smooth colouring!

Wear Mandelbrot

The Mandelbrot Set is formed by iterating a seed x-y pair of numbers, which represent a complex number, using the quadratic equation: z=(z*z)+z. The number of iterations, or the amount of “time”, this loop is performed defines whether the two values represent a point that belongs in the Mandelbrot Set – and for graphical purposes which colour to render the given pixel as.

device-2014-09-09-165405

Mandelbrot Set with the ‘Rainbow’ colours

  • Reaching the maximum number of iterations
  • The calculated value z becomes greater than a ‘breakout value’

If the number of iterations tends to infinity then the x-y pair of values is said to belong to the Mandelbrot Set. Programmatically this can be said to happen if the iterative loop reaches a pre-defined maximum number of iterations (for example, 255).

But if you render the Mandelbrot Set just as pixels that are ‘in’ or ‘out’ then you end up with a monochrome image that looks a bit like an island in a sea. Much like a coastline it’s possible to zoom in on any section and find more and more detail as you move in. More visually impressive is if you render the number of iterations that it takes to exceed a ‘breakout value’ (typically 4) as a colour. As you tend away from the Set the number of iterations decreases and you see bands of colour, almost as though the Mandelbrot set was a high mountain and the height drops as you move away from the set itself.

The Android Wear app calculates the Mandelbrot initially with a zoom level which shows the whole set. Tapping the screen zooms in, making the new centre the position where you tapped. In essence you are taking a subset of the original image and blowing it up to the full resolution of the screen again. The fractal nature of the Mandelbrot Set allows you to do this infinitely, although in practise eventually computers lack the accuracy to represent the numbers involved precisely enough, meaning that there’s a practical limit. Double-tapping will undo the last zoom and pull you out from the Mandelbrot.

Corresponding Julia set

Swiping up and down on the watch app will cycle through a handful of pre-defined colour pallets that we chose to show off the Set in its best light. Swiping to the left will use the centre of the current Mandelbrot Set to calculate a Julia Set and display it. A Julia set is a similar fractal pattern that is based on using a seed pair of values from the Mandelbrot Set and essentially using the same equation but offsetting using the value pairs. Swiping right will put you back at the same place you were in the Mandelbrot Set.

In many ways the smart watch form factor is ideal for the Mandelbrot because the screen only has a relatively small number of pixels, making rendering a screen’s-worth of pixels only take a second or two. High-end phones have around 27 times more pixels so the app on the phone (which we’ve developed but not released yet) takes substantially more time to calculate the Mandelbrot Set.

Footnote: I find it stunning to see how fast these Android Wear watches can render the Mandelbrot Set. When I was at school we were drawing these fractals using the BBC Model B computer and you could sit and watch each line being rendered. Now a whole screen pops up after barely a couple of seconds. The raw processing power that’s paired with a full colour display and small enough to fit on your wrist is impressive. (Jonathan Howell)

SciCalc: a scientific calculator for android wear

SciCalc first screen

SciCalc first screen

When looking to make the world a better place the key is often to find a problem and then come up with the solution to it. With our newly-purchased Android Wear watches we swiftly found that a calculator was a handy little addition to have. Sadly we also noticed that the Android Wear calculators available at the time from the Google Play Store were lacking in functions. Often not offering more than the basic four operators.

So we decided to write a better calculator to supply this gap in the market. Our calculator, dubbed SciCalc, supports many functions including: x cubed, x to the power, inverse, sin, cos, and tan.

Given the small dimensions of the watch display we felt that the most which could reasonably be fitted on the first screen were the basic functions. So we employed a swipe feature to flip over to a separate ‘advanced functions’ screen. Swiping left again would bring back the home screen. The idea was to allow extra pages to be slipped in whilst always using ‘swipe left’, leading to a wrap-around layout rather than the bouncing left-right that some other apps use.

SciCalc: advanced operators

SciCalc: advanced operators

Some of the challenges that I had with building the app included a problem concerning precision in dividing and multiplying. At first I used doubles which caused problems as the calculator would say 3.0000000000001 when it should say 3. This was solved by using BigDecimals. The inaccuracy error would come about because in Java the value 0.1 couldn’t be represented precisely as a double but using BigDecimal meant that it could.

Another problem was that as you cube a number it can become very big and you can easily fill up the amount of digits the screen can hold sensibly, 13 digits. So I had to turn to scientific notation or it wouldn’t be possible to read all of the digits in the number. Sadly, BigDecimal doesn’t have this feature so I had to put in a check for the length of the number and then convert it to a float if it had more than 8 digits. This conversion to a float automatically changed it to use scientific notation.

You can find the SciCalc app in the Google Play Store

Ireckon – what do you reckon?

One month on from the launch of Ireckon’s first question we’d like to share some of the results that we’ve had so far. Although we’re always hoping that more people will find the app and join the fun. Let’s find out what we all reckon about things!

If you’ve not seen the app before then take a look at Ireckon at the Google Play store.

Most desirable smart watch?

  1. LG G Watch – 65.3%
  2. Samsung Gear Live – 30.6%
  3. Sony Smart Watch 2 – 2.8%
  4. Pebble – 1.4%

A surprisingly strong turn out for the LG G Watch. When we played with both watches, everyone here preferred the Samsung over the LG.

Who is your favourite Harry?

  1. Potter – 61.9%
  2. Houdini – 25.4%
  3. Prince – 7.9%
  4. Styles – 4.8%

Potter rules the world it appears, but fantastic to see Houdini in second place. Although if Harry Styles wants to replicate Houdini’s suspended strait jacket escape then I’ll happily change my vote.

Who made the best Bond?

  1. Sean Connery – 36.6%
  2. Daniel Craig – 26.8%
  3. Pierce Brosnan – 19.5%
  4. Roger Moore – 17.1%

The original and the best, Mr. Connery showing the later Bonds how it’s done. But it was a difficult question to answer as they all make great James Bonds.

Which is the best phone?

  1. HTC One (M8) – 46.7%
  2. Galaxy S5 – 33.3%
  3. iPhone 5S – 13.3%
  4. Lumia 930 – 6.7%

Ahhh, the HTC One (M8), the best phone you never bought. You have to feel sorry for HTC when Samsung achieves massive sales figures while the world at large prefers the One.

Which is your favourite Disney princess?

  1. Belle – 38.1%
  2. Cinderella – 33.3%
  3. Snow White – 19.0%
  4. Sleeping Beauty – 9.5%

Belle is indeed a charmer, but never forget that Snow White has seven little live-in helpers to do all the housework.

If you’d like to check the latest scores head over to Ireckon at Fife-V

Are there any questions that you’d like to ask? Get in touch at ireckon@fife-v.com

Samsung Gear Live review

Smart watches have already come a long way and now a new OS, Android Wear, has arrived on the scene to shake things up. At the moment you’ve got a limited choice with only the Samsung Gear Live and LG G Watch currently on sale.

Samsung's Gear Live smart watch

Samsung’s Gear Live smart watch

I’ve had the pleasure of being able to give the Samsung Gear Live a test and as a first generation device it’s actually pretty impressive.

Let’s kick off with the nitty gritty, it has a 1.63″ Super AMOLED screen with vibrant colours and a high pixel density (320 x 320). The processor is a 1.2GHz quad-core Snapdragon 400, like the LG G Watch. It has 512MB of RAM with 4GB of internal memory. It’s pretty much a smartphone on your wrist.

From a pragmatic point of view its IP67 rated, giving you high levels of dust and water resistance (no one wants a watch that you have to protect in the rain). The big addition of the Gear Live over the competition is the inclusion of a heart rate monitor. I tried it out and whilst I didn’t have a way of testing its absolute accuracy it certainly seemed to give approximately accurate numbers and it climbed to a realistic after exercise. The watch does warn that it’s not to be relied upon for medical purposes.

But what is it actually like to live with? Pairing with your phone is simple. Download the Android Wear app for your mobile, switch on Bluetooth, and the pairing process happens automatically (no need to type in codes, just check the codes match and click ‘ok’).

From that moment on your watch is now the convenient way to know what’s happening on your phone. Any notification that pops up on your phone – whether it be a new email, text message, or Google now card – will appear on your watch, accompanied with a gentle vibrate. These notifications can be scrolled through and dismissing on the watch, thereby dismissing them on your phone too.

The display itself can be put into a mode where it switches off until you bring it up to your face to look at it (via gyroscopic means rather than a proximity sensor) or left on all the time which I think makes it much more desirable and useful as a watch. And, unlike a traditional watch, you can download new watch faces from the Google Play store.

Samsung's Gear Live - with integrated heart rate monitor

Samsung’s Gear Live – with integrated heart rate monitor

This is the big difference between an Android phone and the Gear Live, there isn’t a home screen as such. You can install apps but you have to navigate through a couple of menus to get at them. The primary interface is Google’s voice input, which by and large ok.

For example, you can say “OK Google” into your watch and then something like “send Jon a text”, followed by whatever you’d like to send in the text. It certainly works up to a point but since the message automatically sends after you finish speaking you’d better hope it didn’t get any of the words wrong. I wouldn’t text my boss using it, that’s for sure.

Overall I’ve enjoyed the watch. It’s not crazily expensive for a luxury watch. The notifications are something that you start to get used to and you miss them when they are gone, consigning you to checking your phone every time it vibrates in your pocket.

The only thing that would stop me from rushing out and buying one is the battery life. In all my testing I never managed to get more than a day and a half out of it. That’s not really enough for a watch. Samsung’s Tizen-based watches (the Gear 2 and Gear 2 Neo) promise a significantly higher battery life, still not stunning by any means but enough to make them more practical devices. However, if Santa Claus happened to drop a second generation version of this down my chimney for Christmas then I’d welcome it with open arms.

Problem with starting Wear apps on an idle watch

If you have an Android app running on a phone that needs to start a Wear app on a smart watch then you’ll have a problem if the Wear device is in an idle state.

For example if the phone app uses this bit of code in order to start the Wear app (by launching a new activity):

Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), START_ACTIVITY_PATH, data);

Then if the watch screen is in idle mode and the app isn’t already running then the app won’t launch. On the LG G Watch there are no error messages shown on the screen, it just jumps to displaying the “Home screen”. If the screen was already unlocked then the Wear app would start normally.

The workaround for this involved starting another activity on the watch briefly before the main activity to turn the screen on. This couldn’t do be done from the main app for an unknown reason.

So in terms  of coding, I set up a new Window Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

public class WindowActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    super.finish();
  }
}

And then made sure I called it in the WearableListenerService’s onMessageReceived method before trying to open the Wear app:

 if (path.equals(START_ACTIVITY_PATH)) {
   if (!started) {
     // reactivate the screen
     Intent startWindow = new Intent(this, WindowActivity.class);
     startWindow.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWindow);

     // now open the Wear app
     Intent startWatch = new Intent(this, Watch.class);
     startWatch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWatch);
   }
   else {
     // screen already started so just open Wear app
     Intent startWatch = new Intent(this, Watch.class);
     startWatch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startWatch);
   }
 }

Android Wear app not installing

When using Android Studio to package up an Android Wear app into a signed APK a missing manifest permission in the phone app can break the Wear app installation.

There are no errors in the gradle build or when creating the APK. You can try to install the APK and it will succeed on the phone, but the corresponding Wear app won’t install on the watch. There will be no visible errors on the watch or phone to alert you to this.

If you are debugging your application then there are errors reported in the logging but they can be very hard to spot because of the constant flow of debug coming from the watch.

The solution turned out to be that you must replicate any android permissions that the Wear app needs in the phone app’s manifest file.

 

Android services – lost messages problem

When Android runs a service it’s necessary to bind it to a application, otherwise the operating system will clean the service up. This is useful because it reduces the waste of resources. Ireckon uses a wearable listener service which means that once it has interpreted the message from the watch and sent it back, the android garbage system destroys it quickly – but not quickly enough.

The problem for Ireckon was that if a message was sent from the watch to the phone whilst the OS was in the process of destroying the service then the message would be lost.

The solution to this was to check for a response from the phone. If there wasn’t a response it was necessary to back off for a few seconds and then send the message again, repeating this if necessary.

LG G Watch – review

The LG G Watch, model LG-W100, is a first-generation Android Wear watch from LG. On first impressions the watch looks good to me, although style is personal and others might be waiting for the Motorola 360 before taking the plunge. The G Watch has a smooth rubber strap with a conventional buckle to fasten it, unlike the Samsung Gear Live which has poppers to fasten it. This, in my opinion, is quite a benefit because the conventional watch strap looks nicer, more like a standard watch, and feels less likely to come undone.

LG G Watch

LG G Watch

The LG G Watch is running the Android Wear 4.4W OS. The initial shock, compared to the phone version of Android, is that there are no apps on the home screen. The initial screen is reserved for notifications. Accessing apps is a fiddly task. In order to open a new app you have to tap the screen, scroll down to the ‘start…’ option and then scroll to the app. This might put developers off trying to make apps targeted for just the watch.

Another surprising design decision is the lack of an easily accessible power button. The watch can only be switched on either by placing it in its cradle or by pushing a very small button on the underneath of the watch with something like a paperclip. At first this seems like a big pain if you turn off the watch (to save power perhaps) and then have to turn it on later. However, you’ll find the battery will likely outlast your mobile phone and you’ll naturally fall into a charging cycle of daily topping up both your watch and phone. Just for testing purposes, I did turn it off during the day to see how easy it was to switch back on using the small button. It was very difficult and 90% of the time I didn’t have anything small enough to switch it on with. Ultimately you’ll leave it powered up all the time so it can replace your current watch, probably with one of the plethora of watch displays that are appearing on the Google Play store.

One very important thing to consider before purchasing this watch is the battery life. The battery life of this watch when using it “normally” was about a day. I could comfortably use the watch all day and still have a couple of hours left the next day, but you will need to recharge once a day. You won’t reasonably get two days out of it. This is pretty poor for a watch, but not unusual for a smart watch. However in my experience, fully charging the watch via a standard USB port took roughly 40 minutes (if you can resist playing with it for that long).

The style is very reminiscent of a development product. It certainly doesn’t scream that you just spent £160/$273 (Jul 2014). I would have expected a leather strap with the watch having a smoother look to it.

Overall I am enjoying this new approach to the android platform, making it wearable and easy to check out notifications, but until the battery can be extended to at least five days I won’t be impressed.

Ireckon Development

Ireckon is an Android Wear watch app which provides users a fun “quiz on their wrist”. They are given a stream of questions and can then vote for their favourite answer by tapping on the watch screen. One example question would be: “Who your favourite Harry?” with the possible answers being Prince Harry, Harry Styles, Harry Potter, and Harry Houdini. The totals for the answers can be viewed on the companion website http://ireckon.fife-v.com

Ireckon on a watch

Ireckon on a watch

The goal was to write a dedicated watch application rather than a standard Android app which would then have Wear capabilities added later. Many Wear apps are actually Android phone apps with minimal Wear additions, mostly using the in-built notification system.

Ireckon was designed to have no companion app visible on the phone, although there does need to be a service app running on the phone for the watch to send messages to. This “hidden” app provides the server communications, sending data from the watch to the server and returning the server’s answers to the watch.

The core communications model is based around a question-answer pair. The watch app asks for a question which the phone requests from the server. The server sends over a JSON response which the phone passes back to the watch. The phone only runs the communications service for a second or two to send requests to the server and get the JSON response, minimising the battery drain on the phone.

Ireckon question

Ireckon question

Ireckon answer

Ireckon answer

A major hurdle with the Ireckon project was trying to get the four “answer” buttons to be the same size. This is because the button’s default action is to change size depending on the text, leading to buttons of varying sizes in relation to the number of characters in the answers. Just inserting the buttons into the GridLayout didn’t work. I would have expected that there would be an option for the GridLayout which would force all the cells to be the same size but the nature of the buttons meant that they tried to resize to the text. This meant that they would become very wide. One solution would be to insert newline characters to force buttons with long text labels to wrap but this meant that the server side would have to hold them too and we would have to figure them out at one end. The successful solution to this was to get the height and width of the GridLayout and divide it by the number of rows and columns respectively, using those values to set the buttons’ dimensions. The text would now be forced into the set size of box and wrapping would happen automatically.

Another challenge came when we decided to add more questions. Originally you would just come to the last page which would say “No more questions” but we wanted to be able to dynamically add extra questions. This meant that the Wear app would need some way of being notified that there were new questions available. We solved this by starting a polling service which asks the server every six hours how many new questions are available. The API call is set up so that if the watch asks for a new question it responds not only with the question but how many more new questions are now available.

We used the Alarm Manager to achieve this polling solution but we did first consider using the Google Cloud Messaging Service (GCM).  The GCM uses push notifications to send new information to users, but this proved to be too complex and excessive for this app. If updates needed to be urgently pushed out then the GCM would be the ideal solution. The remaining issue with relying on the Alarm Manager is that if the phone has no internet connection then it can’t poll. Also, if there’s no watch connected at the time then the notification can’t be delivered. So both of these conditions have to be met for the notification to be successfully sent.

 

Download the Wear app

View the current total percentages