Skip to content

Commit

Permalink
additional polish
Browse files Browse the repository at this point in the history
  • Loading branch information
kevvz committed Sep 30, 2024
1 parent 478ffdb commit 8c9ef4f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Friend/firmware/firmware_v1.0/src/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <zephyr/bluetooth/services/bas.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/gpio.h>
#include "transport.h"
#include "button.h"
#include "transport.h"

LOG_MODULE_REGISTER(button, CONFIG_LOG_DEFAULT_LEVEL);

Expand Down
1 change: 0 additions & 1 deletion Friend/firmware/firmware_v1.0/src/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "codec.h"
#include "config.h"
#include "utils.h"
#include "sdcard.h"
#ifdef CODEC_OPUS
#include "lib/opus-1.2.1/opus.h"
#endif
Expand Down
27 changes: 20 additions & 7 deletions Friend/firmware/firmware_v1.0/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ int main(void)
// Indicate transport initialization
set_led_green(true);
set_led_green(false);
err = mount_sd_card();
LOG_INF("result of mount:%d",err);

k_msleep(500);
storage_init();

err = transport_start();
if (err)
Expand All @@ -137,9 +132,27 @@ int main(void)
k_msleep(200);
}
set_led_green(false);
return err;
// return err;
}
err = mount_sd_card();
if (err)
{
LOG_ERR("Failed to mount SD card: %d", err);
}
LOG_INF("result of mount:%d",err);

k_msleep(500);
err = storage_init();
if (err)
{
LOG_ERR("Failed to initialize storage: %d", err);
}
err = init_haptic_pin();
if (err)
{
LOG_ERR("Failed to initialize haptic pin: %d", err);
}
init_haptic_pin();

set_led_blue(true);
set_codec_callback(codec_handler);
err = codec_start();
Expand Down
7 changes: 4 additions & 3 deletions Friend/firmware/firmware_v1.0/src/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int mount_sd_card(void)
return -1;
}
//initialize the sd card
static const char *disk_pdrv = "SD";
const char *disk_pdrv = "SD";
int err = disk_access_init(disk_pdrv);
LOG_INF("disk_access_init: %d\n", err);
if (err)
Expand Down Expand Up @@ -238,7 +238,8 @@ int write_to_file(uint8_t *data,uint32_t length)
return 0;
}

int initialize_audio_file(uint8_t num) {
int initialize_audio_file(uint8_t num)
{
char *header = generate_new_audio_header(num);
if (header == NULL)
{
Expand Down Expand Up @@ -379,7 +380,7 @@ int clear_audio_directory()
return -1;
}
printk("done with clearing\n");

file_count = 1;
move_write_pointer(1);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions Friend/firmware/firmware_v1.0/src/speaker.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <math.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>

#include "speaker.h"
LOG_MODULE_REGISTER(speaker, CONFIG_LOG_DEFAULT_LEVEL);


Expand Down Expand Up @@ -140,8 +140,8 @@ uint16_t speak(uint16_t len, const void *buf) //direct from bt
void generate_gentle_chime(int16_t *buffer, int num_samples)
{
LOG_INF("Generating gentle chime");//2500
float frequencies[] = {523.25, 659.25, 783.99, 1046.50}; // C5, E5, G5, C6
int num_freqs = sizeof(frequencies) / sizeof(frequencies[0]);//4
const float frequencies[] = {523.25, 659.25, 783.99, 1046.50}; // C5, E5, G5, C6
const int num_freqs = sizeof(frequencies) / sizeof(frequencies[0]);//4

for (int i = 0; i < num_samples; i++)
{
Expand All @@ -162,7 +162,7 @@ int play_boot_sound(void)
{
int ret;
int16_t *buffer = (int16_t *) buzz_buffer;
int samples_per_block = MAX_BLOCK_SIZE / (NUM_CHANNELS * sizeof(int16_t));
const int samples_per_block = MAX_BLOCK_SIZE / (NUM_CHANNELS * sizeof(int16_t));

generate_gentle_chime(buffer, samples_per_block);
LOG_INF("Writing to speaker");
Expand Down
2 changes: 1 addition & 1 deletion Friend/firmware/firmware_v1.0/src/speaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int speaker_init();
*
* @return The amount of data successfully sent in bytes.
*/
uint16_t speak();
uint16_t speak(uint16_t len, const void *buf);

/**
* @brief Play a chime effect
Expand Down
14 changes: 6 additions & 8 deletions Friend/firmware/firmware_v1.0/src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ uint32_t remaining_length = 0;

static int setup_storage_tx()
{
transport_started= (uint8_t)0;
transport_started = (uint8_t)0;
// offset = 0;
LOG_INF("about to transmit storage\n");
k_msleep(1000);
Expand Down Expand Up @@ -146,8 +146,8 @@ static uint8_t parse_storage_command(void *buf,uint16_t len)
LOG_INF("invalid command");
return INVALID_COMMAND;
}
uint8_t command = ((uint8_t*)buf)[0];
uint8_t file_num = ((uint8_t*)buf)[1];
const uint8_t command = ((uint8_t*)buf)[0];
const uint8_t file_num = ((uint8_t*)buf)[1];
uint32_t size = 0;
if ( len == 6 )
{
Expand All @@ -169,7 +169,7 @@ static uint8_t parse_storage_command(void *buf,uint16_t len)
if (command == READ_COMMAND) //read
{
uint32_t temp = file_num_array[file_num-1];
if ( file_num == (file_count ) )
if ( file_num == ( file_count ) )
{
LOG_INF("file_count == final file");
offset = size;
Expand Down Expand Up @@ -240,7 +240,7 @@ static void write_to_gatt(struct bt_conn *conn)
storage_write_buffer[1] = (id >> 8) & 0xFF;
storage_write_buffer[2] = index;

uint32_t packet_size = MIN(remaining_length,OPUS_ENTRY_LENGTH);
const uint32_t packet_size = MIN(remaining_length,OPUS_ENTRY_LENGTH);

int r = read_audio_data(storage_write_buffer+FRAME_PREFIX_LENGTH,packet_size,offset);
offset = offset + packet_size;
Expand All @@ -261,8 +261,6 @@ static void write_to_gatt(struct bt_conn *conn)
// int err = bt_gatt_notify(conn, &storage_service.attrs[1], &storage_write_buffer,packet_size);
// }



void storage_write(void)
{
while (1)
Expand Down Expand Up @@ -326,7 +324,7 @@ void storage_write(void)
{
printk("done. attempting to download more files\n");
uint8_t stop_result[1] = {100};
int err = bt_gatt_notify(conn, &storage_service.attrs[1], &stop_result,1);
int err = bt_gatt_notify(get_current_connection(), &storage_service.attrs[1], &stop_result,1);
k_sleep(K_MSEC(10));
}

Expand Down
75 changes: 39 additions & 36 deletions Friend/firmware/firmware_v1.0/src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "sdcard.h"
#include "storage.h"
#include "lib/battery/battery.h"
#include "btutils.h"

LOG_MODULE_REGISTER(transport, CONFIG_LOG_DEFAULT_LEVEL);

Expand Down Expand Up @@ -130,15 +131,17 @@ void broadcast_accel(struct k_work *work_item) {

//only time mega sensor is changed is through here (hopefully), so no chance of race condition
int err = bt_gatt_notify(current_connection, &accel_service.attrs[1], &mega_sensor, sizeof(mega_sensor));
if (err) {
if (err)
{
LOG_ERR("Error updating Accelerometer data");
}
k_work_reschedule(&accel_work, K_MSEC(ACCEL_REFRESH_INTERVAL));
}


//use d4,d5
static void accel_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) {
static void accel_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value)
{
if (value == BT_GATT_CCC_NOTIFY)
{
LOG_INF("Client subscribed for notifications");
Expand All @@ -153,34 +156,39 @@ static void accel_ccc_config_changed_handler(const struct bt_gatt_attr *attr, ui
}
}

int accel_start() {
int accel_start()
{
struct sensor_value odr_attr;
lsm6dsl_dev = DEVICE_DT_GET_ONE(st_lsm6dsl);
k_msleep(50);
if (lsm6dsl_dev == NULL) {
if (lsm6dsl_dev == NULL)
{
LOG_ERR("Could not get LSM6DSL device");
return 0;
}
if (!device_is_ready(lsm6dsl_dev)) {
if (!device_is_ready(lsm6dsl_dev))
{
LOG_ERR("LSM6DSL: not ready");
return 0;
}
odr_attr.val1 = 52;
odr_attr.val2 = 0;

if (sensor_attr_set(lsm6dsl_dev, SENSOR_CHAN_ACCEL_XYZ,
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
LOG_ERR("Cannot set sampling frequency for Accelerometer.");
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0)
{
LOG_ERR("Cannot set sampling frequency for Accelerometer.");
return 0;
}
if (sensor_attr_set(lsm6dsl_dev, SENSOR_CHAN_GYRO_XYZ,
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
LOG_ERR("Cannot set sampling frequency for gyro.");
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
LOG_ERR("Cannot set sampling frequency for gyro.");
return 0;
}
if (sensor_sample_fetch(lsm6dsl_dev) < 0) {
LOG_ERR("Sensor sample update error");
return 0;
if (sensor_sample_fetch(lsm6dsl_dev) < 0)
{
LOG_ERR("Sensor sample update error");
return 0;
}

LOG_INF("Accelerometer is ready for use \n");
Expand Down Expand Up @@ -345,9 +353,9 @@ static void _transport_connected(struct bt_conn *conn, uint8_t err)

// // Put NFC to sleep when Bluetooth is connected
// nfc_sleep();
#ifdef CONFIG_ACCELEROMETER
k_work_schedule(&accel_work, K_MSEC(ACCEL_REFRESH_INTERVAL));
#endif
// #ifdef CONFIG_ACCELEROMETER
// k_work_schedule(&accel_work, K_MSEC(ACCEL_REFRESH_INTERVAL));
// #endif

}

Expand Down Expand Up @@ -384,8 +392,8 @@ static void _le_param_updated(struct bt_conn *conn, uint16_t interval,
static void _le_phy_updated(struct bt_conn *conn,
struct bt_conn_le_phy_info *param)
{
LOG_DBG("LE PHY updated: TX PHY %s, RX PHY %s",
phy2str(param->tx_phy), phy2str(param->rx_phy));
// LOG_DBG("LE PHY updated: TX PHY %s, RX PHY %s",
// phy2str(param->tx_phy), phy2str(param->rx_phy));
}

static void _le_data_length_updated(struct bt_conn *conn,
Expand Down Expand Up @@ -467,7 +475,7 @@ static bool read_from_tx_queue()
//

// Thread
K_THREAD_STACK_DEFINE(pusher_stack, 2048);
K_THREAD_STACK_DEFINE(pusher_stack, 4096);
static struct k_thread pusher_thread;
static uint16_t packet_next_index = 0;
static uint8_t pusher_temp_data[CODEC_OUTPUT_MAX_BYTES + NET_BUFFER_HEADER_SIZE];
Expand Down Expand Up @@ -531,14 +539,15 @@ static bool push_to_gatt(struct bt_conn *conn)
#define MAX_WRITE_SIZE 400
static uint32_t offset = 0;
static uint16_t buffer_offset = 0;
bool write_to_storage(void) {
bool write_to_storage(void)
{
if (!read_from_tx_queue())
{
return false;
}

uint8_t *buffer = tx_buffer+2;
uint32_t packet_size = tx_buffer_size;
const uint32_t packet_size = tx_buffer_size;
//load into write at 400 bytes at a time. is faster
memcpy(storage_temp_data + OPUS_PREFIX_LENGTH + buffer_offset, buffer, packet_size);
storage_temp_data[buffer_offset] = (uint8_t)tx_buffer_size;
Expand Down Expand Up @@ -598,25 +607,21 @@ static bool use_storage = true;
#define MAX_AUDIO_FILE_SIZE 300000
static int recent_file_size_updated = 0;

void update_file_size()
{
file_num_array[0] = get_file_size(1);
printk("file size for file count %d %d\n",file_count,file_num_array[0]);
}
void update_file_size()
{
file_num_array[0] = get_file_size(1);
printk("file size for file count %d %d\n",file_count,file_num_array[0]);
}

void pusher(void)
{
k_msleep(500);
while (1)
{


//
// Load current connection
//

struct bt_conn *conn = current_connection;
bool use_gatt = true;
//updating the most recent file size is expensive!
static bool file_size_updated = true;
static bool connection_was_true = false;
Expand Down Expand Up @@ -663,19 +668,17 @@ void pusher(void)
}
else
{
k_sleep(K_MSEC(10));

}
}
if (valid)
{

bool sent = push_to_gatt(conn);
if (!sent)
{
k_sleep(K_MSEC(50));
// k_sleep(K_MSEC(50));
}
}

if (conn)
{
bt_conn_unref(conn);
Expand All @@ -702,7 +705,7 @@ int transport_start()
return err;
}
LOG_INF("Transport bluetooth initialized");
// Enable accelerometer
// Enable accelerometer
#ifdef CONFIG_ACCELEROMETER
err = accel_start();
if (!err)
Expand All @@ -711,10 +714,11 @@ int transport_start()
}
else
{
LOG_INF("Accelerometer initialized");
bt_gatt_service_register(&accel_service);
}
#endif
// Enable button
// Enable button
#ifdef CONFIG_ENABLE_BUTTON
button_init();
register_button_service();
Expand All @@ -734,7 +738,6 @@ int transport_start()
}

play_boot_sound();

#endif
// Start advertising

Expand Down

0 comments on commit 8c9ef4f

Please sign in to comment.