Skip to content

Commit d54ed3d

Browse files
committed
Test and update C tutorial part 1
Signed-off-by: C-D-Lewis <[email protected]>
1 parent 38a17bd commit d54ed3d

File tree

2 files changed

+121
-23
lines changed

2 files changed

+121
-23
lines changed

source/tutorials/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<div class="vcenter">
3232
<h3>Build a Watchface</h3>
3333
<p>Learn how to create your first watchface. This tutorial will cover basic Pebble concepts, and is the recommended starting point for new developers.</p>
34-
<a href="/tutorials/js-watchface-tutorial/" target="_blank" class="btn btn--fg-green btn--bg-white">Build with JS</a>
35-
<a href="/tutorials/watchface-tutorial/" target="_blank" class="btn btn--fg-green btn--bg-white">Build with C</a>
34+
<a href="/tutorials/watchface-tutorial/" class="btn btn--fg-green btn--bg-white">Build with C</a>
35+
<a href="/tutorials/js-watchface-tutorial/" class="btn btn--fg-green btn--bg-white">Build with JS</a>
3636
</div>
3737
</div>
3838
</div>
@@ -48,7 +48,7 @@ <h3>Build a Watchface</h3>
4848
<div class="vcenter">
4949
<h3>Build a One Click Action</h3>
5050
<p>Learn how to create your first one click action watchapp. This guide explains how to create a watchapp that will makes a web request upon launch and display the result.</p>
51-
<a href="/guides/design-and-interaction/one-click-actions/" target="_blank" class="btn btn--fg-lightblue btn--bg-white">Build with C</a>
51+
<a href="/guides/design-and-interaction/one-click-actions/" class="btn btn--fg-lightblue btn--bg-white">Build with C</a>
5252
</div>
5353
</div>
5454
</div>
@@ -76,7 +76,7 @@ <h3>Learn C with Pebble</h3>
7676
<div class="vcenter">
7777
<h3>Publish Your App</h3>
7878
<p>Learn how to publish your watchface or watchapp on Pebble's appstore.</p>
79-
<a href="/guides/appstore-publishing/publishing-an-app" target="_blank" class="btn btn--fg-lightblue btn--bg-dawk">Publish an App</a>
79+
<a href="/guides/appstore-publishing/publishing-an-app" class="btn btn--fg-lightblue btn--bg-dawk">Publish an App</a>
8080
</div>
8181
</div>
8282
</div>

source/tutorials/watchface-tutorial/part1.md

Lines changed: 117 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,22 @@ this:
7070

7171
The main difference between the two kinds are that watchfaces serve as the
7272
default display on the watch, with the Up and Down buttons allowing use of the
73-
Pebble timeline. This means that these buttons are not available for custom
74-
behavior (Back and Select are also not available to watchfaces). In contrast,
75-
watchapps are launched from the Pebble system menu. These have more capabilities
76-
such as button clicks and menu elements, but we will come to those later.
73+
Pebble timeline and Pebble Health (though these are customisable). This means
74+
that these buttons are not available for custom behavior (Back and Select are
75+
also not available to watchfaces). In contrast, watchapps are launched from the
76+
Pebble system menu. These can have more capabilities such as button clicks and
77+
menu elements, but we will come to those later.
7778

78-
Finally, set a value for `companyName` and we can start to write some code!
79+
Now we can start to write some code!
7980

8081

8182
## Watchface Basics
8283

8384
Our first source file is already created for you by the `pebble` command
8485
line tool and lives in the project's `src` directory. By default, this file
85-
contains sample code which you can safely remove, since we will be starting from
86-
scratch. Alternatively, you can avoid this by using the `--simple` flag when
87-
creating the project.
86+
contains sample code which you can safely remove completely, since we will be
87+
starting from scratch. Alternatively, you can avoid this by using the `--simple`
88+
flag when creating the project.
8889

8990
Let's add the basic code segments which are required by every watchapp. The
9091
first of these is the main directive to use the Pebble SDK at the top of the
@@ -94,7 +95,7 @@ file like so:
9495
#include <pebble.h>
9596
```
9697

97-
After this first line, we must begin with the recommended app structure,
98+
After this first line, we should begin with the recommended app structure,
9899
specifically a standard C `main()` function and two other functions to help us
99100
organize the creation and destruction of all the Pebble SDK elements. This helps
100101
make the task of managing memory allocation and deallocation as simple as
@@ -191,8 +192,7 @@ valid after each iterative change, so let's do this now.
191192
## First Compilation and Installation
192193

193194
To compile the watchface, make sure you have saved your project files and
194-
then run `pebble build` from the project's root directory. The installable
195-
`.pbw` file will be deposited in the `build` directory. After a successful
195+
then run `pebble build` from the project's root directory. After a successful
196196
compile you will see a message reading `'build' finished successfully`. If there
197197
are any problems with your code, the compiler will tell you which lines are in
198198
error so you can fix them.
@@ -202,12 +202,25 @@ In order to install your watchface on your Pebble, first
202202
Make sure you are using the latest version of the Pebble app.
203203

204204
Install the watchapp by running `pebble install`, supplying your phone's IP
205-
address with the `--phone` flag. For example: `pebble install
206-
--phone 192.168.1.78`.
205+
address with the `--phone` flag. For example:
206+
207+
```
208+
pebble install --phone 192.168.1.78
209+
```
207210

208211
> Instead of using the --phone flag every time you install, set the PEBBLE_PHONE environment variable:
209212
> `export PEBBLE_PHONE=192.168.1.78` and simply use `pebble install`.
210213
214+
You can also use the emulator by specifying the platform you want to use, for
215+
example the Pebble Time platform `basalt`:
216+
217+
```
218+
pebble install --emulator basalt
219+
```
220+
221+
> Use of the emulator may require more dependencies to be installed depending
222+
> on your system and configuration.
223+
211224
Congratulations! You should see that you have a new item in the watchface menu,
212225
but it is entirely blank!
213226

@@ -292,8 +305,7 @@ static void main_window_unload(Window *window) {
292305
```
293306

294307
This completes the setup of the basic watchface layout. If you run `pebble
295-
build && pebble install` (with your phone's IP address) for the new build, you
296-
should now see the following:
308+
build && pebble install` for the new build, you should now see the following:
297309

298310
{% screenshot_viewer %}
299311
{
@@ -339,8 +351,9 @@ tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
339351
```
340352

341353
The logic to update the time ``TextLayer`` will be created in a function called
342-
`update_time()`, enabling us to call it both from the ``TickHandler`` as well as
343-
`main_window_load()` to ensure it is showing a time from the very beginning.
354+
`update_time()`, using a newly created time object, enabling us to call it both
355+
from the ``TickHandler`` as well as `main_window_load()` to ensure it is showing
356+
a time from the very beginning.
344357

345358
This function will use `strftime()`
346359
([See here for formatting](http://www.cplusplus.com/reference/ctime/strftime/))
@@ -417,9 +430,94 @@ watchface! To do this we:
417430
these to a buffer for display in the ``TextLayer``.
418431

419432
If you have problems with your code, check it against the sample source code
420-
provided using the button below.
433+
provided below.
434+
435+
<details>
436+
<summary>View source code</summary>
437+
{% markdown %}
438+
```c
439+
#include <pebble.h>
440+
441+
static Window *s_main_window;
442+
static TextLayer *s_time_layer;
443+
444+
static void update_time() {
445+
// Get a tm structure
446+
time_t temp = time(NULL);
447+
struct tm *tick_time = localtime(&temp);
448+
449+
// Write the current hours and minutes into a buffer
450+
static char s_buffer[8];
451+
strftime(s_buffer, sizeof(s_buffer), clock_is_24h_style() ?
452+
"%H:%M" : "%I:%M", tick_time);
453+
454+
// Display this time on the TextLayer
455+
text_layer_set_text(s_time_layer, s_buffer);
456+
}
457+
458+
static void main_window_load(Window *window) {
459+
// Get information about the Window
460+
Layer *window_layer = window_get_root_layer(window);
461+
GRect bounds = layer_get_bounds(window_layer);
462+
463+
// Create the TextLayer with specific bounds
464+
s_time_layer = text_layer_create(
465+
GRect(0, PBL_IF_ROUND_ELSE(58, 52), bounds.size.w, 50));
466+
467+
// Improve the layout to be more like a watchface
468+
text_layer_set_background_color(s_time_layer, GColorClear);
469+
text_layer_set_text_color(s_time_layer, GColorBlack);
470+
text_layer_set_text(s_time_layer, "00:00");
471+
text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
472+
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
473+
474+
// Add it as a child layer to the Window's root layer
475+
layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
476+
}
477+
478+
static void main_window_unload(Window *window) {
479+
// Destroy TextLayer
480+
text_layer_destroy(s_time_layer);
481+
}
482+
483+
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
484+
update_time();
485+
}
421486

422-
[View Source Code >{center,bg-lightblue,fg-white}](https://gist.github.com/9b9d50b990d742a3ae34)
487+
488+
static void init() {
489+
// Create main Window element and assign to pointer
490+
s_main_window = window_create();
491+
492+
// Set handlers to manage the elements inside the Window
493+
window_set_window_handlers(s_main_window, (WindowHandlers) {
494+
.load = main_window_load,
495+
.unload = main_window_unload
496+
});
497+
498+
// Show the Window on the watch, with animated=true
499+
window_stack_push(s_main_window, true);
500+
501+
// Register with TickTimerService
502+
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
503+
504+
// Make sure the time is displayed from the start
505+
update_time();
506+
}
507+
508+
static void deinit() {
509+
// Destroy Window
510+
window_destroy(s_main_window);
511+
}
512+
513+
int main(void) {
514+
init();
515+
app_event_loop();
516+
deinit();
517+
}
518+
```
519+
{% endmarkdown %}
520+
</details>
423521
424522
## What's Next?
425523

0 commit comments

Comments
 (0)