-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
179 lines (156 loc) · 3.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// PIC10F222 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config IOSCFS = 8MHZ // Internal Oscillator Frequency Select bit (8 MHz)
#pragma config MCPU = OFF // Master Clear Pull-up Enable bit (Pull-up disabled)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config CP = OFF // Code protection bit (Code protection off)
#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "gpioHandle.h"
void push(uint8_t pushVal);
uint8_t pop(void);
uint8_t pop(void)
{
uint8_t retVal;
if( head%2 ){
switch(head){
case 1:
retVal = (uint8_t)(ringBufLower & 0xf0);
break;
case 3:
retVal = (uint8_t)(ringBufUpper & 0xf0);
break;
}
retVal >>=4;
}else{
switch(head){
case 0:
retVal = (uint8_t)(ringBufLower & 0x0f);
break;
case 2:
retVal = (uint8_t)(ringBufUpper & 0x0f);
break;
}
}
// moving head cursor.
head++;
head %= 4;
return retVal;
}
void push(uint8_t pushVal)
{
if( tail%2 ){
pushVal <<=4;
switch(tail){
case 1:
ringBufLower &= 0x0f;
ringBufLower |= (uint8_t)pushVal;
break;
case 3:
ringBufUpper &= 0x0f;
ringBufUpper |= (uint8_t)pushVal;
break;
}
}else{
switch(tail){
case 0:
ringBufLower &= 0xf0;
ringBufLower |= pushVal;
break;
case 2:
ringBufUpper &= 0xf0;
ringBufUpper |= pushVal;
break;
}
}
// moving tail cursor
tail++;
tail %= 4;
}
void main(void) {
asm("MOVLW 0b00111110");
asm("MOVWF OSCCAL");
/* initial chip configuration */
GPIObits.GP2 = 1; // for preventing unintended MIDI message output.
OPTION = 0b10000010; // PSA=010 for TMR0, then TMR0 counts every 4us.
TRISGPIO = 0b00001011; // <7:4> are not implemented. <3> is actually not cared, because it always work as input port by hardware constraint.
ADCON0 = 0b01000001; // ANS<0> (GPIO0's pin) is used as analog input.
if(GPIObits.GP3 == 0) an0oldVal[1] = 0xff;
else an0oldVal[1] = 0;
// send all-sound-off to the channel where coming CC messages will be.
//send1byte(0xB0|CCCHANNEL);
//send1byte(ALLSOUNDOFFVALUE);
//send1byte(0x0);
head = 0;
tail = 0;
/*
lastGp1 |= 1;
lastGp3 |= 1;
GP1bitHistory = 0xff;
GP3bitHistory = 0xff;
*/
lastGp1 = 0;
lastGp3 = 0;
// Declaring as this, GP1bitHistory, GP3bitHistory are not cleared by 0xff.
// Because even if being in the worst case that booting sequence had 0->1 transition,
// they only sent NOTE-OFF. They will sound nothing.
TMR0 = 0;
/* main loop */
while(1){
if( tail == head ){
collectGpioStat();
TMRcarry();
REMOVECHATTERINGANDFIXCURRENTPEDALSTATUS
TMRcarry();
if( TMR0roundUpper > 252){ // 1-TMR0-round = 1.024ms. 1.024ms*252 ~258ms is fair for active sensing.
send1byte(0xfe);
TMR0roundUpper = 0;
}
}else{
while( tail != head){
switch( pop() ){
case 0x00:
send1byte(GP1ON0);
send1byte(GP1ON1);
send1byte(GP1ON2);
break;
case 0x01:
send1byte(GP1OFF0);
send1byte(GP1OFF1);
send1byte(GP1OFF2);
break;
case 0x02:
send1byte(GP3ON0);
send1byte(GP3ON1);
send1byte(GP3ON2);
break;
case 0x03:
send1byte(GP3OFF0);
send1byte(GP3OFF1);
send1byte(GP3OFF2);
break;
case 0x04:
send1byte(AN0CHANNEL);
send1byte(AN0CCNUM);
send1byte(an0lastSentVal);
break;
}
}
TMR0roundUpper = 0;
TMR0roundLower = 0;
}
CLRWDT();
}
}
void TMRcarry(void){
STATUSbits.CARRY = 0;
TMR0roundLower += TMR0;
TMR0 = 0;
if( STATUSbits.CARRY )TMR0roundUpper++;
}