Skip to content

Commit

Permalink
Tidying up, deal with more control commands
Browse files Browse the repository at this point in the history
  • Loading branch information
drdpj committed Jan 18, 2023
1 parent aa9d62f commit bbb6341
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
6 changes: 2 additions & 4 deletions Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ extern "C" {
#define MIDI_ON 0b10010000
#define MIDI_OFF 0b10000000
#define MIDI_CONT 0b10110000
#define MIDI_PRES 0b11010000
#define MIDI_PROG 0b11000000
#define MIDI_SYSCOM 0b11110000
#define MIDI_IGNORE 255
#define MIDI_PED 64
#define MIDI_NOTES_OFF 123
#define MIDI_SOUND_OFF 120
#define PEDAL_ZONE 0
#define FALSE 0
#define TRUE 1
Expand Down
54 changes: 11 additions & 43 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* opensource.org/licenses/BSD-3-Clause
*
* USER CODE segments
* Copyright (C) 2019 Daniel Jameson
* Copyright (C) 2019-2023 Daniel Jameson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -50,7 +50,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "utilities.h"
#include "stdbool.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand All @@ -60,28 +60,6 @@

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MIDI_CHANNEL_MASK 0b00001111
#define MIDI_MASK 0b11110000
#define MIDI_ON 0b10010000
#define MIDI_OFF 0b10000000
#define MIDI_CONT 0b10110000
#define MIDI_PRES 0b11010000
#define MIDI_PROG 0b11000000
#define MIDI_SYSCOM 0b11110000
#define MIDI_IGNORE 255
#define MIDI_PED 64
#define PEDAL_ZONE 0
#define FALSE 0
#define TRUE 1

/* MIDI states */
#define STATUS_MESSAGE 1
#define ONE_DATABYTE 2
#define TWO_DATABYTE 3
#define SYS_EX 4

/* Some USART defs - ring buffer etc */
#define RX_BUFFER_SIZE 256


/* USER CODE END PD */
Expand Down Expand Up @@ -131,6 +109,7 @@ void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI2_Init(void);

/* USER CODE BEGIN PFP */
inline void parseMidi(void);
inline void Note_On(uint8_t);
Expand All @@ -141,21 +120,6 @@ inline void Note_Off(uint8_t);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/*static bool msgrx_circ_buf_is_empty(void) {
if(rd_ptr == DMA_WRITE_PTR) {
return true;
}
return false;
}
static uint8_t msgrx_circ_buf_get(void) {
uint8_t c = 0;
if(rd_ptr != DMA_WRITE_PTR) {
c = dmaRingBuffer[rd_ptr++];
rd_ptr &= (RX_BUFFER_SIZE - 1);
}
return c;
}*/

/* USER CODE END 0 */

Expand All @@ -166,7 +130,7 @@ static uint8_t msgrx_circ_buf_get(void) {
int main(void)
{
/* USER CODE BEGIN 1 */
/*uint8_t midiByte = 0;*/

uint8_t i = 0;

/* all keys high to start with */
Expand All @@ -176,8 +140,6 @@ int main(void)
outputArray[i] = 0xffff;
}



/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/
Expand Down Expand Up @@ -205,7 +167,6 @@ int main(void)
/* enable the SPI and UART - not using the HAL read functions so need to turn them on */
__HAL_SPI_ENABLE(&hspi2);
__HAL_UART_ENABLE(&huart2);
/*HAL_UART_Receive_DMA(huart_cobs, dmaRingBuffer, RX_BUFFER_SIZE);*/
/* USER CODE END 2 */

/* Infinite loop */
Expand Down Expand Up @@ -406,13 +367,20 @@ inline void parseMidi()
Note_Off(midiData1);
break;
case MIDI_CONT:
/* Handle the pedal */
if (midiData1 == MIDI_PED)
{
if (midiData2 < 64) {
keyboardMatrix[PEDAL_ZONE] |= (1 << 6);
}
else keyboardMatrix[PEDAL_ZONE] &= ~(1 << 6);
}
/* Handle the sound off/notes off control commands - everything off! */
if (midiData1 == MIDI_SOUND_OFF || midiData1 == MIDI_NOTES_OFF) {
for (uint_fast8_t i = 36; i<85; i++) {
Note_Off(i);
}
}
break;

default:
Expand Down
8 changes: 7 additions & 1 deletion Src/stm32f1xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* opensource.org/licenses/BSD-3-Clause
*
* USER CODE segments
* Copyright (C) 2019 Daniel Jameson
* Copyright (C) 2019-2023 Daniel Jameson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -300,18 +300,23 @@ midiByte = (uint8_t)huart2.Instance->DR;
switch (state)
{
case 0:
/* standard MIDI messages, and we really don't want any of the running stuff */
if (midiByte >0x7F && midiByte <0xF0)
{
midiStatus = midiByte;
state = 1;
}
/* if we get a system exclusive message...*/
if (midiByte == 0xF0) state = 3;
break;

case 1:
/* Data bytes are all < 0x80 */
if (midiByte <0x80)
{
midiData1 = midiByte;

/* 0xC0 and 0xD0 statuses are all 1 byte data */
if (((midiStatus & MIDI_MASK) == 0xC0) || ((midiStatus & MIDI_MASK) == 0xD0))
{
state = 0;
Expand All @@ -326,6 +331,7 @@ midiByte = (uint8_t)huart2.Instance->DR;
break;

case 3:
/* we carry this on until we get an 0xF7 byte to end the sys*/
if (midiByte == 0xF7) state = 0;
if (midiByte >0x7F && midiByte <0xF0) {
midiStatus = midiByte;
Expand Down
2 changes: 1 addition & 1 deletion Src/utilities.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* utilities.c for STM32 MIDI to M4000 controller.
* Copyright (C) 2019 Daniel Jameson
* Copyright (C) 2019-2023 Daniel Jameson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit bbb6341

Please sign in to comment.