'**************************************************************** '* Name : Bomi_Hub.BAS * '* Author : Godfried-Willem RAES * '* Notice : Copyleft (c) 2010 Logosoft Public Domain * '* Date : 14-Oct-10 * '* Version : 2.0 * '* Notes : Version 1.0 was coded in C by Johannes Taelman * '* : * '**************************************************************** '14.10.2010: First version. '15.10.2010: Minor bugs killed. There still is an issue with the noteoff's on the lites. 'PIC: 18F2525 On MidiHub board Compressor motor, tremulant solenoid, lites ' - X17.2: PIC pin 12 CCP1 -rc2 PWM analog compressor motor speed (0-10V) Ctrl 7 ' - X17.3: PIC pin 13 CCP2 -rc1 PWM power output Softshift solenoid Ctrl 1 ' - X11.2: PIC pin 16 RC5 motor ON/OFF - Ctrl 66 ' - X11.3: PIC pin 15 RC4 optional motor controller reset: CC86 ' - X12.2: PIC pin 21 RB0 lite - map on note 125 on/off + pres YELLOW LED (3W) ' - X12.3: PIC pin 22 RB1 lite - map on note 126 on/off + pres ' - X15.2: PIC pin 14 RC3 lite - map on note 127 on/off + pres ' - X15.3: PIC pin 11 RC0 lite - map on note 0 on/off + pres RED LED (1W) 'NOTE: the PIC should track whether any note is playing or not. ' All notes off should unpower the softshift solenoid and reset controllers 1 and 11. 'Controller 1: steers the main value for the PWM on the softshift magnet. ' Note: 0= fully opened, 127=fully closed!!! 'Controller 7: Motor speed 'Controller 86: reset motor on error. (one shot task) 'Controller 11: steers the periodic modulation frequency around the value set with controller 1. ' Tremulant must be off with controller set to zero. 'controller 12: modulation depth ' cfr. code prototype in GMT Include "18F2525.inc" 'version for the bomi board. 'Include "18F25K20.inc" 'for test & debug on an Amicus board. 'Include "18F4620.inc" 'not applicable to this code/hardware. 'constant definitions: 'initialisations for the midi input parser: Symbol Midichannel = 3 ' Bomi_Channel Symbol NoteOff_Status = 128 + Midichannel ' 2 bytes follow Symbol NoteOn_Status = 144 + Midichannel Symbol Keypres_Status = 160 + Midichannel Symbol Control_Status = 176 + Midichannel Symbol ProgChange_Status = 192 + Midichannel ' 1 byte message Symbol Aftertouch_Status = 208 + Midichannel ' 1 byte follows Symbol Pitchbend_Status = 224 + Midichannel ' lsb msb follow Symbol NrTasks = 6 ' maximum 16 ' Setup the USART DECLARE Hserial_Baud = 31250 ' Set baud rate for the USART to MIDI specs. DECLARE Hserial_TXSTA = 0x24 ' instead of the normal 0x20 - ?? 0x24 DECLARE All_Digital = True ' Declare Hserial_Clear = On ' should clear on errors. Bytes get lost of course... This must be 31250 for MIDI ' Create variables DIM CntHw AS WORD System 'only used in the timer0 interrupt. DIM CntLw AS TMR0L.Word 'this is the trick to read both TMR0L and TMR0H 'it makes Cntlw the low word of cnt DIM Bytein AS BYTE System ' midi byte read from buffer DIM StBit AS Bytein.7 ' highest bit of ByteIn DIM i AS BYTE System ' general purpose counter ' midi variables DIM statusbyte AS BYTE System DIM noteUit AS BYTE System ' note off + release value DIM release AS BYTE System DIM noteAan AS BYTE System ' note on + release value DIM velo AS BYTE System DIM notePres AS BYTE System ' note pressure + pressure value DIM pres AS BYTE System DIM Ctrl AS BYTE System ' continuous controller + value DIM value AS BYTE System DIM prog AS BYTE System ' program change + program-byte DIM aft AS BYTE System ' channel aftertouch DIM pblsb AS BYTE System ' pitch bend lsb DIM pbmsb AS BYTE System ' pitch bend msb DIM veltim AS DWORD System ' 32 bit velo DIM newtim AS DWORD System DIM VelFlags AS WORD System ' bits 0 - 15 used as flags for active timers DIM VelFlags0 AS VelFlags.Byte0 ' alias for bits 0-7 DIM VelFlags1 AS VelFlags.Byte1 ' bits 8-15 - not used in this code. DIM CC1 AS BYTE System 'softshift valve pwm DIM CC7 AS BYTE System 'motor speed pwm DIM CC11 AS BYTE System 'valve modulation speed DIM CC12 AS BYTE System 'valve modulation depth DIM CC66 AS BYTE System ' global on/off switch DIM st AS BYTE System DIM b1 AS BYTE System DIM b2 AS BYTE System DIM Cnt AS DWORD System '32 bit counter DIM Lites AS BYTE System 'bits used as flags DIM CC1min AS BYTE System DIM CC1max AS BYTE System DIM Tremolo AS BYTE System 'only bit 0 is toggled DIM wvar AS WORD System DIM tveltim AS DWORD system 'added by xof for better algorithm for repeats.. '----------------------------------------------------------------------------------------- ' Load the USART Interrupt handler And buffer read subroutines into memory Include "Midi_Irq.inc" ' our own version for UART And Timer0 Interrupt Include "Timers.inc" ' required for velo support with timed pulses and periods. 'Include "DwordArrays.inc" ' support for dword arrays. 'first framework for a multitasker: DIM Task_rsi[NrTasks] AS WORD 'task reschedule interval (period), if 0 the task is not active 'max. value limited to 65535. For longer periods, it will have to 'become dword!!! DIM Velmsb[NrTasks] AS WORD 'the application for velo-timers, is in fact just a one-shot task DIM VelLsb[NrTasks] AS WORD 'DeclareDwordArray(TimeVals , NrTasks) 'alternative using the macro's. [not yet used] ' assigning values syntax: DwordArray Timvals,[i], value ' reading values syntax: value = DwordArray TimVals,[i] ' Mapping defines for midi-events on pin outputs and inputs: ' $define Softshift PORTC.2 'CCP1 - PWM channel 1 ' $define MotorSpeed PORTC.1 'CCP2 - PWM Channel 2 $define MotorSwitch PORTC.5 'ón/off switch for motor $define MotorReset PORTC.4 'reset after failure one shot ' lights: $define Lite125 PORTB.0 $define Lite126 PORTB.1 $define Lite127 PORTC.3 $define Lite0 PORTC.0 'red LED for debug: $define Debug_Led PORTB.5 ' for testing - red led ' configure the input and output pins: TRISA = %01000000 'bits set to 0 are output, 1 = input TRISB = %11100000 TRISC = %11000000 'RC1 en RC2 zijn pwm outputs and must be set to output 'RC6 en RC7 zijn USART I/O and must be set to input 'make sure we initialize those pins on start up: Low Lite0 Low Lite125 Low Lite126 Low Lite127 Low Debug_Led Low MotorSwitch 'motor switch off HPWM 1, 255, PWMminF 'motor speed off - inverted pwm HPWM 2, 0, PWMminF 'klep open Low MotorReset 'ínv CC66 = 0 CC7 = 0 CC1 = 0 CC11 = 0 CC12 = 0 '----------------------------------------------------------------------------------------- ' Main program starts here MAIN: High Debug_Led DelayMS 50 ' wait for stability Low Debug_Led Init_Usart_Interrupt ' Initiate the USART serial buffer interrupt ' this procedure is in the include file Clear_Serial_Buffer ' Clear the serial buffer and reset its pointers ' in the include as well ' following only required for pulse outputs: ' Note that we can only use timer0 since the other timers ' are used by the PWM subsystem ' Configure Timer0 for: ' Clear TMR0L and TMR0H registers ' Interrupt on Timer0 overflow ' 16-bit operation ' Internal clock source 40MHz ' 1:256 Prescaler : thus 40MHz / 256 = 156.250kHz Opentimer0 (Timer_INT_On & T0_16BIT & T0_SOURCE_INT & T0_PS_1_256) ' Setup the High priorities for the interrupts ' start the main program loop: LOOP: ' Create an infinite loop Bytein = HRSIn ' Read data from the serial buffer, with no timeout ' Start the midi parser. Midi_Parse: IF Bytein > Control_Status THEN ' here higher statusses are not implemented. IF Bytein > 253 THEN '254 = midiclock, 255= reset 'midiclock can interrupt all other msg's... '255 had to be intercepted since thats what we 'get when no new byte flows in (?) GOTO Check_Timers 'throw away... ELSE CLEAR statusbyte 'reset the status byte GOTO Check_Timers 'throw away END IF EndIf IF StBit =1 THEN 'should be faster than If Bytein > 127 Then 'status byte received, bit 7 is set CLEAR statusbyte 'if on another channel, the statusbyte needs a reset SELECT Bytein 'eqv to Select case ByteIn CASE NoteOff_Status statusbyte = Bytein noteUit = 255 'reset value. Cannot be 0 !!! release = 255 '0 is a valid midi note! CASE NoteOn_Status statusbyte = Bytein noteAan = 255 velo = 255 CASE Keypres_Status 'used for lights statusbyte = Bytein notePres = 255 pres = 255 CASE Control_Status ' this is the main thing to listen to.... statusbyte = Bytein Ctrl = 255 value = 255 ' Case ProgChange_Status ' statusbyte = Bytein ' prog = 255 ' Case Aftertouch_Status ' statusbyte = Bytein ' aft = 255 ' Case Pitchbend_Status ' statusbyte = Bytein ' pblsb = 255 ' pbmsb = 255 END SELECT ELSE 'midi byte is 7 bits SELECT statusbyte CASE 0 'not a message for this channel GOTO Check_Timers 'disregard CASE NoteOff_Status IF noteUit = 255 THEN noteUit = Bytein ELSE release = Bytein 'message complete, so we can do the action... SELECT noteUit CASE 0 CLEAR Lites.0 CLEAR VelFlags0.0 Low Lite0 CASE 125 CLEAR Lites.1 CLEAR VelFlags0.1 Low Lite125 CASE 126 CLEAR Lites.2 CLEAR VelFlags0.2 Low Lite126 CASE 127 CLEAR Lites.3 CLEAR VelFlags0.3 Low Lite127 END SELECT noteUit = 255 'reset EndIf GOTO Check_Timers CASE NoteOn_Status IF noteAan = 255 THEN noteAan = Bytein ELSE velo = Bytein IF velo = 0 THEN SELECT noteAan CASE 0 Lites.0 = 0 CLEAR VelFlags0.0 Low Lite0 CASE 125 Lites.1 = 0 CLEAR VelFlags0.1 Low Lite125 CASE 126 Lites.2 = 0 CLEAR VelFlags0.2 Low Lite126 CASE 127 Lites.3 = 0 CLEAR VelFlags0.3 Low Lite127 END SELECT noteAan = 255 'reset !!! GOTO Check_Timers 'jump out EndIf 'If CC66.0 = 1 Then 'only if powered up SELECT noteAan CASE 0 SET Lites.0 CLEAR VelFlags0.0 High Lite0 CASE 125 SET Lites.1 CLEAR VelFlags0.1 'so flashing is reset High Lite125 CASE 126 SET Lites.2 CLEAR VelFlags0.2 High Lite126 CASE 127 SET Lites.3 CLEAR VelFlags0.3 High Lite127 END SELECT 'EndIf noteAan = 255 'reset EndIf GOTO Check_Timers CASE Keypres_Status 'used for lite flashing IF notePres = 255 THEN notePres = Bytein ELSE pres = Bytein GOSUB KeyPres EndIf GOTO Check_Timers CASE Control_Status 'this is where the action takes place for controllers IF Ctrl = 255 THEN Ctrl = Bytein ELSE value = Bytein GOSUB Controller EndIf GOTO Check_Timers ' Case ProgChange_Status ' If prog = 255 Then 'single byte message ' prog = Bytein 'weak coding... ' GoSub ProgChange ' EndIf END SELECT EndIf Check_Timers: ' here we check the Task counters and compare them with the 32 bit cnt value ' using the Velflags dword variable: IF VelFlags0 > 0 THEN 'if any bit is set here, there is a timer running IF VelFlags0.0 = 1 THEN 'lite0 pulse veltim.Word1 = Velmsb[0] veltim.Word0 = VelLsb[0] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task0 'lite0 EndIf IF VelFlags0.1 = 1 THEN veltim.Word1 = Velmsb[1] veltim.Word0 = VelLsb[1] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task1 'lite125 EndIf IF VelFlags0.2 = 1 THEN veltim.Word1 = Velmsb[2] veltim.Word0 = VelLsb[2] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task2 'lite126 EndIf IF VelFlags0.3 = 1 THEN veltim.Word1 = Velmsb[3] veltim.Word0 = VelLsb[3] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task3 'lite127 EndIf IF VelFlags0.4 = 1 THEN 'tremulant veltim.Word1 = Velmsb[4] veltim.Word0 = VelLsb[4] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task4 'tremulant EndIf IF VelFlags0.5 = 1 THEN veltim.Word1 = Velmsb[5] veltim.Word0 = VelLsb[5] Cnt.Word1 = CntHw Cnt.Word0 = CntLw 'read counter IF Cnt >= veltim THEN GOSUB Task5 'resetpulse EndIf ELSE IF CntHw > 0xFF THEN CLEAR CntHw EndIf 'end of the main loop GOTO LOOP KeyPres: 'the note to which the pressure should be applied is passed in NotePres, the value in Pres 'here we use it for flashing lights on Bomi SELECT notePres CASE 0 IF Lites.0 = 1 THEN SET VelFlags0.0 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw Task_rsi[0] = (~pres & 127) << 9 veltim = Cnt + Task_rsi[0] 'add the period duration Velmsb[0] = veltim.Word1 VelLsb[0] = veltim.Word0 ELSE CLEAR VelFlags0.0 Low Lite0 EndIf CASE 125 IF Lites.1 = 1 THEN SET VelFlags0.1 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw Task_rsi[1] = (~pres & 127) << 9 veltim = Cnt + Task_rsi[1] 'add the period duration Velmsb[1] = veltim.Word1 VelLsb[1] = veltim.Word0 ELSE CLEAR VelFlags0.1 Low Lite125 EndIf CASE 126 IF Lites.2 = 1 THEN SET VelFlags0.2 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw Task_rsi[2] = (~pres & 127) << 9 veltim = Cnt + Task_rsi[2] 'add the period duration Velmsb[2] = veltim.Word1 VelLsb[2] = veltim.Word0 ELSE CLEAR VelFlags0.2 Low Lite126 EndIf CASE 127 IF Lites.3 = 1 THEN SET VelFlags0.3 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw Task_rsi[3] = (~pres & 127) << 9 veltim = Cnt + Task_rsi[3] 'add the period duration Velmsb[3] = veltim.Word1 VelLsb[3] = veltim.Word0 ELSE CLEAR VelFlags0.3 Low Lite127 EndIf END SELECT notePres = 255 RETURN ProgChange: prog = 255 'this is not realy required RETURN Pitchbend: 'only implemented on dsPIC based robots pblsb = 255 RETURN Aftertouch: 'this is the channel aftertouch, affecting all notes aft = 255 'not mandatory RETURN Controller: SELECT Ctrl CASE 1 'tremulant valve - PWM 'when the valve has no voltage, it is maximum opened. 'with maximum voltage, it is closed. IF value = 0 THEN HPWM 2, 0, PWMminF 'klep volledig open, omhoog, spanningsloos ELSE HPWM 2, value+value,PWMminF EndIf CC1 = value 'CC1min and CC1max have to be recalculated as well now: IF CC12 < CC1 THEN CC1min = CC1 - CC12 ELSE CLEAR CC1min EndIf CC1max = CC1 + CC12 IF CC1max > 127 THEN CC1max = 127 CASE 7 'motor speed PWM - inverted!!! IF value = 0 THEN HPWM 1, 255, PWMminF ELSE HPWM 1, 255 -(value + value), PWMminF EndIf CC7 = value CASE 11 'tremulant frequency CC11 = value IF CC11 > 0 THEN SET VelFlags0.4 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw 'Task_rsi[4] = (~CC11 & 127) << 9 'to be checked. wvar = CC11 << 7 ' = * 128 now range 0 - 16256 Task_rsi[4] = 16384 - wvar 'xof's idea: by resetting the veltim here, we create timing errors. we might be halfway a period already and now prolong it by rescheduling. ' if we'd just omit the following 3 lines, that wouldn't happen and the speed would be reset at the next pulse ' downside is indeed that when we where at a very slow speed then, we wouldn't speed up imediately.. ' ->> ideally we would compare what the old veltime was with what the new one would be and take the smallest of the two values? 'an attempt to code this: ' veltim.Word1 = Velmsb[4] 'get original veltim according to old rsi back ' veltim.Word0 = VelLsb[4] ' tveltim = Cnt + Task_rsi[4] 'this is the one according to new rsi ' veltim = min(veltim, tveltim) 'we take the oe hapening first. next veltim will automatically be scheduled according to the new rsi.. veltim = Cnt + Task_rsi[4] 'add the period duration Velmsb[4] = veltim.Word1 VelLsb[4] = veltim.Word0 ELSE CLEAR VelFlags0.4 'reset valve to value set by CC1: HPWM 2, CC1+CC1,PWMminF EndIf CASE 12 'tremulant modulation depth 'needs to be recalculated if CC1 changes. CC12 = value IF CC12 < CC1 THEN CC1min = CC1 - CC12 ELSE CLEAR CC1min EndIf CC1max = CC1 + CC12 IF CC1max > 127 THEN CC1max = 127 CASE 66 'on/off for the robot IF value = 0 THEN CLEAR CC66.0 High MotorSwitch GOSUB AllNotesOff ELSE Low MotorSwitch SET CC66.0 EndIf CASE 86 'motor reset - one shot pulse 'the value of the controller is irrelevant here. SET VelFlags0.5 Cnt.Word0 = CntLw 'read timer Cnt.Word1 = CntHw Task_rsi[5] = 512 'duration of the pulse veltim = Cnt + Task_rsi[5] 'add the period duration Velmsb[5] = veltim.Word1 VelLsb[5] = veltim.Word0 TOGGLE MotorReset CASE 123 GOSUB AllNotesOff END SELECT Ctrl = 255 'mandatory reset RETURN AllNotesOff: CLEAR VelFlags0 'stop all running timers Low Lite0 Low Lite125 Low Lite126 Low Lite127 CLEAR CC1 CLEAR CC11 CLEAR CC12 HPWM 2, 0, PWMminF 'softshift open 'no change on motor speed!!! 'no motor turn off Low Debug_Led CLEAR Lites RETURN Task0: IF Lites.0 = 0 THEN CLEAR VelFlags0.0 'stop task, as lite is switched off Low Lite0 ELSE 'reload task0 - light SET VelFlags0.0 'can just stay set veltim = Cnt + Task_rsi[0] 'add the period duration Velmsb[0] = veltim.Word1 VelLsb[0] = veltim.Word0 TOGGLE Lite0 EndIf RETURN Task1: IF Lites.1 = 0 THEN CLEAR VelFlags0.1 'stop task, as lite is switched off Low Lite125 ELSE 'reload task1 light: SET VelFlags0.1 'can just stay set veltim = Cnt + Task_rsi[1] 'add the period duration in Task_rsi[1] Velmsb[1] = veltim.Word1 VelLsb[1] = veltim.Word0 TOGGLE Lite125 EndIf RETURN Task2: IF Lites.2 = 0 THEN CLEAR VelFlags0.2 'stop task, as lite is switched off Low Lite126 ELSE 'reload task2 - light SET VelFlags0.2 'can just stay set veltim = Cnt + Task_rsi[2] 'add the period duration Velmsb[2] = veltim.Word1 VelLsb[2] = veltim.Word0 TOGGLE Lite126 EndIf RETURN Task3: IF Lites.3 = 0 THEN CLEAR VelFlags0.3 'stop task, as lite is switched off Low Lite127 ELSE 'reload task3 - light SET VelFlags0.3 'can just stay set veltim = Cnt + Task_rsi[3] 'add the period duration Velmsb[3] = veltim.Word1 VelLsb[3] = veltim.Word0 TOGGLE Lite127 EndIf RETURN Task4: IF CC11 = 0 THEN CLEAR VelFlags0.4 'stop task, as tremulant is switched off 'reset the valve to the position determined by CC1 HPWM 2,CC1+CC1,PWMminF ELSE 'reload task4 - tremulant SET VelFlags0.4 'can just stay set veltim = Cnt + Task_rsi[4] 'add the period duration Velmsb[4] = veltim.Word1 VelLsb[4] = veltim.Word0 IF Tremolo.0 = 1 THEN HPWM 2, CC1min+CC1min,PWMminF CLEAR Tremolo.0 ELSE HPWM 2, CC1max+CC1max,PWMminF SET Tremolo.0 EndIf EndIf RETURN Task5: 'one shot task for reset pulse motor controller CLEAR VelFlags0.5 'stop task TOGGLE MotorReset RETURN '[EOF]