import sys
import time 
import asyncio

import numpy as np
import pandas as pd
import tkinter as tk  
import matplotlib.pyplot as plt

from bleak import BleakClient
import struct

import csv
from datetime import datetime

# Set BLE divece MAC adress
address = ""

S_uuid = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
C_uuid = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"

#--------------------------------------------------------------------------------------------#

# 2ch
PLOT_SIZE =2
ADC_SAMPLE_SIZE = 64

SAMPLING_FS = 2*1000
TIME_LENGTH = 5

# ※equal size with PLOT_SIZE 
figureSize = (10,8) #plot figure size
titles = ["value1","value2","value3","value4"] # subplot title
minMaxs = [[0,pow(2,8)],[0,pow(2,8)],[0,pow(2,8)],[0,pow(2,8)]]  #subplot ylim
lineType = ["r-","c-","b-","m-"] 

#file name
FILE_NAME = datetime.now().strftime("%Y%m%d_%H_%M_%S")
#--------------------------------------------------------------------------------------------#

#file save path
FILE_PATH ="./"+ FILE_NAME + ".csv"

# ble data parsing format 
def bleDataParsing(data:bytearray):
    parsingData = []
    #8bit
    for i in range(len(data)):
        parsingData.append(data[i])
        
    return parsingData

# 1. plot
N = SAMPLING_FS* TIME_LENGTH 
data_x = np.arange(0, N, 1)
data_y = np.ones((PLOT_SIZE,N)) 

plt.ion()
plt.rcParams.update({'font.size': 18})
figure =  plt.figure(figsize=figureSize)
def onclick(event):
    print("click")
    with open(FILE_PATH ,'a', newline='',encoding='utf8') as f:
        reader = csv.reader(f)
        wr = csv.writer(f)
        wr.writerow("")
figure.canvas.mpl_connect('button_press_event', onclick)

subplots = []
lines = []
for i in range(PLOT_SIZE):
    ax = figure.add_subplot(PLOT_SIZE,1,i+1)
    l, = ax.plot(data_x, data_y[i],lineType[i], linewidth=3)
    subplots.append(ax)
    lines.append(l)
    plt.title(titles[i])

figure.tight_layout()

async def plot():
    while True:
        for i in range(PLOT_SIZE):
            lines[i].set_data(data_x,data_y[i])
            subplots[i].set_ylim(minMaxs[i])
            subplots[i].set_xlim(0,N)
        figure.canvas.flush_events()
        await asyncio.sleep(0.05)

# 2. BLE
PLOT_FLAG = 0
def bleDataSet(parsingData):
    global PLOT_FLAG
    buff = []
    for i in range(len(parsingData)):
        #print(i%PLOT_SIZE, PLOT_FLAG)
        buff.append(parsingData[i])
        data_y[i%PLOT_SIZE][PLOT_FLAG] = parsingData[i]
        if(i%PLOT_SIZE==0):
            PLOT_FLAG+=1
            if(PLOT_FLAG ==N):
                PLOT_FLAG = 0
    
def callback(sender:int , data:bytearray):
    #read
    parsingData = bleDataParsing(data)
    #print(parsingData)
    bleDataSet(parsingData)

    with open(FILE_PATH ,'a', newline='',encoding='utf8') as f:
        reader = csv.reader(f)
        wr = csv.writer(f)
        buff = []
        for data in parsingData:
            buff.append(data)
            current_time = datetime.now()
            buff.append(current_time)
        wr.writerow(buff)
    
async def ble(address):
    print("start")
    client = BleakClient(address)
    try:
        #connect
        print("connect start")
        await client.connect() 
        print("connect to " + client.address)

        # notify
        await client.start_notify(C_uuid,callback)
        
        while(True):
            data = await client.read_gatt_char(C_uuid)

    except Exception as e:
        print(e)
        pass

    finally:
        await client.disconnect()

async def main():
    t1 = loop.create_task(ble(address))
    t2 = loop.create_task(plot())
    await t2, t1
     
if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    loop.run_until_complete(main())





