Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SD Card support for App and Added Commands for SD Card reading #752 #789

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Friend/firmware/firmware_v1.0/src/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,23 @@ int get_next_item(struct fs_dir_t *zdp, struct fs_dirent *entry) {
count++;
}
return count;
}
//we should clear instead of delete since we lose fifo structure
int clear_audio_file(uint8_t num) {

char *ptr = generate_new_audio_header(num);
snprintf(current_full_path, sizeof(current_full_path), "%s%s", disk_mount_pt, ptr);
free(ptr);
int res = fs_unlink(&current_full_path);
if (res) {
printk("error deleting file\n");
return -1;
}
res = create_file(&current_full_path);
if (res) {
printk("error creating file\n");
return -1;
}

return 0;
}
109 changes: 90 additions & 19 deletions Friend/firmware/firmware_v1.0/src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ LOG_MODULE_REGISTER(storage, CONFIG_LOG_DEFAULT_LEVEL);
#define OPUS_ENTRY_LENGTH 100
#define FRAME_PREFIX_LENGTH 3

#define READ_COMMAND 0
#define DELETE_COMMAND 1
static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value);
static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags);

Expand Down Expand Up @@ -68,7 +70,7 @@ static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint
static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) {
k_msleep(10);
// char amount[1] = {file_count};
uint32_t amount[20] = {0};
uint32_t amount[50] = {0};
for (int i = 0; i < file_count; i++) {
amount[i] = file_num_array[i];
}
Expand All @@ -79,15 +81,6 @@ static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt

uint8_t transport_started = 0;

static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) {

LOG_INF("about to schedule the storage");
transport_started = 1;
k_msleep(1000);


return len;
}

static uint16_t packet_next_index = 0;

Expand All @@ -98,38 +91,108 @@ static uint8_t index = 0;
static uint8_t current_packet_size = 0;
static uint8_t tx_buffer_size = 0;


static uint8_t delete_started = 0;
static uint8_t current_read_num = 1;
uint32_t remaining_length = 0;

static int setup_storage_tx(void) {
static int setup_storage_tx() {
transport_started= (uint8_t)0;
offset = 0;

// offset = 0;
LOG_INF("about to transmit storage\n");
k_msleep(1000);
int res = move_read_pointer(current_read_num);
if (res) {
printk("bad pointer\n");
LOG_ERR("bad pointer\n");
transport_started = 0;
// current_read_num++;
current_read_num = 1;
remaining_length = 0;
return -1;
}
else {
if ((uint32_t)get_file_size(current_read_num) == 0 ) {
if (file_num_array[current_read_num-1] == 0 ) {
// LOG_ERR("bad file size, moving again...");
current_read_num++;
// file_num++;
move_read_pointer(current_read_num);
}
printk("current read ptr %d\n",current_read_num);

remaining_length = get_file_size(current_read_num);
LOG_INF("remaining length: %d",remaining_length);
remaining_length = file_num_array[current_read_num-1];
remaining_length = remaining_length - offset;
// offset=offset_;
printk("remaining length: %d\n",remaining_length);
LOG_INF("offset: %d\n",offset);
printk("file: %d\n",current_read_num);
}
return 0;

}

static int parse_storage_command(void *buf,uint16_t len) {

if (len != 6 && len != 2) {
printk("not the exact length for command");
return -2;
}
uint8_t command = ((uint8_t*)buf)[0];
uint8_t file_num = ((uint8_t*)buf)[1];
uint32_t size = 0;
if ( len == 6 ) {

size = ((uint8_t*)buf)[2] <<24 |((uint8_t*)buf)[3] << 16 | ((uint8_t*)buf)[4] << 8 | ((uint8_t*)buf)[5];

}
printk("command successful: command: %d file: %d size: %d \n",command,file_num,size);

if (file_num == 0) {
printk("invalid file count 0\n");
return -3;
}
if (file_num > file_count) { //invalid file count
printk("invalid file count\n");
return -3;
//add audio all?
}
if (command == READ_COMMAND) { //read
uint32_t temp = file_num_array[file_num-1];
if (temp == 0) {
printk("file size is 0\n");
return -4;
}
if (size > temp) {
printk("requested size is too large\n");
return -5;
}
else {
printk("valid command, setting up \n");
offset = size;
current_read_num = file_num;
transport_started = 1;
}
}
else if (command == DELETE_COMMAND) {
printk("delete something\n");

}
else {
printk("invalid command \n");
return -6;
}
return 0;

}

static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) {

LOG_INF("about to schedule the storage");
printk("was sent %d \n ", ((uint8_t*)buf)[0] );
int result = parse_storage_command(buf,len);
printk("length of storage write: %d\n",len);
printk("result: %d \n", result);

k_msleep(1000);
return len;
}

static void write_to_gatt(struct bt_conn *conn) {
uint32_t id = packet_next_index++;
Expand Down Expand Up @@ -158,6 +221,11 @@ void storage_write(void) {
setup_storage_tx();
}

// if (delete_started) {

// delete_started = 0;
// }

if(remaining_length > 0 ) {

struct bt_conn *conn = get_current_connection();
Expand All @@ -170,6 +238,9 @@ void storage_write(void) {
transport_started = 0;
if (remaining_length == 0) {
printk("done. attempting to download more files\n");
uint8_t stop_result[2] = {100,100};
int err = bt_gatt_notify(conn, &storage_service.attrs[1], &stop_result,1);


// current_read_num++;
k_sleep(K_MSEC(10));
Expand Down
4 changes: 2 additions & 2 deletions Friend/firmware/firmware_v1.0/src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ bool write_to_storage(void) {
return false;
}

uint8_t *buffer = tx_buffer+3;
uint8_t *buffer = tx_buffer+2;
uint32_t packet_size = tx_buffer_size;

memset(storage_temp_data, 0, OPUS_PADDED_LENGTH);
Expand Down Expand Up @@ -597,7 +597,7 @@ void pusher(void)

bool result = write_to_storage();
file_num_array[file_count-1] = get_file_size(file_count);
printk("file size for file count %d %d\n",file_count,file_num_array[file_count-1]);
// printk("file size for file count %d %d\n",file_count,file_num_array[file_count-1]);


if (result)
Expand Down
29 changes: 14 additions & 15 deletions Friend/firmware/testing/decode_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,24 @@
sample_frame = info_char[i*103:(i+1)*103]
amount = int(sample_frame[3])
frame_to_decode = bytes(list(sample_frame[4:4+amount]))
print(frame_to_decode)
print(len(frame_to_decode))

f.append(frame_to_decode)
print(np.frombuffer(frame_to_decode,dtype=np.uint8))

# print(i)
opus_frame = opus_decoder.decode(bytes(frame_to_decode), 160,decode_fec=False)


# for frame in f:
# try:
# decoded_frame = opus_decoder.decode(bytes(frame), 960)
# pcm_data.extend(decoded_frame)
# except Exception as e:
# print(f"Error decoding frame: {e}")
# count+=1
for frame in f:
try:
decoded_frame = opus_decoder.decode(bytes(frame), 320)
pcm_data.extend(decoded_frame)
except Exception as e:
print(f"Error decoding frame: {e}")
count+=1

# with wave.open('decoded_audio.wav', 'wb') as wav_file:
# wav_file.setnchannels(1) # Mono
# wav_file.setsampwidth(2) # 16-bit
# wav_file.setframerate(16000) # Sample rate
# wav_file.writeframes(pcm_data)
with wave.open('decoded_audio.wav', 'wb') as wav_file:
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 16-bit
wav_file.setframerate(16000) # Sample rate
wav_file.writeframes(pcm_data)
# print(count)
18 changes: 11 additions & 7 deletions Friend/firmware/testing/get_audio_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ async def on_notify(sender: bleak.BleakGATTCharacteristic, data: bytearray):
global count
global done
print(len(data))
amount_to_append = data[3]
audio_frames.append(data[4:data[3]+4])
count +=1
print(np.frombuffer(data,dtype=np.uint8))
if (len(data)==1):
print(data[0])
else:
amount_to_append = data[3]
audio_frames.append(data[4:data[3]+4])
count +=1
print(np.frombuffer(data,dtype=np.uint8))


stuff = await client.start_notify(storage_uuid, on_notify)
print('b')

# await client.read_gatt_char(storage_read_uuid)
await asyncio.sleep(1)
await client.write_gatt_char(storage_uuid, b'd', response=True)
print('c')
command = bytearray([0,1,0,0,0,0])
await client.write_gatt_char(storage_uuid, command, response=True)

print(stuff)
await asyncio.sleep(1)
while True:
Expand Down
45 changes: 45 additions & 0 deletions Friend/firmware/testing/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import asyncio
import os
import threading
import time
# from deepgram import (
# DeepgramClient,
# PrerecordedOptions,
# FileSource,
# )

from fastapi import APIRouter, FastAPI,Depends, HTTPException, UploadFile

app = FastAPI()


@app.get("/memory")
async def root():
return {"message": "sexp"}

@app.post("/download_wav")
async def download_wav(file: UploadFile):
with open("downloaded_wav_file.wav", "wb") as f:
f.write(file.file.read())
# try:
# deepgram = DeepgramClient("DEEPGRAM_API_KEY")

# with open(AUDIO_FILE, "rb") as file:
# buffer_data = file.read()

# payload: FileSource = {
# "buffer": buffer_data,
# }

# options = PrerecordedOptions(
# model="nova-2",
# smart_format=True,
# )

# response = deepgram.listen.rest.v("1").transcribe_file(payload, options)

# print(response.to_json(indent=4))

# except Exception as e:
# print(f"Exception: {e}")

29 changes: 28 additions & 1 deletion app/lib/backend/http/api/memories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Future<ServerMemory?> memoryPostProcessing(File file, String memoryId) async {
);
request.files.add(await http.MultipartFile.fromPath('file', file.path, filename: basename(file.path)));
request.headers.addAll({'Authorization': await getAuthHeader()});

try {
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
Expand Down Expand Up @@ -234,3 +234,30 @@ Future<bool> setMemoryEventsState(
debugPrint('setMemoryEventsState: ${response.body}');
return response.statusCode == 200;
}


Future sendStorageToBackend(File file, String memoryId) async {
var request = http.MultipartRequest(
'POST',
// Uri.parse('${Env.apiBaseUrl}v1/memories/$memoryId/post-processing?emotional_feedback=$optEmotionalFeedback'),
Uri.parse('http://127.0.0.1:8000/download_wav'),
);
request.files.add(await http.MultipartFile.fromPath('file', file.path, filename: basename(file.path)));

try {
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);

if (response.statusCode == 200) {
debugPrint('storageSend Response body: ${jsonDecode(response.body)}');

} else {
debugPrint('Failed to storageSend. Status code: ${response.statusCode}');
return null;
}
} catch (e) {
debugPrint('An error occurred storageSend: $e');
return null;
}

}
Loading