Some refactoring; no changes in fundamental functionality.
This commit is contained in:
parent
1a1910ea45
commit
8c65cbcb4e
|
@ -0,0 +1 @@
|
||||||
|
*asm
|
3659
playback/main.asm
3659
playback/main.asm
File diff suppressed because it is too large
Load Diff
|
@ -23,13 +23,18 @@
|
||||||
|
|
||||||
// CSINT should be 256^2 / 10 000 = 65 536 / 10 000 = 6
|
// CSINT should be 256^2 / 10 000 = 65 536 / 10 000 = 6
|
||||||
// USINT should be 60 000
|
// USINT should be 60 000
|
||||||
#define CSUS 10000 // CSUS microseconds (us) in a centisecond (cs)
|
#define CSUS 10000 // microseconds (us) in a centisecond (cs)
|
||||||
#define CSINT 6 // centiseconds storable in int of microseconds
|
#define CSINT 6 // centiseconds storable in int of microseconds
|
||||||
#define USINT 60000 // CSINT in microseconds
|
#define USINT 60000 // CSINT in microseconds
|
||||||
|
|
||||||
|
#define DELAY 1000 // input sample delay in microseconds
|
||||||
#define MAXNOTES 64 // record at most 64 notes (arbitrary)
|
#define MAXNOTES 64 // record at most 64 notes (arbitrary)
|
||||||
|
|
||||||
|
unsigned int rec[MAXNOTES]; // notes recorded
|
||||||
|
unsigned int len[MAXNOTES]; // length of each note in centiseconds
|
||||||
|
|
||||||
volatile bool recording = true;
|
volatile bool recording = true;
|
||||||
|
volatile unsigned int notes = 1;
|
||||||
volatile unsigned int us = 0;
|
volatile unsigned int us = 0;
|
||||||
volatile unsigned int cs = 0;
|
volatile unsigned int cs = 0;
|
||||||
|
|
||||||
|
@ -46,33 +51,35 @@ void play(unsigned int note) {
|
||||||
CCR1 = note == n ? 0 : 100;
|
CCR1 = note == n ? 0 : 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sing(unsigned int* song, unsigned int* lengths, unsigned int length) {
|
void playback() {
|
||||||
for (int i = 0; i < length; i++) {
|
for (unsigned int i = 0; i < notes; i++) {
|
||||||
play(song[i]);
|
play(rec[i]);
|
||||||
for (int j = 0; j < lengths[i]; j++) {
|
for (unsigned int j = 0; j < len[i]; j++) {
|
||||||
__delay_cycles(CSUS);
|
__delay_cycles(CSUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int record(unsigned int* song, unsigned int* lengths) {
|
void save_length(unsigned int position) {
|
||||||
unsigned int pos = 1;
|
len[position] = cs + (us + TAR)/CSUS;
|
||||||
|
cs = us = TAR = 0; // reset all counts
|
||||||
|
}
|
||||||
|
|
||||||
|
void record() {
|
||||||
unsigned int note;
|
unsigned int note;
|
||||||
TAR = 0;
|
TAR = 0;
|
||||||
P1OUT |= LED1;
|
P1OUT |= LED1;
|
||||||
while (recording) {
|
while (recording && notes < MAXNOTES) {
|
||||||
note = whole[P2IN & 0xF];
|
note = whole[P2IN & 0xF]; // depends on how input works
|
||||||
if (note != song[pos - 1]) {
|
if (note != rec[notes - 1]) {
|
||||||
lengths[pos - 1] = cs + (us + TAR)/CSUS;
|
save_length(notes - 1);
|
||||||
song[pos] = note;
|
rec[notes] = note;
|
||||||
play(note);
|
play(note);
|
||||||
cs = us = TAR = 0;
|
notes++;
|
||||||
pos++;
|
|
||||||
}
|
}
|
||||||
__delay_cycles(0x200);
|
__delay_cycles(DELAY);
|
||||||
}
|
}
|
||||||
P1OUT &= ~LED1;
|
P1OUT &= ~LED1;
|
||||||
return pos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
|
@ -89,11 +96,8 @@ void main(void) {
|
||||||
TACCTL0 = CCIE; // enable clock interrupt
|
TACCTL0 = CCIE; // enable clock interrupt
|
||||||
|
|
||||||
__enable_interrupt();
|
__enable_interrupt();
|
||||||
|
record();
|
||||||
unsigned int rec[MAXNOTES];
|
playback();
|
||||||
unsigned int len[MAXNOTES];
|
|
||||||
unsigned int length = record(rec, len);
|
|
||||||
sing(rec, len, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__TI_COMPILER_VERSION__)
|
#if defined(__TI_COMPILER_VERSION__)
|
||||||
|
@ -105,10 +109,10 @@ __interrupt void timer0_a0_isr(void)
|
||||||
{
|
{
|
||||||
unsigned int us_left = USINT - us; // count up to USINT microseconds in us
|
unsigned int us_left = USINT - us; // count up to USINT microseconds in us
|
||||||
if (us_left < TACCR0) {
|
if (us_left < TACCR0) {
|
||||||
cs += CSINT; // add centiseconds to count
|
cs += CSINT; // add centiseconds to cs count
|
||||||
us = TACCR0 - us_left; // save overflow
|
us = TACCR0 - us_left; // save overflow
|
||||||
} else {
|
} else {
|
||||||
us += TACCR0;
|
us += TACCR0; // add microseconds to us count
|
||||||
}
|
}
|
||||||
TACCTL0 &= ~CCIFG; // set interrupt flag to 0
|
TACCTL0 &= ~CCIFG; // set interrupt flag to 0
|
||||||
}
|
}
|
||||||
|
@ -120,8 +124,9 @@ __interrupt void port1_isr(void)
|
||||||
void __attribute__ ((interrupt(PORT1_VECTOR))) port1_isr (void)
|
void __attribute__ ((interrupt(PORT1_VECTOR))) port1_isr (void)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
recording = false; // stop recording
|
save_length(notes); // save length of current note
|
||||||
P1OUT ^= BIT0;
|
TACCTL0 &= ~CCIE; // disable interrupts for clock
|
||||||
P1IFG &= ~BIT3; // set interrupt flag to 0
|
|
||||||
P1IE &= ~BIT3; // disable interrupts for button
|
P1IE &= ~BIT3; // disable interrupts for button
|
||||||
|
P1IFG &= ~BIT3; // set interrupt flag to 0
|
||||||
|
recording = false; // stop recording
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue