Skip to content

Commit

Permalink
attempting to send status and read MIDI data
Browse files Browse the repository at this point in the history
  • Loading branch information
pingdynasty committed Oct 20, 2019
1 parent b3f0a11 commit 366eb16
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 130 deletions.
41 changes: 0 additions & 41 deletions MidiBLE/inc/peripheral_mngr_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@
#include "ble_const.h"
#include "bluenrg1_api.h"

/** @addtogroup BLUEMIC_1_APP BLUEMIC_1_APP
* @{
*/

/** @addtogroup BLUEMIC_1_APP_MNGR BLUEMIC_1_APP_MNGR
* @{
*/

/** @defgroup APP_MNGR_Exported_Types APP_MNGR_Exported_Types
* @{
*/

/**
* @brief BlueMic-1 application status
*/
typedef enum
{
APP_SUCCESS = 0x00, /*!< APP Success.*/
Expand All @@ -77,24 +62,10 @@ typedef enum
* @}
*/

/** @defgroup APP_MNGR_Exported_Defines APP_MNGR_Exported_Defines
* @{
*/
#define APP_STATUS_ADVERTISEMENT (0x10) /*!< BlueVoice Peripheral device is in advertisement mode.*/
#define APP_STATUS_CONNECTED (0x20) /*!< BlueVoice device is connected.*/
/**
* @}
*/

/* Exported variables --------------------------------------------------------*/
extern volatile uint8_t AccGryro_DataReady;
extern volatile uint8_t APP_PER_state;

/* Exported functions ------------------------------------------------------- */

/** @defgroup APP_MNGR_Functions_Prototype APP_MNGR_Functions_Prototype
* @{
*/

/**
* @brief Process user input.
Expand Down Expand Up @@ -131,18 +102,6 @@ APP_Status PER_APP_Advertise(void);
*/
void PER_APP_Error_Handler(void);

/**
* @}
*/

/**
* @}
*/

/**
* @}
*/

#endif /* __PERIPHERAL_MNGR_APP_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
3 changes: 1 addition & 2 deletions MidiBLE/src/BlueNRG1_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ void SVC_Handler(void)
*/
void SysTick_Handler(void)
{
usiTimerVal++;
usiTimerVal++; // used by outgoing MIDI messages
lSystickCounter++;
/* BluevoiceADPCM_BNRG1_IncTick(); */
}


Expand Down
13 changes: 9 additions & 4 deletions MidiBLE/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
#define SPI_MISO_PIN GPIO_Pin_2

/* Private variables ---------------------------------------------------------*/
volatile uint32_t lSystickCounter=0;
uint8_t ucClassVector;
volatile uint32_t lSystickCounter = 0;
extern uint16_t MidiServiceHandle;
extern uint16_t MidiHandle;
extern volatile uint8_t APP_PER_state;

/* Private function prototypes -----------------------------------------------*/
void init(void);
Expand All @@ -70,9 +71,9 @@ void readSpiPacket(){

// Delay
/* while(delay--){} */

uint8_t rxbuf[4];
uint8_t txbuf[4] = {0x01, 0x02, 0x03, 0x04};
uint8_t txbuf[4] = {APP_PER_state, APP_PER_state, APP_PER_state, APP_PER_state};

rxbuf[0]= SPI_ReceiveData();
SPI_SendData(txbuf[0]);
Expand Down Expand Up @@ -120,6 +121,10 @@ int main(void)
/*Set module in advertise mode*/
PER_APP_Advertise();

// todo: probably need to do something like this to read MIDI data
/* int ret = MODE_APP_DataRead(MidiServiceHandle); */
/* int ret = MODE_APP_DataRead(MidiHandle); */

/* Infinite loop */
while(1)
{
Expand Down
71 changes: 24 additions & 47 deletions MidiBLE/src/midi_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,67 +61,44 @@ MIDI_APP_Status MODE_APP_add_char(uint16_t service_handle)

uint8_t MODE_APP_DataRead(uint16_t service_handle)
{
if(aci_gatt_read_char_value(service_handle, ModeHandle)==BLE_STATUS_INSUFFICIENT_RESOURCES)
{
return MIDI_APP_ERROR;
}
return MIDI_APP_SUCCESS;
if(aci_gatt_read_char_value(service_handle, ModeHandle)==BLE_STATUS_INSUFFICIENT_RESOURCES)
{
return MIDI_APP_ERROR;
}
return MIDI_APP_SUCCESS;
}

// _____ MIDI Handle ______________________________________________________________________________________________________________
MIDI_APP_Status MIDI_APP_add_char(uint16_t service_handle)
{
uint8_t ret = aci_gatt_add_char(service_handle,
UUID_TYPE_128, (Char_UUID_t *) MIDI_IO_char_uuid,
5, CHAR_PROP_READ|CHAR_PROP_NOTIFY|CHAR_PROP_WRITE_WITHOUT_RESP, ATTR_PERMISSION_ENCRY_WRITE, GATT_NOTIFY_ATTRIBUTE_WRITE, 16, 1,
(uint16_t*)&MidiHandle);
UUID_TYPE_128, (Char_UUID_t *) MIDI_IO_char_uuid,
5, CHAR_PROP_READ|CHAR_PROP_NOTIFY|CHAR_PROP_WRITE_WITHOUT_RESP, ATTR_PERMISSION_ENCRY_WRITE, GATT_NOTIFY_ATTRIBUTE_WRITE, 16, 1,
(uint16_t*)&MidiHandle);

if (ret != BLE_STATUS_SUCCESS)
{
return MIDI_APP_ERROR;
}
{
return MIDI_APP_ERROR;
}

return MIDI_APP_SUCCESS;
}


MIDI_APP_Status MIDI_APP_DataUpdate(uint16_t service_handle, uint8_t channel, uint8_t note, uint8_t vector)
{
uint8_t rgBuff[5];

// Ensure channel is not 0
if (!channel) channel = 1;

// Build Tx packet
rgBuff[0] = 0x80 | ((usiTimerVal >> 7) & 0x3F);
rgBuff[1] = 0x80 | (usiTimerVal & 0x007F);
rgBuff[2] = 0x90 | ((channel-1) & 0x0F);
rgBuff[3] = note & 0x7F;
rgBuff[4] = vector & 0x7F;

// Update value
if(aci_gatt_update_char_value(service_handle, MidiHandle, 0, sizeof rgBuff, rgBuff)==BLE_STATUS_INSUFFICIENT_RESOURCES)
{
return MIDI_APP_ERROR;
}
return MIDI_APP_SUCCESS;
}

MIDI_APP_Status MIDI_APP_Passthrough(uint16_t service_handle, uint8_t* data)
{
uint8_t rgBuff[5];
uint8_t rgBuff[5];

// Build Tx packet
rgBuff[0] = 0x80 | ((usiTimerVal >> 7) & 0x3F);
rgBuff[1] = 0x80 | (usiTimerVal & 0x007F);
rgBuff[2] = data[1];
rgBuff[3] = data[2];
rgBuff[4] = data[3];
// Build Tx packet
rgBuff[0] = 0x80 | ((usiTimerVal >> 7) & 0x3F);
rgBuff[1] = 0x80 | (usiTimerVal & 0x007F);
rgBuff[2] = data[1];
rgBuff[3] = data[2];
rgBuff[4] = data[3];

// Update value
tBleStatus ret;
ret = aci_gatt_update_char_value(service_handle, MidiHandle, 0, sizeof rgBuff, rgBuff);
if(ret == BLE_STATUS_INSUFFICIENT_RESOURCES)
return MIDI_APP_ERROR;
return MIDI_APP_SUCCESS;
// Update value
tBleStatus ret;
ret = aci_gatt_update_char_value(service_handle, MidiHandle, 0, sizeof rgBuff, rgBuff);
if(ret == BLE_STATUS_INSUFFICIENT_RESOURCES)
return MIDI_APP_ERROR;
return MIDI_APP_SUCCESS;
}
65 changes: 29 additions & 36 deletions MidiBLE/src/peripheral_mngr_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@
*/

#include <stddef.h>
#include <string.h>
#include "peripheral_mngr_app.h"

#define MIDI_SERVICE_UUID 0x00,0xC7,0xC4,0x4E,0xE3,0x6C,0x51,0xA7,0x33,0x4B,0xE8,0xED,0x5A,0x0E,0xB8,0x03
#define APP_READY (0x00)
#define APP_BLUEVOICE_ENABLE (0x01)
#define APP_INERTIAL_ENABLE (0x02)

volatile uint16_t ServiceHandle;
extern volatile uint16_t MidiHandle;
volatile uint16_t MidiServiceHandle;
volatile uint16_t conn_handle;
volatile uint8_t APP_PER_enabled = APP_READY;
Expand All @@ -59,17 +60,14 @@ static const uint8_t midi_service_uuid[16] = {0x00,0xC7,0xC4,0x4E,0xE3,0x6C, 0x

volatile uint8_t APP_PER_state = APP_STATUS_ADVERTISEMENT;

uint16_t usiMidiTest, usiPrescaler;

static uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
/**
* @brief BlueNRG-1 Initialization.
* @param None.
* @retval APP_Status: APP_SUCCESS if the configuration is ok, APP_ERROR otherwise.
*/
APP_Status PER_APP_Init_BLE(void)
{
uint16_t service_handle, dev_name_char_handle, appearance_char_handle;

{
aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, PERIPHERAL_BDADDR);

// Set radio power level
Expand Down Expand Up @@ -97,12 +95,10 @@ APP_Status PER_APP_Service_Init(void)
{

// Add Midi service and handle
aci_gatt_add_service(UUID_TYPE_128,
(Service_UUID_t *) midi_service_uuid, PRIMARY_SERVICE, 10,
(uint16_t*)&MidiServiceHandle);

MIDI_APP_add_char(MidiServiceHandle);

aci_gatt_add_service(UUID_TYPE_128,
(Service_UUID_t *) midi_service_uuid, PRIMARY_SERVICE, 10,
(uint16_t*)&MidiServiceHandle);
MIDI_APP_add_char(MidiServiceHandle);
return APP_SUCCESS;
}

Expand All @@ -117,7 +113,8 @@ APP_Status PER_APP_Advertise(void)

uint8_t local_name[] =
{
AD_TYPE_COMPLETE_LOCAL_NAME, 'O','W','L','-','B','i','o','S','i','g','n','a','l','s',
/* AD_TYPE_SHORTENED_LOCAL_NAME, 'O', 'W', 'L', '-', 'B', 'I', 'O' */
AD_TYPE_COMPLETE_LOCAL_NAME, 'O','W','L','-','B','i','o','S','i','g','n','a','l','s',
'-','P','0','0','0'
};

Expand Down Expand Up @@ -145,6 +142,10 @@ APP_Status PER_APP_Advertise(void)
/* uint8_t manuf_data[] = { */
/* 8, AD_TYPE_SHORTENED_LOCAL_NAME, 'O', 'W', 'L', '-', 'B', 'I', 'O' */
/* }; */ // not sure why this returns a 0x41 BLE_STATUS_FAILED
/* uint8_t manuf_data[] = { */
/* 20, AD_TYPE_COMPLETE_LOCAL_NAME, 'O','W','L','-','B','i','o','S','i','g','n','a','l','s', */
/* '-','P','0','0','0' */
/* }; // not sure why this returns a 0x41 BLE_STATUS_FAILED */
/* ret |= aci_gap_update_adv_data(sizeof(manuf_data), manuf_data); */

if (ret != BLE_STATUS_SUCCESS)
Expand All @@ -170,38 +171,17 @@ void hci_le_connection_complete_event(uint8_t Status,
uint8_t Master_Clock_Accuracy)

{
/* Connection completed */
//BluevoiceADPCM_BNRG1_ConnectionComplete_CB(Connection_Handle);

//BSP_LED_On(LED1);

conn_handle = Connection_Handle;
int ret = aci_l2cap_connection_parameter_update_req(conn_handle,
9 /* interval_min*/,
9 /* interval_max */,
0 /* slave_latency */,
400 /*timeout_multiplier*/);


/* In order to use an iOS device as receiver, with the ST BlueMS app, please substitute the following function with the previous. */
/* With iOS only the 8kHz (as audio sampling frequency) version is available */
// int ret = aci_l2cap_connection_parameter_update_req(conn_handle,
// 8 /* interval_min*/,
// 17 /* interval_max */,
// 0 /* slave_latency */,
// 400 /*timeout_multiplier*/);

/* In order to use an Android device version 4 as receiver, with audio sampling frequancy @16kHz, */
/* please substitute the following function with the previous. */
// int ret = aci_l2cap_connection_parameter_update_req(conn_handle,
// 8 /* interval_min*/,
// 8 /* interval_max */,
// 0 /* slave_latency */,
// 400 /*timeout_multiplier*/);

400 /*timeout_multiplier*/);
if (ret != BLE_STATUS_SUCCESS)
{
while (1);
while (1); // todo: replace with reset or reinit
}

APP_PER_state = APP_STATUS_CONNECTED;
Expand Down Expand Up @@ -242,6 +222,9 @@ void hci_disconnection_complete_event(uint8_t Status,
* Output : See file bluenrg1_events.h
* Return : See file bluenrg1_events.h
*******************************************************************************/
uint16_t attr_handle = 0;
uint8_t midi_handle_value_len = 0;
uint8_t midi_handle_value[4] = {};
void aci_gatt_attribute_modified_event(uint16_t Connection_Handle,
uint16_t Attr_Handle,
uint16_t Offset,
Expand Down Expand Up @@ -278,6 +261,16 @@ void aci_gatt_attribute_modified_event(uint16_t Connection_Handle,
/* if(Attr_Handle == MidiServiceHandle) */
/* { */
/* } */

// todo: after MODE_APP_DataRead, need to catch data here
if(Attr_Handle == MidiHandle+2){
midi_handle_value_len = Attr_Data_Length;
if(Attr_Data_Length <= sizeof(midi_handle_value))
memcpy(midi_handle_value, Attr_Data, Attr_Data_Length);
else
memcpy(midi_handle_value, Attr_Data, sizeof(midi_handle_value));
}
attr_handle = Attr_Handle;
}

void aci_gatt_tx_pool_available_event(uint16_t Connection_Handle,
Expand Down

0 comments on commit 366eb16

Please sign in to comment.