Subversion Repositories svn.mios

Rev

Rev 163 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
163 tk 1
HEADER 3 MIOS C Example
2
 
3
<H3>Sending 7bit MIDI Events on Rotary Encoder movements</H3>
4
 
5
<P CLASS=INFO>We want to send absolute 7bit values on encoder movements in following format: <I>CC &lt;encoder&gt; &lt;value&gt;</I>.</P>
6
 
7
<P CLASS=INFO>Copy the SDCC skeleton into a new directory, open the main.c file and enhance the hooks like described below. Thereafter type "make" in the command shell, and upload the new project.hex file to the core.</P>
8
 
9
<P CLASS=INFO>At the top of the main.c file, we have to specify an array of "unsigned char" (8bit values) which hold the absolute value:</P>
10
 
11
<TT><PRE>
12
// absolute values are stored in this array
13
unsigned char enc_value[8];
14
</PRE></TT>
15
 
16
<P CLASS=INFO>Within the Init() function, you have to specify, how many shift registers are connected to the core, and which encoder speed mode should be taken:</P>
17
 
18
<TT><PRE>
19
/////////////////////////////////////////////////////////////////////////////
20
// This function is called by MIOS after startup to initialize the
21
// application
22
/////////////////////////////////////////////////////////////////////////////
23
void Init(void) __wparam
24
{
25
  unsigned char i;
26
 
27
  // set shift register update frequency
28
  MIOS_SRIO_UpdateFrqSet(1); // ms
29
 
30
  // we need to set at least one IO shift register pair
31
  MIOS_SRIO_NumberSet(16); // for 128 pins
32
 
33
  // set speed mode for 8 encoders
34
  for(i=0; i<8; ++i) {
35
    // available speed modes: SLOW, NORMAL and FAST
36
    MIOS_ENC_SpeedSet(i, MIOS_ENC_SPEED_FAST, 2); // encoder, speed mode, divider
37
  }
38
}
39
</PRE></TT>
40
 
41
<P CLASS=INFO>We add the code which should be executed on rotary encoder movements to the ENC_NotifyChange() function. It increments/decrements the absolute value depending on the encoder turn direction and sends the new value over the MIDI interface, if it has been changed:</P>
42
 
43
<TT><PRE>
44
/////////////////////////////////////////////////////////////////////////////
45
// This function is called by MIOS when an encoder has been moved
46
// incrementer is positive when encoder has been turned clockwise, else
47
// it is negative
48
/////////////////////////////////////////////////////////////////////////////
49
void ENC_NotifyChange(unsigned char encoder, char incrementer) __wparam
50
{
51
  unsigned char new_value;
52
 
53
  // do nothing if encoder number greater than array size
54
  if( encoder >= sizeof(enc_value) )
55
    return;
56
 
57
  // add incrementer to absolute value, store result in new_value
58
  new_value = enc_value[encoder] + incrementer;
59
 
60
  // branch depending on direction
61
  if( incrementer >= 0 ) {
62
    // overrun check: if new value >= 0x80, force to 0x7f (127)
63
    if( new_value >= 0x80 )
64
      new_value = 0x7f;
65
  } else {
66
    // underrun check: if new value >= 0x80, force to 0x00 (0)
67
    if( new_value >= 0x80 )
68
      new_value = 0x00;
69
  }
70
 
71
  // store and send absolute value if it has been changed
72
  if( new_value != enc_value[encoder] ) {
73
    enc_value[encoder] = new_value;
74
    MIOS_MIDI_TxBufferPut(0xb0);           // CC at MIDI Channel #1
75
    MIOS_MIDI_TxBufferPut(0x10 + encoder); // CC# is 0x10 (16) for first encoder
76
    MIOS_MIDI_TxBufferPut(new_value);
77
  }
78
}
79
</PRE></TT>
80
 
81
 
82
<P CLASS=INFO><B>Note:</B> by using the MIOS_HLP_16bitAddSaturate() help function, this routine can be simplified like shown below:</P>
83
 
84
<TT><PRE>
85
void ENC_NotifyChange(unsigned char encoder, char incrementer) __wparam
86
{
87
  unsigned int value;
88
 
89
  // do nothing if encoder number greater than array size
90
  if( encoder >= sizeof(enc_value) )
91
    return;
92
 
93
  // add incrementer to absolute value by using a MIOS help function
94
  value = (unsigned int)enc_value[encoder];
95
  if( MIOS_HLP_16bitAddSaturate(incrementer, &value, 0x7f) ) {
96
    // store new value and send it
97
    enc_value[encoder] = (unsigned char)value;
98
 
99
    MIOS_MIDI_TxBufferPut(0xb0);           // CC at MIDI Channel #1
100
    MIOS_MIDI_TxBufferPut(0x10 + encoder); // CC# is 0x10 (16) for first encoder
101
    MIOS_MIDI_TxBufferPut((unsigned char)value);
102
  }
103
}
104
</PRE></TT>
105
 
106
 
878 tk 107
<P CLASS=INFO>The pins to which the encoders are connected must be specified in MIOS_ENC_TABLE, which can be placed at the top of your main.c file:</P>
163 tk 108
 
109
<TT><PRE>
878 tk 110
MIOS_ENC_TABLE {
111
             //  sr pin mode
112
  MIOS_ENC_ENTRY( 1, 0, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 1
113
  MIOS_ENC_ENTRY( 1, 2, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 2
114
  MIOS_ENC_ENTRY( 1, 4, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 3
115
  MIOS_ENC_ENTRY( 1, 6, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 4
116
  MIOS_ENC_ENTRY( 2, 0, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 5
117
  MIOS_ENC_ENTRY( 2, 2, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 6
118
  MIOS_ENC_ENTRY( 2, 4, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 7
119
  MIOS_ENC_ENTRY( 2, 6, MIOS_ENC_MODE_NON_DETENTED), // V-Pot 8
120
 
121
  MIOS_ENC_EOT
122
};
163 tk 123
</PRE></TT>
124
 
878 tk 125
<P CLASS=INFO><B>IMPORTANT:</B> the default encoder table has to be disabled in the <B>Makefile</B> via -DDONT_INCLUDE_MIOS_ENC_TABLE. You could add this define to the MIOS_WRAPPER_DEFINES variable:</P>
126
<TT><PRE>
127
MIOS_WRAPPER_DEFINES = -DSTACK_HEAD=0x37f -DSTACK_IRQ_HEAD=0x33f -DDONT_INCLUDE_MIOS_ENC_TABLE
128
</PRE></TT>
129
 
163 tk 130
<P CLASS=INFO>A list of available MIOS function can be found <A HREF="cmios_fun.html">here</A>.</P>
131
 
132
FOOTER