midifunc.h、midifunc.c

/*************************************************/
/* midifunc.h : MIDI functions (C part)          */
/*              for ATtiny10                     */
/*                                               */
/* 2012/01/16 : Created by pcm1723               */
/*************************************************/

#ifndef _MIDIFUNC_H_
#define _MIDIFUNC_H_

#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "m2c_tn10.h"
#include "sw_uart_rx.h"
#include "mididec.h"

extern void midi_note_on(/* ch_prop_t *c_p, */ uint8_t MD_2nd, uint8_t MD_3rd);
extern void midi_note_off(/* ch_prop_t *c_p, */ uint8_t MD_2nd);
extern void pitch_bend(/* ch_prop_t *c_p, int16_t bend */);
extern void calc_pitch(/* ch_prop_t *c_p, unit_prop_t *u_p */ uint8_t gate_flag);
extern uint16_t lb8_to_lin(uint8_t lb);

static inline void GATEQ_ON( void )
{
  unit_prop[0].gate_que |= 0x8000;
} // void GATEQ_ON()

static inline void GATEQ_OFF( void )
{
  unit_prop[0].gate_que &= 0x7fff;
} // void GATEQ_ON()

//extern void all_note_off(/* ch_prop_t *c_p */);
static inline void all_note_off(/* ch_prop_t *c_p */)
{
  GATEQ_OFF();
  unit_prop[0].on_cnt = 0;
  unit_prop[0].gate_que = 0;
}

static inline void GM_reset(void)
{
  ch_prop[0].MD_bend_range = 2; // default bend range = 2 semitone
  ch_prop[0].MD_RPN_MLB    = 0x7f; // invalidate RPN
  all_note_off();
} // void GM_reset()

#endif // #ifndef
/*************************************************/
/* midifunc.c : MIDI functions (C part)          */
/*              for ATtiny10                     */
/*                                               */
/* 2012/01/16 : Created by pcm1723               */
/*************************************************/

#include "sw_uart_rx.h"
#include "mididec.h"
#include "midifunc.h"

#if (0)
void midi_note_on(/* ch_prop_t *c_p, */ uint8_t MD_2nd, uint8_t MD_3rd)
{
//  cli(); // for 16 bit register access
  OCR0A = ((uint16_t)MD_2nd << 2); // 7 bit MIDI note to 9 bit PWM
//  sei(); // leave region
  unit_prop[0].on_cnt++; // increment ON count
   GATEQ_ON();
} // void midi_note_on()
#endif

void midi_note_off(/*ch_prop_t *c_p, */ uint8_t MD_2nd)
{
  if (unit_prop[0].on_cnt) { // do not decrement if already OFF
    unit_prop[0].on_cnt--;   // decrement ON count
  } // if (unit_prop[0].on_cnt) { ...
  if (0 == unit_prop[0].on_cnt) { // avoid GATE OFF if still ON
    GATEQ_OFF();
  } // if (0 == ...
} // void midi_note_off()