Barebone ESP12E programming (don’t use Adafruit EZ-Link!)

After creating my ESP12E on a stick, I plugged it into the breadboard to see if all was working.
I had been programming my Arduino UNO with my Adafruit EZ-LINK (serial over bluetooth module) successfully so I also tried to use this for the ESP. Turns out that this does not work! I kept getting strange error messages when trying to upload to the board. After loosing about 1 day trying to figure out why this did not work I figured out that it was the EZ-LINK that was the problem, it does something to the data that messes the upload cycle up!

EZ_LINK_NOGO

After replacing the Adafruit EZ-Link with a normal USB to serial module upload started working correctly.

Just remember the following hardware connections:

1. GPIO15 put it to ground
2. EN(able-) put it to VCC
3. VCC to VCC of course
4. GND to GND of course
5. TX => RX of the serial/USB convertor
6. RX -> TX of the serial/USB convertor
Before hitting upload
=> put GPIO0 to ground
=> put RST temp to ground
=> this resets the device and puts it in “upload mode”
=> you can leave GPIO0 to ground after upload, it does work, however if the device would be reset the

Barebone ESP12E programming (don’t use Adafruit EZ-Link!)

Swift multi-line UIButton with each line resized to fit width

Here is my solution for a standard UIButton that has…
(1) … multiple lines …
(2) … each line has a different preferred font size …
(3) … each line’s font size is automatically sized down to fit the width if needed

I tried playing with the UIButton.titleLabel and its various properties lineBreakMode, adjustsFontSizeToFitWidth …
but could not get the desired result. So in the end I just ended up using the standard attributedTitle of the UIButton
and calculating the given attributed text in advance (given the width) so that everything fits nicely.

I wrapped everything up in an extension and some overloaded operators to make it look nice:

Swift multi-line UIButton with each line resized to fit width

PaintCoded full size free coloured tabs

In this post I will explain how I made the following ui using the standard UITabBarController and PaintCode:
endresult

(click on the image, its an animated gif).

At first glance this looks as being normal tabbar behaviour however these things are not so “standard”:
– the tab item is in full color and not tinted
– the tab items completely cover the tab

Create the graphics using PaintCode

To explain how to make graphics in PaintCode I made the following tutorial video:

RootViewController that takes care of creating the custom tabbar item

Notes:

  • By using UIImageRenderingMode.AlwaysOriginal we avoid the image being tinted and thus the “real” colors are used
  • the size of the tabbar is fixed as  50 points (defined by Apple)
  • I also found out that the image needs to be shifted down by 6 points to cover the tab completely

StarViewController implementation

Note that we are using a “tabBarItemCreated” flag to make sure we only create the tabitem once. If you do not implement this behaviour the initial (selected) state of the tabs is erratic.

I also found out that in the getter when the tabBarItem is not yet created and the flag is false, I had to first set this flag to true and only then do the super.tabBarItem = makeTabItem(….); apparently the super.tabBarItem = … calls the getter and then this would create an endless loop!

SquareViewController implementation

This is basically a copy of the StarViewController but only the title and the imageCreator is different!

App delegate to for setting the styling of the tabbar
As final touch we need to set the correct colours for the labels on the tab.
We do this via the appearance proxy for the UITabBarItem.

PaintCoded full size free coloured tabs

Adding a custom back button “the standard way”

My idea was to implement a Root ViewController that would restyle the standard back-button.
Turned out this was not easy at all. After about 1 day of fiddling around with different options
I finally came to a working solution.

The trick is to have a transparant background that is BIG enough (bigger then the actual back chevron otherwise this gets resized to smaller size) and then use a custom image for the chevron.

It works, but as you can see on the measurements below, it has a small disadvantage: there is an extra 14 points of gap on the left. If you want matching buttons on the right, you will need to compensate this by 14 points (and yes its points so on retina this is 28 pixels…)

custombackbuttonstoryboard

custombackbuttonmeasurements

Adding a custom back button “the standard way”

xcode script for automatically updating the build number

This is the build script I use for automatically updating the building number in xcode.
Note that the build number is updated each time you compile/build.

The script is installed by adding a “Run script” in the “Build Phases”:

buildscript

xcode script for automatically updating the build number

Generating random strings (for testing)

While converting some of my objective-c application towards swift, I also needed to port the generation of randome strings to swift. I use random strings during development to debug length of labels, buttons… to see if the UI scales nicely. In effect I use it to generate dummy data in my model.

After doing some research, I came up with the following 3 different functions:

Usage:

Generating random strings (for testing)

How I use notifications in swift

Today while I was converting one of my easier projects from objective-c to swift I researched the best way of using  (custom)notifications in swift. Below is the method I selected.

The class that is sending the notifications

Embedding notification strings

I like to embed the literal strings for the notifications inside the class that is sending them. Since swift does not (yet) support static variables in classes itself I selected the method where I embed a struct “notify” inside the class that contains the static and constant (let) strings. Yes, structs can have static variables classed not… a bit weird maybe…

Sending the notification

Sending the notification boils down to calling the postNotificationName… function and retrieving the correct name from the “notif” struct

The class that is listening for notifications

Hook the listeners

I implement a function “hookListeners” that groups all the addObserver statements.
In the above code, the class that want to listen to notifications is derived from UIViewController so I call this function in the viewWillAppear. I am sure that there are other points that are possible as well (viewDidLoad, viewDidAppear) but this works for me.

Notice the elegant way I can access the name for the notification!

The selector in the addObserver function  is specified as a literal string. This means that the compiler does not do any checking whether its a valid selector (compared to objective-c this is a step backwards in my opinion). Make sure that the selector does not point to a private function if so this will throw an “unrecognised selector send to…” runtime crash

Unhook te listeners

Same principle here, one function “unhookListeners” and called in viewWillDisappear.

How I use notifications in swift

Function call order for custom UIButton with IBDesignable, IBInspectable

Starting from xcode 6 apple introduced the possibility to render your custom UIViews straight in IB. I personally love this feature because finally the interface designed in IB approaches visually what it will look like for real. Previously you had a bunch of custom UIViews in IB that ware just well… white rectangles….

This article gives some info about a custom button “HixRoundedButton” I made for one of my projects, more particular the sequence and order of the functions called in IB when rendering to screen in IB.

HixRoundedButton has the following functions:

  • Displays a rounded gradient filled rectangle as background
  • You can set an option that automatically sizes the title of the button to the max size that still fits in the button’s view

The below IB scene contains 3 such buttons (green, red and grey ones) :1

By using IBInspectable the following properties are configurable in IB:

2

Below you can find the code, here are some remarks about it:

  • An UIButton contains a standard UILabel “titleLabel” that holds the title. As can be seen in the apple doc, this can be accessed to configure the appearance…
  • One time configuration of this UILabel is done when added as a sub view via “didAddSubview”.
  • The title is set before the UILabel is actually on screen, this is why the check on valid window is there. Otherwise the font size would be calculated as zero and nothing is displayed. To make sure that even at first start the styling is correct, styling is also done in the “drawRect” function.
  • The “layoutSubviews” is used to make the frame of the UILabel (holding the button title) the same as the superview. Without this for some reason the frame of the UILabel would behave erratically. I believe this has something to do with the insets property of the UIButton…

Running the application in the simulator produced this:

Rendering in IB (traced this using breakpoints and debug view option)

Function call order for custom UIButton with IBDesignable, IBInspectable