Following all the interest created by my Plum-O-Meter, I couldn't resist trying a similar experiment with my newly arrived Apple Pencil, so here's PencilScale, an iPad Pro application that uses the Pencil as an electronic scale.
PencilScale requires some additional special hardware that I've had to create myself using the advanced manufacturing facilities in my kitchen. The custom harness is in two parts: a base unit and a platform that sits on top of the Pencil. Luckily, Apple actually supplied the necessary raw materials in their packaging:
Both parts consist of seven segments, after folding, the vertical segments are 3cm high and the horizontal segments are 5cm square (amazingly, one the Pencil packaging sides is exactly 27cm by 10cm as if Apple had this in mind!).
After some highly technical fabrication, my harness pieces look like a work of art:
Once the hardware was complete, I moved onto the software. To jazz up the user interface, I used my old Nixie Tube project and the rest of the app is pretty simple stuff. When the touch begins, I make sure it originates from the Pencil by looking at the touch's type property...
overridefunc touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
guardlet touch = touches.firstwhere touch.type == UITouchType.Styluselse
{
return
}
zeroButton.enabled = true
update(weight: touch.force, touchLocation: touch.locationInView(view))
}
The update() method simply subtract's the touch's force from a base weight (which is set as the current touch force when the 'zero' button is pressed) and multiplies it by 140 which gives the weight in grams (very roughly):
func update(weight weight: CGFloat, touchLocation: CGPoint?)
{
self.weight = weight
self.touchLocation = touchLocation
}
var weight: CGFloat = 0
{
didSet
{
ifweight == 0
{
nixieDigitDisplay.setValue(string: "--------")
}
else
{
nixieDigitDisplay.setValue(string: String(format: "%.1f", max(0, (weight - baseWeight) * 140)))
}
}
}
...and amazingly, that is pretty much all there is to it!
Interestingly, the maximum possible force of the Pencil with the iPad Pro is 4.16666 which indicates to me that its precision is much higher than 3D Touch on the iPhone 6s, however as you can see from the video, the accuracy seems a little hit and miss, maybe with more experimentation I can get better results.
The source code, as always, is available at my GitHub repository here. Enjoy!