Subversion Repositories svn.mios32

Compare Revisions

Ignore whitespace Rev 2262 → Rev 2263

/trunk/apps/examples/blm_scalar_communication/app.c
New file
0,0 → 1,347
// $Id$
/*
* MIOS32 Application Template
*
* ==========================================================================
*
* Copyright (C) <year> <your name> (<your email address>)
* Licensed for personal non-commercial use only.
* All other rights reserved.
*
* ==========================================================================
*/
 
/////////////////////////////////////////////////////////////////////////////
// Include files
/////////////////////////////////////////////////////////////////////////////
 
#include <mios32.h>
#include "app.h"
#include <blm_scalar_master.h>
 
// FreeRTOS
#include <FreeRTOS.h>
#include <portmacro.h>
#include <task.h>
#include <queue.h>
#include <semphr.h>
 
 
/////////////////////////////////////////////////////////////////////////////
// FreeRTOS tasks and semaphores
/////////////////////////////////////////////////////////////////////////////
 
// this semaphore (resp. mutex) ensures exclusive access to the MIDI Out port
xSemaphoreHandle xMIDIOUTSemaphore;
 
// simplify access to this mutex
#define MUTEX_MIDIOUT_TAKE { TASKS_MUTEX_MIDIOUT_Take(); }
#define MUTEX_MIDIOUT_GIVE { TASKS_MUTEX_MIDIOUT_Give(); }
 
// periodic task
static void TASK_Period1mS(void *pvParameters);
#define PRIORITY_TASK_PERIOD1MS ( tskIDLE_PRIORITY + 2 ) // lower than MIDI
 
 
/////////////////////////////////////////////////////////////////////////////
// Local functions
/////////////////////////////////////////////////////////////////////////////
static s32 APP_SYSEX_Parser(mios32_midi_port_t port, u8 midi_in);
static s32 APP_MIDI_TimeOut(mios32_midi_port_t port);
 
static s32 APP_BLM_ButtonCallback(u8 blm, blm_scalar_master_element_t element_type, u8 button_x, u8 button_y, u8 button_depressed);
static s32 APP_BLM_FaderCallback(u8 blm, u8 fader, u8 value);
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called after startup to initialize the application
/////////////////////////////////////////////////////////////////////////////
void APP_Init(void)
{
u8 blm = 0; // currently only a single blm device is supported
 
// create semaphores
xMIDIOUTSemaphore = xSemaphoreCreateRecursiveMutex();
 
// initialize all LEDs
MIOS32_BOARD_LED_Init(0xffffffff);
 
// initialize BLM_SCALAR_MASTER communication handler
BLM_SCALAR_MASTER_Init(0);
 
// install BLM callbacks
BLM_SCALAR_MASTER_ButtonCallback_Init(APP_BLM_ButtonCallback);
BLM_SCALAR_MASTER_FaderCallback_Init(APP_BLM_FaderCallback);
 
// set MIDI port (change here if your MBHP_BLM_SCALAR is connected to a different port)
BLM_SCALAR_MASTER_MIDI_PortSet(blm, UART2); // == IN3/OUT3
 
// install SysEx callback
MIOS32_MIDI_SysExCallback_Init(APP_SYSEX_Parser);
 
// install timeout callback function
MIOS32_MIDI_TimeOutCallback_Init(APP_MIDI_TimeOut);
 
// start tasks
xTaskCreate(TASK_Period1mS, (signed portCHAR *)"Period1mS", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_PERIOD1MS, NULL);
 
// send layout request to MBHP_BLM_SCALAR
BLM_SCALAR_MASTER_SendRequest(blm, 0x00);
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This task is running endless in background
/////////////////////////////////////////////////////////////////////////////
void APP_Background(void)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called each mS from the main task which also handles DIN, ENC
// and AIN events. You could add more jobs here, but they shouldn't consume
// more than 300 uS to ensure the responsiveness of buttons, encoders, pots.
// Alternatively you could create a dedicated task for application specific
// jobs as explained in $MIOS32_PATH/apps/tutorials/006_rtos_tasks
/////////////////////////////////////////////////////////////////////////////
void APP_Tick(void)
{
u8 blm = 0; // currently only a single blm device is supported
static blm_scalar_master_connection_state_t prev_connection_state = BLM_SCALAR_MASTER_CONNECTION_STATE_IDLE;
blm_scalar_master_connection_state_t connection_state = BLM_SCALAR_MASTER_ConnectionStateGet(blm);
 
// print message on connection change and turn on/off the status LED
if( connection_state != prev_connection_state ) {
prev_connection_state = connection_state;
 
if( connection_state == BLM_SCALAR_MASTER_CONNECTION_STATE_IDLE ) {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM has been disconnected."); // will appear ca. 10 seconds after communication is broken
MIOS32_BOARD_LED_Set(1, 0);
MUTEX_MIDIOUT_GIVE;
} else {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM has been connected."); // will appear ca. 5 seconds after BLM has been connected
MIOS32_MIDI_SendDebugMessage("Reported grid layout: %dx%d with %d colours.", BLM_SCALAR_MASTER_NumColumnsGet(blm), BLM_SCALAR_MASTER_NumRowsGet(blm), BLM_SCALAR_MASTER_NumColoursGet(blm));
MIOS32_BOARD_LED_Set(1, 1);
MUTEX_MIDIOUT_GIVE;
}
}
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called each mS from the MIDI task which checks for incoming
// MIDI events. You could add more MIDI related jobs here, but they shouldn't
// consume more than 300 uS to ensure the responsiveness of incoming MIDI.
/////////////////////////////////////////////////////////////////////////////
void APP_MIDI_Tick(void)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called when a MIDI package has been received
/////////////////////////////////////////////////////////////////////////////
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
{
// forward to BLM master
BLM_SCALAR_MASTER_MIDI_Receive(port, midi_package);
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This function parses an incoming sysex stream for MIOS32 commands
/////////////////////////////////////////////////////////////////////////////
static s32 APP_SYSEX_Parser(mios32_midi_port_t port, u8 midi_in)
{
// forward SysEx to BLM MASTER
BLM_SCALAR_MASTER_SYSEX_Parser(port, midi_in);
 
return 0; // no error
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This function parses an incoming sysex stream for MIOS32 commands
/////////////////////////////////////////////////////////////////////////////
static s32 APP_MIDI_TimeOut(mios32_midi_port_t port)
{
// forward timeout to BLM MASTER
BLM_SCALAR_MASTER_MIDI_TimeOut(port);
 
return 0; // no error
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This function is called when a BLM button has been pressed/depressed
/////////////////////////////////////////////////////////////////////////////
static s32 APP_BLM_ButtonCallback(u8 blm, blm_scalar_master_element_t element_id, u8 button_x, u8 button_y, u8 button_depressed)
{
// for demo purposes we implemented following:
// - message will be print whenever a button has been pressed
// later you want to remove these messages from your application, because they consume time!
// - button toggles between green and off
// - if SHIFT button is pressed, the button toggles between red and off
// - the SHIFT button toggles between yellow and off
 
// the BLM_SCALAR driver doesn't store the button state, but we can retrieve it with the colour
u8 shift_button_pressed = BLM_SCALAR_MASTER_LED_Get(blm, BLM_SCALAR_MASTER_ELEMENT_SHIFT, 0, 0) == BLM_SCALAR_MASTER_COLOUR_YELLOW;
 
// current colour
blm_scalar_master_colour_t colour = BLM_SCALAR_MASTER_LED_Get(blm, element_id, button_x, button_y);
 
// set new colour
if( element_id == BLM_SCALAR_MASTER_ELEMENT_SHIFT ) {
blm_scalar_master_colour_t new_colour = button_depressed ? BLM_SCALAR_MASTER_COLOUR_OFF : BLM_SCALAR_MASTER_COLOUR_YELLOW;
BLM_SCALAR_MASTER_LED_Set(blm, element_id, button_x, button_y, new_colour);
} else {
if( !button_depressed ) {
blm_scalar_master_colour_t new_colour = BLM_SCALAR_MASTER_COLOUR_OFF;
if( colour != BLM_SCALAR_MASTER_COLOUR_OFF ) {
new_colour = BLM_SCALAR_MASTER_COLOUR_OFF;
} else {
new_colour = shift_button_pressed ? BLM_SCALAR_MASTER_COLOUR_RED : BLM_SCALAR_MASTER_COLOUR_GREEN;
}
 
BLM_SCALAR_MASTER_LED_Set(blm, element_id, button_x, button_y, new_colour);
}
}
 
 
// print messages for debugging purposes
switch( element_id ) {
case BLM_SCALAR_MASTER_ELEMENT_GRID: {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM Grid Button x=%d y=%d %s\n", button_x, button_y, button_depressed ? "depressed" : "pressed");
MUTEX_MIDIOUT_GIVE;
} break;
 
case BLM_SCALAR_MASTER_ELEMENT_EXTRA_ROW: {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM Extra Row Button: x=%d y=%d %s\n", button_x, button_y, button_depressed ? "depressed" : "pressed");
MUTEX_MIDIOUT_GIVE;
} break;
 
case BLM_SCALAR_MASTER_ELEMENT_EXTRA_COLUMN: {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM Extra Column Button: x=%d y=%d %s\n", button_x, button_y, button_depressed ? "depressed" : "pressed");
MUTEX_MIDIOUT_GIVE;
} break;
 
case BLM_SCALAR_MASTER_ELEMENT_SHIFT: {
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendDebugMessage("BLM Shift Button: x=%d y=%d %s\n", button_x, button_y, button_depressed ? "depressed" : "pressed");
MUTEX_MIDIOUT_GIVE;
} break;
 
default:
return -1; // unexpected element_id
}
 
return 0; // no error
}
 
/////////////////////////////////////////////////////////////////////////////
// This function is called when a BLM fader has been moved
/////////////////////////////////////////////////////////////////////////////
static s32 APP_BLM_FaderCallback(u8 blm, u8 fader, u8 value)
{
// just forward as CC event
MUTEX_MIDIOUT_TAKE;
MIOS32_MIDI_SendCC(DEFAULT, fader, 1, value);
MUTEX_MIDIOUT_GIVE;
 
return 0; // no error
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called before the shift register chain is scanned
/////////////////////////////////////////////////////////////////////////////
void APP_SRIO_ServicePrepare(void)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called after the shift register chain has been scanned
/////////////////////////////////////////////////////////////////////////////
void APP_SRIO_ServiceFinish(void)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called when a button has been toggled
// pin_value is 1 when button released, and 0 when button pressed
/////////////////////////////////////////////////////////////////////////////
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called when an encoder has been moved
// incrementer is positive when encoder has been turned clockwise, else
// it is negative
/////////////////////////////////////////////////////////////////////////////
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This hook is called when a pot has been moved
/////////////////////////////////////////////////////////////////////////////
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
{
}
 
 
/////////////////////////////////////////////////////////////////////////////
// This task is called periodically each mS
/////////////////////////////////////////////////////////////////////////////
static void TASK_Period1mS(void *pvParameters)
{
portTickType xLastExecutionTime;
 
// Initialise the xLastExecutionTime variable on task entry
xLastExecutionTime = xTaskGetTickCount();
 
while( 1 ) {
vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
 
// skip delay gap if we had to wait for more than 5 ticks to avoid
// unnecessary repeats until xLastExecutionTime reached xTaskGetTickCount() again
portTickType xCurrentTickCount = xTaskGetTickCount();
if( xLastExecutionTime < (xCurrentTickCount-5) )
xLastExecutionTime = xCurrentTickCount;
 
// call BLM handler, e.g. to send LED updates
BLM_SCALAR_MASTER_Periodic_mS();
}
}
 
 
/////////////////////////////////////////////////////////////////////////////
// Function to take/give MIDI OUT Semaphore
// Note: mios32_defines.h provides special macros which call this function,
// so that it can also be used by drivers (such as BLM_SCALAR_MASTER)
/////////////////////////////////////////////////////////////////////////////
 
void TASKS_MUTEX_MIDIOUT_Take(void)
{
if( xMIDIOUTSemaphore ) {
while( xSemaphoreTakeRecursive(xMIDIOUTSemaphore, (portTickType)1) != pdTRUE );
}
}
 
void TASKS_MUTEX_MIDIOUT_Give(void)
{
if( xMIDIOUTSemaphore ) {
xSemaphoreGiveRecursive(xMIDIOUTSemaphore);
}
}
Property changes:
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: blm_scalar_communication/app.h
===================================================================
--- blm_scalar_communication/app.h (revision 0)
+++ blm_scalar_communication/app.h (revision 2263)
@@ -0,0 +1,49 @@
+// $Id$
+/*
+ * Header file of application
+ *
+ * ==========================================================================
+ *
+ * Copyright (C) <year> <your name> (<your email address>)
+ * Licensed for personal non-commercial use only.
+ * All other rights reserved.
+ *
+ * ==========================================================================
+ */
+
+#ifndef _APP_H
+#define _APP_H
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Global definitions
+/////////////////////////////////////////////////////////////////////////////
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Global Types
+/////////////////////////////////////////////////////////////////////////////
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Prototypes
+/////////////////////////////////////////////////////////////////////////////
+
+extern void APP_Init(void);
+extern void APP_Background(void);
+extern void APP_Tick(void);
+extern void APP_MIDI_Tick(void);
+extern void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package);
+extern void APP_SRIO_ServicePrepare(void);
+extern void APP_SRIO_ServiceFinish(void);
+extern void APP_DIN_NotifyToggle(u32 pin, u32 pin_value);
+extern void APP_ENC_NotifyChange(u32 encoder, s32 incrementer);
+extern void APP_AIN_NotifyChange(u32 pin, u32 pin_value);
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Export global variables
+/////////////////////////////////////////////////////////////////////////////
+
+
+#endif /* _APP_H */
/blm_scalar_communication/app.h
Property changes:
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: blm_scalar_communication/mios32_config.h
===================================================================
--- blm_scalar_communication/mios32_config.h (revision 0)
+++ blm_scalar_communication/mios32_config.h (revision 2263)
@@ -0,0 +1,35 @@
+// $Id$
+/*
+ * Local MIOS32 configuration file
+ *
+ * this file allows to disable (or re-configure) default functions of MIOS32
+ * available switches are listed in $MIOS32_PATH/modules/mios32/MIOS32_CONFIG.txt
+ *
+ */
+
+#ifndef _MIOS32_CONFIG_H
+#define _MIOS32_CONFIG_H
+
+// The boot message which is print during startup and returned on a SysEx query
+#define MIOS32_LCD_BOOT_MSG_LINE1 "BLM_SCALAR Communication"
+#define MIOS32_LCD_BOOT_MSG_LINE2 "(C) 2016 T.Klose"
+
+
+// map MIDI mutex to BLM_SCALAR master
+// located in tasks.c to access MIDI IN/OUT mutex from external
+extern void TASKS_MUTEX_MIDIOUT_Take(void);
+extern void TASKS_MUTEX_MIDIOUT_Give(void);
+#define BLM_SCALAR_MASTER_MUTEX_MIDIOUT_TAKE { TASKS_MUTEX_MIDIOUT_Take(); }
+#define BLM_SCALAR_MASTER_MUTEX_MIDIOUT_GIVE { TASKS_MUTEX_MIDIOUT_Give(); }
+
+
+#if defined(MIOS32_FAMILY_STM32F10x)
+// enable third UART
+# define MIOS32_UART_NUM 3
+#else
+// enable third and fourth UART
+# define MIOS32_UART_NUM 4
+#endif
+
+
+#endif /* _MIOS32_CONFIG_H */
/blm_scalar_communication/mios32_config.h
Property changes:
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: blm_scalar_communication/README.txt
===================================================================
--- blm_scalar_communication/README.txt (revision 0)
+++ blm_scalar_communication/README.txt (revision 2263)
@@ -0,0 +1,23 @@
+$Id$
+
+<Application Title>
+===============================================================================
+Copyright (C) <year> <your name> (<your email address>)
+Licensed for personal non-commercial use only.
+All other rights reserved.
+===============================================================================
+
+Required tools:
+ -> http://www.ucapps.de/mio32_c.html
+
+===============================================================================
+
+Required hardware:
+ o MBHP_CORE_STM32 or MBHP_CORE_LPC17 or MBHP_CORE_STM32F4
+ o BLM16x16+X or a virtual BLM
+
+===============================================================================
+
+Demo for the modules/mbhp_scalar_master driver
+
+===============================================================================
/blm_scalar_communication/README.txt
Property changes:
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Index: blm_scalar_communication/Makefile
===================================================================
--- blm_scalar_communication/Makefile (revision 0)
+++ blm_scalar_communication/Makefile (revision 2263)
@@ -0,0 +1,58 @@
+# $Id$
+
+################################################################################
+# following setup taken from environment variables
+################################################################################
+
+PROCESSOR = $(MIOS32_PROCESSOR)
+FAMILY = $(MIOS32_FAMILY)
+BOARD = $(MIOS32_BOARD)
+LCD = $(MIOS32_LCD)
+
+
+################################################################################
+# Source Files, include paths and libraries
+################################################################################
+
+THUMB_SOURCE = app.c
+
+# (following source stubs not relevant for Cortex M3 derivatives)
+THUMB_AS_SOURCE =
+ARM_SOURCE =
+ARM_AS_SOURCE =
+
+C_INCLUDE = -I .
+A_INCLUDE = -I .
+
+LIBS =
+
+
+################################################################################
+# Remaining variables
+################################################################################
+
+LD_FILE = $(MIOS32_PATH)/etc/ld/$(FAMILY)/$(PROCESSOR).ld
+PROJECT = project
+
+DEBUG = -g
+OPTIMIZE = -Os
+
+CFLAGS = $(DEBUG) $(OPTIMIZE)
+
+
+################################################################################
+# Include source modules via additional makefiles
+################################################################################
+
+# sources of programming model
+include $(MIOS32_PATH)/programming_models/traditional/programming_model.mk
+
+# application specific LCD driver (selected via makefile variable)
+include $(MIOS32_PATH)/modules/app_lcd/$(LCD)/app_lcd.mk
+
+# BLM Master Driver
+include $(MIOS32_PATH)/modules/blm_scalar_master/blm_scalar_master.mk
+
+# common make rules
+# Please keep this include statement at the end of this Makefile. Add new modules above.
+include $(MIOS32_PATH)/include/makefile/common.mk
/blm_scalar_communication/Makefile
Property changes:
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property