19
19
* Note: Currently only iOS act as a CTS server, Android does not. The
20
20
* easiest way to test this sketch is using an iOS device.
21
21
*
22
+ * This sketch is similar to client_cts but also uses a Feather OLED Wing
23
+ * to display time https://www.adafruit.com/product/2900
24
+ *
22
25
* Current Time Service info:
23
26
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml
24
27
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml
25
28
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml
26
29
*/
27
30
28
31
#include < bluefruit.h>
32
+ #include < Adafruit_GFX.h>
33
+ #include < Adafruit_SSD1306.h>
34
+
35
+ #define OLED_RESET 4
36
+ Adafruit_SSD1306 oled (OLED_RESET);
29
37
30
38
// BLE Client Current Time Service
31
39
BLEClientCts bleCTime;
32
40
33
41
void setup ()
34
42
{
35
- Serial. begin ( 115200 );
36
- Serial. println ( " Bluefruit52 BLE Client Current Time Example " );
37
- Serial. println ( " ------------------------------------------- \n " );
43
+ // init with the I2C addr 0x3C (for the 128x32) and show splashscreen
44
+ oled. begin (SSD1306_SWITCHCAPVCC, 0x3C );
45
+ oled. display ( );
38
46
39
- Serial. println ( " Go to iOS's Bluetooth settings and connect to Bluefruit52 " );
40
- Serial. println ( " It may appear up as 'Accessory' depending on your iOS version. " );
47
+ oled. setTextSize ( 1 ); // max is 4 line, 21 chars each
48
+ oled. setTextColor (WHITE );
41
49
42
50
Bluefruit.begin ();
43
51
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
@@ -49,13 +57,16 @@ void setup()
49
57
// Configure CTS client
50
58
bleCTime.begin ();
51
59
52
- // Callback invoked when iOS device time changes
53
- // To test this go to Setting -> Date & Time -> Toggle Time Zone "Set Automatically"
54
- // Or change the time manually etc ...
55
- bleCTime.setAdjustCallback (cts_adjust_callback);
56
-
57
60
// Set up and start advertising
58
61
startAdv ();
62
+
63
+ // splash screen effect
64
+ delay (100 );
65
+
66
+ oled.clearDisplay ();
67
+ oled.setCursor (0 , 0 );
68
+ oled.println (" Waiting to connect" );
69
+ oled.display ();
59
70
}
60
71
61
72
void startAdv (void )
@@ -88,17 +99,15 @@ void startAdv(void)
88
99
89
100
void loop ()
90
101
{
91
- // Not connected, wait for a connection
92
- if ( !Bluefruit.connected () ) return ;
93
-
94
102
// If service is not yet discovered
95
- if ( !bleCTime.discovered () ) return ;
103
+ if ( !bleCTime.discovered () && !Bluefruit. connPaired () ) return ;
96
104
97
105
// Get Time from iOS once per second
98
- // Note it is advised to update this quickly
106
+ // Note it is not advised to update this quickly
99
107
// Application should use local clock and update time after
100
- // a long period (e.g an hour or day)s
108
+ // a long period (e.g an hour or day)
101
109
bleCTime.getCurrentTime ();
110
+ bleCTime.getLocalTimeInfo ();
102
111
103
112
printTime ();
104
113
@@ -107,61 +116,68 @@ void loop()
107
116
108
117
void connect_callback (uint16_t conn_handle)
109
118
{
110
- Serial.println (" Connected" );
111
-
112
- Serial.print (" Discovering CTS ... " );
119
+ oled.clearDisplay ();
120
+ oled.setCursor (0 , 0 );
121
+ oled.println (" Connected." );
122
+ oled.print (" Discovering ... " );
123
+ oled.display ();
124
+
113
125
if ( bleCTime.discover (conn_handle) )
114
126
{
115
- Serial.println (" Discovered" );
127
+ oled.println (" OK" );
128
+
129
+ // ANCS requires pairing to work
130
+ oled.print (" Paring ... " );
116
131
117
- // iOS requires pairing to work, it makes sense to request security here as well
118
- Serial. print ( " Attempting to PAIR with the iOS device, please press PAIR on your phone ... " );
132
+ oled. display ();
133
+
119
134
if ( Bluefruit.requestPairing () )
120
135
{
121
- Serial .println (" Done " );
122
- Serial. println ( " Enabling Time Adjust Notify " );
136
+ oled .println (" OK " );
137
+
123
138
bleCTime.enableAdjust ();
124
139
125
- Serial. print ( " Get Current Time chars value " );
140
+ oled. println ( " Receiving Time... " );
126
141
bleCTime.getCurrentTime ();
127
-
128
- Serial.print (" Get Local Time Info chars value" );
129
142
bleCTime.getLocalTimeInfo ();
130
-
131
- Serial.println ();
132
143
}
133
-
134
- Serial.println ();
135
144
}
136
- }
137
-
138
- void cts_adjust_callback (uint8_t reason)
139
- {
140
- const char * reason_str[] = { " Manual" , " External Reference" , " Change of Time Zone" , " Change of DST" };
141
145
142
- Serial.println (" iOS Device time changed due to " );
143
- Serial.println ( reason_str[reason] );
146
+ oled.display ();
144
147
}
145
148
146
149
void printTime (void )
147
150
{
148
151
const char * day_of_week_str[] = { " Mon" , " Tue" , " Wed" , " Thu" , " Fri" , " Sat" , " Sun" };
152
+ const char * month_str[] = { " na" , " Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" , " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" };
149
153
150
- Serial.printf (" %04d-%02d-%02d " , bleCTime.Time .year , bleCTime.Time .month , bleCTime.Time .day );
151
- Serial.printf (" %02d:%02d:%02d " , bleCTime.Time .hour , bleCTime.Time .minute , bleCTime.Time .second );
152
- Serial.print (day_of_week_str[bleCTime.Time .weekday ]);
154
+ oled.clearDisplay ();
155
+ oled.setCursor (0 , 0 );
156
+
157
+ oled.print (day_of_week_str[bleCTime.Time .weekday ]);
158
+ oled.printf (" %s %02d %04d\n " , month_str[bleCTime.Time .month ], bleCTime.Time .day , bleCTime.Time .year );
153
159
160
+
161
+ oled.setTextSize (2 );
162
+ oled.printf (" %02d:%02d:%02d\n " , bleCTime.Time .hour , bleCTime.Time .minute , bleCTime.Time .second );
163
+
154
164
int utc_offset = bleCTime.LocalInfo .timezone *15 ; // in 15 minutes unit
155
- Serial.printf (" (UTC %+d:%02d, " , utc_offset/60 , utc_offset%60 );
156
- Serial.printf (" DST %+.1f)" , ((float ) bleCTime.LocalInfo .dst_offset *15 )/60 );
157
- Serial.println ();
165
+
166
+ oled.setTextSize (1 );
167
+ oled.printf (" UTC %+d:%02d, " , utc_offset/60 , utc_offset%60 );
168
+ oled.printf (" DST %+.1f" , ((float ) bleCTime.LocalInfo .dst_offset *15 )/60 );
169
+ oled.println ();
170
+
171
+ oled.display ();
158
172
}
159
173
160
174
161
175
void disconnect_callback (uint16_t conn_handle, uint8_t reason)
162
176
{
163
177
(void ) reason;
164
178
165
- Serial.println ();
166
- Serial.println (" Disconnected" );
179
+ oled.clearDisplay ();
180
+ oled.setCursor (0 , 0 );
181
+ oled.println (" Waiting to connect" );
182
+ oled.display ();
167
183
}
0 commit comments