You can find the video recording here: https://www.youtube.com/watch?v=8tOhdUXcSkw
Heiko Behrens and Matthew Hungerford talk about advanced programming techniques for Pebble. This talk focused on size to optimize pebble apps for code size, heap space, and advice on use of floating point.
The Mandelbrot demo and XKCD app were featured.
Day 1 - Video 3A
1. SIZE
MATT & HEIKO â DEVELOPER EXPERIENCE ENGINEERS
ON TWITTER AS @MTHUNGERFORD & @HBEHRENS
2. AVAILABLE MEMORY WHEN DEVELOPING FOR PEBBLE
> pebble build
app memory usage:
=============
Total footprint in RAM: 9596 bytes / ~24kb
Free RAM available (heap): 14980 bytes
3. AVAILABLE MEMORY WHEN DEVELOPING FOR PEBBLE
app memory usage:
=============
Total footprint in RAM: 9596 bytes / ~24kb
Free RAM available (heap): 14980 bytes
RAM
Code
Static Variables
Heap
4. MEMORY â WHERE GOES WHAT?
static TextLayer *text_layer;
!
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
!
text_layer = text_layer_create(âŠ);
text_layer_set_text(text_layer, "Press a button");
text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(text_layer));
}
RAM
Code
Static Variables A
Heap
B
C
Stack
C
C
A
B
A
5. AVAILABLE MEMORY WHEN DEVELOPING FOR PEBBLE
Stack
Code
Static Variables
Heap
function calls
local variables
arrays of dynamic size {
RAM
7. processor
64MHz
ARM Cortex-M3 core
512 kilobytes flash
128 kilobytes RAM
display connector
RAM
serial flash
4 megabytes bluetooth radio
(hidden on back)
Bluetooth 2 + BLE
accelerometer
ambient light
sensor
magnetometer
battery
vibrating motor
8. RAM
Data
Stacks
Heap Worker
Kernel
Stack
Code + Heap
App
App State
Stack
Code + Heap
84k
12k
2k 32k
24k
128K RAM â WHY ONLY 24K FOR MY APP?
20. CASE STUDY: MANDELBROT FRACTALS
Implements recursive fractal algorithm
provided at
rosettacode.org/wiki/Mandelbrot_set
https://github.com/mhungerford/pebble-mandelbrot-generator
21. FLOATING POINT MATH
Not supported in hardware on some ARM processors,
including the Cortex-M3 used by Pebble
!
All floating point operations (*, /, +, -, sin, cos, tan)
must be emulated in software
source: http://en.wikipedia.org/wiki/IEEE_754-1985
22. FIXED POINT MATH
In fixed point, we position the decimal point at a fixed position, and
everything above it is an integer, and below it a decimal value
(fraction).
!
We lose half our range if we do a 50/50 fixed point.
source: http://wiki.nycresistor.com/wiki/GB101:Fixed_point_math
23. SLL-MATH (FIXED-POINT)
Using fixed-point sll-math (64-bit signed long long), we retain large
range, as well as use some ARM assembly to provide high
performance.
https://code.google.com/p/al-lib3d/source/browse/trunk/math-sll.h
25. FLOATING POINT VS FIXED POINT
Floating Point
(softfloat)
http://github.com/mhungerford/pebble-mandelbrot-generator
Fixed Point
(sll-math)
Code Size 10k 2.7k
Seconds Per Frame
(not FPS) 20 S 2 S
26. MATH WRAPPER
Typically when using fixed-point (in this case SLL-Math),
operators can't be used, as C doesn't support operator overloading.
Floating Point Fixed Point
float rotation = 15.5f;
val = rotation * 0.75f;
sll rotation = float2sll(15.5f);
val = sllmul(rotation, float2sll(0.75f));
27. MATH WRAPPER
For softfloat to work, the compiler uses calls to the external softfloat
library with names such as __aeabi__fmul(float, float)
!
By using doubles as a container and providing our own versions of
__aeabi_dmul, we can essentially make double = sll type and do
direct operations, as we are âoverloadingâ the functions in a manner.
Math wrapper
double rotation = dbl(15.5f);
val = rotation * dbl(0.75f);
28. MATH WRAPPER
Fractal Tree demo uses
*, /, +, -, sin, cos to randomly
generate different trees every frame.
Floating Point
(softfloat)
Fixed Point
(sll-wrap)
Code Size 9k 2.7k
FPS 11 18
https://github.com/mhungerford/pebble-tinymath
33. ALLOCATING HEAP MEMORYâŠ
Always check the results of
!
malloc(size_t size) and *_create(âŠ)
Measure available heap space
!
heap_bytes_free(void) and heap_bytes_used(void)
SDK functions may require additional heap space
!
gpath_draw_filled(GContext* ctx, GPath *path)
37. DYNAMIC STACK ALLOCATION
typedef struct {
GPoint inner;
GPoint mid;
GPoint outer;
} CompassCalibrationWindowHelperPoint;
CompassCalibrationWindowHelperPoint points[num_segments];
for (int s = 0; s < num_segments; s++) {
...
Dynamic stack allocation is a fast and convenient approach to access
up to 2k of additional memory.
compass source: https://github.com/pebble-hacks/pebble-compass
44. PNG SUPPORT
http://github.com/mhungerford/png_demo
uPNG port (single API call)
GBitmap*
gbitmap_create_with_png_resource(
uint32_t resource_id);
!
matches standard SDK call for GBitmap
GBitmap*
gbitmap_create_with_resource(
uint32_t resource_id);
47. EXTERNALIZING YOUR DATA
Persistent Storage App Messages
Canvas for Pebble Pebble Mars
good source for encoding/loading images: https://github.com/pebble-hacks/pebble-faces
48. alternative to heap for (dynamic) short-term data
measure and optimize
always check return values
can be abused
can be compressed
âbackendâ to the watch
Phone Flash RAM
Stack
Code
Static Variables
Heap
Persistent Storage
Resources
JavaScript
or
Native Code