Skip to content

Commit 5065017

Browse files
ujur007sjanc
authored andcommitted
Add Blues example app
1 parent 286c630 commit 5065017

File tree

4 files changed

+339
-0
lines changed

4 files changed

+339
-0
lines changed

apps/blues-wireless/pkg.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: apps/blues-wireless
21+
pkg.type: app
22+
pkg.description: Basic example application for Blues Wireless notecard.
23+
pkg.author: "Apache Mynewt <[email protected]>"
24+
pkg.homepage: "http://mynewt.apache.org/"
25+
pkg.keywords:
26+
27+
pkg.deps:
28+
- "@apache-mynewt-core/kernel/os"
29+
- "@apache-mynewt-core/hw/hal"
30+
- "@apache-mynewt-core/sys/console/full"
31+
- "@apache-mynewt-core/sys/log/stub"
32+
- "@apache-mynewt-core/sys/shell"
33+
- "@apache-mynewt-core/sys/sysinit"
34+
- "@apache-mynewt-core/net/cellular/blues"
35+
36+

apps/blues-wireless/src/main.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
21+
#include <assert.h>
22+
#include <string.h>
23+
24+
#include "sysinit/sysinit.h"
25+
#include "os/os.h"
26+
#include "bsp/bsp.h"
27+
#include "hal/hal_gpio.h"
28+
#include "hal/hal_i2c.h"
29+
#include "hal/hal_uart.h"
30+
#include <console/console.h>
31+
32+
#include <note.h>
33+
#include "note_c_hooks.h"
34+
35+
/*Product UUID from notehub*/
36+
37+
#define PRODUCT_UID "UID"
38+
39+
float lat, lon;
40+
41+
/* For LED toggling */
42+
int g_led_pin;
43+
44+
static void
45+
init_notecard(void)
46+
{
47+
NoteSetFnDefault(malloc, free, platform_delay, platform_millis);
48+
NoteSetFnDebugOutput(note_log_print);
49+
NoteSetFnI2C(NOTE_I2C_ADDR_DEFAULT, NOTE_I2C_MAX_DEFAULT, note_i2c_reset, note_i2c_transmit, note_i2c_receive);
50+
51+
J *req = NoteNewRequest("hub.set");
52+
JAddStringToObject(req, "product", PRODUCT_UID);
53+
JAddStringToObject(req, "mode", "periodic");
54+
JAddBoolToObject(req, "sync", true);
55+
56+
NoteRequestWithRetry(req, 5);
57+
58+
J *req2 = NoteNewRequest("card.version");
59+
60+
NoteRequestWithRetry(req2, 5);
61+
62+
J *req3 = NoteNewRequest("card.location.mode");
63+
JAddStringToObject(req3, "mode","continuous");
64+
65+
NoteRequestWithRetry(req3, 5);
66+
}
67+
68+
static void
69+
update_location(void)
70+
{
71+
J *location = NoteRequestResponse(NoteNewRequest("card.location"));
72+
73+
if (location != NULL) {
74+
lat = JGetNumber(location, "lat");
75+
lon = JGetNumber(location, "lon");
76+
}
77+
J *req = NoteNewRequest("note.add");
78+
JAddStringToObject(req, "file", "location.qo");
79+
JAddBoolToObject(req, "sync", true);
80+
81+
J *body = JCreateObject();
82+
JAddStringToObject(body, "message", "location");
83+
84+
if (location == NULL) {
85+
J *timereq = NoteRequestResponse(NoteNewRequest("card.time"));
86+
if (timereq != NULL) {
87+
lat = JGetNumber(timereq, "lat");
88+
lon = JGetNumber(timereq, "lat");
89+
}
90+
}
91+
JAddNumberToObject(body, "Latitude", lat);
92+
JAddNumberToObject(body, "Longitude", lon);
93+
JAddItemToObject(req, "body", body);
94+
NoteRequest(req);
95+
}
96+
97+
/**
98+
* main
99+
*
100+
* The main task for the project. This function initializes packages,
101+
* and then blinks the BSP LED in a loop.
102+
*
103+
* @return int NOTE: this function should never return!
104+
*/
105+
int
106+
mynewt_main(int argc, char **argv)
107+
{
108+
int rc;
109+
sysinit();
110+
111+
g_led_pin = LED_BLINK_PIN;
112+
hal_gpio_init_out(g_led_pin, 1);
113+
init_notecard();
114+
115+
while (1) {
116+
/* Wait one second */
117+
os_time_delay(OS_TICKS_PER_SEC * 10);
118+
119+
/* Toggle the LED */
120+
hal_gpio_toggle(g_led_pin);
121+
update_location();
122+
}
123+
assert(0);
124+
return rc;
125+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
#
20+
*/
21+
22+
23+
#include "note_c_hooks.h"
24+
#include "bsp/bsp.h"
25+
#include "hal/hal_i2c.h"
26+
#include "os/os_time.h"
27+
#include <console/console.h>
28+
29+
static const size_t REQUEST_HEADER_SIZE = 2;
30+
31+
const uint8_t i2c_num = 0;
32+
bool i2c_initialized = false;
33+
34+
uint32_t
35+
platform_millis(void)
36+
{
37+
return (uint32_t)(os_get_uptime_usec()/1000);
38+
}
39+
40+
void
41+
platform_delay(uint32_t ms)
42+
{
43+
os_time_t d_time;
44+
d_time = os_time_ms_to_ticks32(ms);
45+
os_time_delay(d_time);
46+
}
47+
48+
const char *
49+
note_i2c_receive(uint16_t device_address, uint8_t *buffer,
50+
uint16_t size, uint32_t *available)
51+
{
52+
uint8_t size_buf[2];
53+
size_buf[0] = 0;
54+
size_buf[1] = (uint8_t)size;
55+
56+
struct hal_i2c_master_data data = {
57+
.address = device_address,
58+
.len = sizeof(size_buf),
59+
.buffer = size_buf,
60+
};
61+
62+
uint8_t write_result = hal_i2c_master_write(i2c_num, &data, OS_TICKS_PER_SEC / 10, 0);
63+
64+
if (write_result != 0) {
65+
return "i2c: unable to initiate read from the notecard\n";
66+
}
67+
/*
68+
Read from the Notecard and copy the response bytes into the
69+
response buffer
70+
*/
71+
const int request_length = size + REQUEST_HEADER_SIZE;
72+
uint8_t read_buf[256];
73+
74+
data.len = request_length;
75+
data.buffer = read_buf;
76+
int8_t read_result = hal_i2c_master_read(i2c_num, &data, OS_TICKS_PER_SEC / 10, 1);
77+
78+
if (read_result != 0) {
79+
return "i2c: Unable to receive data from the Notecard.\n";
80+
} else {
81+
*available = (uint32_t)read_buf[0];
82+
uint8_t bytes_to_read = read_buf[1];
83+
for (size_t i = 0; i < bytes_to_read; i++) {
84+
buffer[i] = read_buf[i + 2];
85+
}
86+
return NULL;
87+
}
88+
}
89+
90+
bool
91+
note_i2c_reset(uint16_t device_address)
92+
{
93+
(void)device_address;
94+
95+
if (i2c_initialized) {
96+
return true;
97+
}
98+
if (hal_i2c_enable(i2c_num) != 0) {
99+
console_printf("i2c: Device not ready.\n");
100+
return false;
101+
}
102+
console_printf("i2c: Device is ready.\n");
103+
i2c_initialized = true;
104+
return true;
105+
}
106+
107+
const char *
108+
note_i2c_transmit(uint16_t device_address, uint8_t *buffer,
109+
uint16_t size)
110+
{
111+
uint8_t write_buf[size + 1];
112+
write_buf[0] = (uint8_t)size;
113+
for (size_t i = 0; i < size; i++) {
114+
write_buf[i + 1] = buffer[i];
115+
}
116+
117+
struct hal_i2c_master_data data = {
118+
.address = device_address,
119+
.len = size + 1,
120+
.buffer = write_buf,
121+
};
122+
123+
uint8_t write_result = hal_i2c_master_write(i2c_num, &data, OS_TICKS_PER_SEC / 5, 1);
124+
125+
if (write_result != 0) {
126+
return "i2c: Unable to transmit data to the Notecard\n";
127+
} else {
128+
return NULL;
129+
}
130+
}
131+
132+
size_t
133+
note_log_print(const char *message)
134+
{
135+
if (message) {
136+
console_printf("%s", message);
137+
return 1;
138+
}
139+
return 0;
140+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
*/
20+
21+
#ifndef NOTE_C_HOOKS_H
22+
#define NOTE_C_HOOKS_H
23+
24+
#include <stdbool.h>
25+
#include <stdint.h>
26+
#include <stdlib.h>
27+
28+
void platform_delay(uint32_t ms);
29+
uint32_t platform_millis(void);
30+
31+
const char *note_i2c_receive(uint16_t device_address, uint8_t *buffer,
32+
uint16_t size, uint32_t *available);
33+
bool note_i2c_reset(uint16_t device_address);
34+
const char *note_i2c_transmit(uint16_t device_address, uint8_t *buffer, uint16_t size);
35+
36+
size_t note_log_print(const char *message);
37+
38+
#endif /* NOTE_C_HOOKS_H */

0 commit comments

Comments
 (0)