-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
127 lines (107 loc) · 3.94 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "app_error.h"
#include "app_timer.h"
#include "fds.h"
#include "fstorage.h"
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_delay.h"
#include "nrf_sdm.h"
#include "nrf_soc.h"
#include "nrf_gpio.h"
#include "nrf_log.h"
#include "nrf51_bitfields.h"
#include "softdevice_handler.h"
// application
#include "friend_data_storage.h"
#include "SEGGER_RTT.h"
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define SCHED_MAX_EVENT_DATA_SIZE ((CEIL_DIV(MAX( \
MAX(BLE_STACK_EVT_MSG_BUF_SIZE, \
ANT_STACK_EVT_STRUCT_SIZE), \
SYS_EVT_MSG_BUF_SIZE \
), \
sizeof(uint32_t))) * \
sizeof(uint32_t))
#define SCHED_QUEUE_SIZE 16u
static friends_list_t friends_list;
static void sys_evt_dispatch(uint32_t sys_evt)
{
fs_sys_event_handler(sys_evt);
}
static void ble_stack_init(void)
{
uint32_t err_code;
// Initialize the SoftDevice handler module.
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_1000MS_CALIBRATION, NULL);
#if defined(S110) || defined(S130) || defined(S310) || defined(S132)
// Enable BLE stack.
ble_enable_params_t ble_enable_params;
memset(&ble_enable_params, 0, sizeof(ble_enable_params));
#if defined(S130) || defined(S310) || defined(S132)
ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
#endif
ble_enable_params.gatts_enable_params.service_changed = 0;
err_code = sd_ble_enable(&ble_enable_params);
SEGGER_RTT_printf(0, "ble_stack_enable error code is: %u\n", err_code);
APP_ERROR_CHECK(err_code);
#endif
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
SEGGER_RTT_printf(0, "sys_evt_dispatch error code is: %u\n", err_code);
APP_ERROR_CHECK(err_code);
}
static void ble_address_to_string_convert(ble_gap_addr_t address, uint8_t * string_buffer)
{
const int address_length = 6;
char temp_str[3];
int i = 0;
for (i = address_length - 1; i >= 0; --i)
{
sprintf(temp_str, "%02X", address.addr[i]);
strcat((char *)string_buffer, temp_str);
}
}
static void print_friends() {
SEGGER_RTT_WriteString(0, "------------- PRINTING FRIENDS -------------\n");
for (uint8_t i = 0; i <= MAX_NUM_OF_FRIENDS; i++) {
if (i >= friends_list.num_of_friends) {
break;
}
uint8_t str[50] = {0};
ble_address_to_string_convert(friends_list.friends[i].address, str);
SEGGER_RTT_printf(0, "Friends Are: color index: %02x, friend address: 0x%s\n", friends_list.friends[i].color_index, str);
}
SEGGER_RTT_WriteString(0, "------------- END FRIENDS -------------\n");
}
int main(void)
{
ble_stack_init();
friend_storage_init();
nrf_delay_ms(1000); //wait because fds is doing stuff.
SEGGER_RTT_WriteString(0, "we got here2!\n");
print_friends();
load_friends(&friends_list);
print_friends();
if (!has_friends(&friends_list)) {
init_friends_list(&friends_list);
}
add_friend(&friends_list);
print_friends();
nrf_delay_ms(1000);
print_friends();
nrf_delay_ms(1000);
for(;;) {
nrf_delay_ms(10000);
add_friend(&friends_list);
nrf_delay_ms(10000);
save_friends(&friends_list);
ret_code_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
}
}
/**
* @}
*/