Improve performance, start of some sound

This commit is contained in:
Jeremy Rand 2012-07-20 15:19:02 -05:00
parent 57440f42d3
commit 2e18c5d14c
3 changed files with 151 additions and 58 deletions

View File

@ -20,7 +20,8 @@ tDigitPos basePos;
tDigitPos selectedOperand; tDigitPos selectedOperand;
static operandChangeNotif operandCallback = NULL; static tOperandDigitChange operandDigitCallback = NULL;
static tSelectedOperandChange selectedOperandCallback = NULL;
void clearDevice(void) void clearDevice(void)
@ -36,11 +37,12 @@ void clearDevice(void)
} }
void initDevice(operandChangeNotif callback) void initDevice(tOperandDigitChange callback1, tSelectedOperandChange callback2)
{ {
tDigitPos pos; tDigitPos pos;
operandCallback = callback; operandDigitCallback = callback1;
selectedOperandCallback = callback2;
clearDevice(); clearDevice();
basePos = 0; basePos = 0;
@ -54,29 +56,37 @@ void initDevice(operandChangeNotif callback)
void incOperandPos(tDigitPos pos) void incOperandPos(tDigitPos pos)
{ {
tDigit oldValue;
if (!IS_VALID_OPERAND_POS(pos)) if (!IS_VALID_OPERAND_POS(pos))
return; return;
if (operand[pos] == 9) if (operand[pos] == 9)
return; return;
oldValue = operand[pos];
operand[pos]++; operand[pos]++;
if (operandCallback != NULL)
operandCallback(pos); if (operandDigitCallback != NULL)
operandDigitCallback(pos, oldValue, operand[pos]);
} }
void decOperandPos(tDigitPos pos) void decOperandPos(tDigitPos pos)
{ {
tDigit oldValue;
if (!IS_VALID_OPERAND_POS(pos)) if (!IS_VALID_OPERAND_POS(pos))
return; return;
if (operand[pos] == 0) if (operand[pos] == 0)
return; return;
oldValue = operand[pos];
operand[pos]--; operand[pos]--;
if (operandCallback != NULL)
operandCallback(pos); if (operandDigitCallback != NULL)
operandDigitCallback(pos, oldValue, operand[pos]);
} }
@ -98,9 +108,9 @@ void shiftOperandPos(bool left)
} }
selectedOperand = newPos; selectedOperand = newPos;
if (operandCallback != NULL) { if (selectedOperandCallback != NULL) {
operandCallback(oldPos); selectedOperandCallback(oldPos);
operandCallback(newPos); selectedOperandCallback(newPos);
} }
} }

View File

@ -40,13 +40,14 @@ extern tDigitPos basePos;
#define IS_VALID_BASE_POS(pos) (((pos) >= BASE_POS_MIN) && ((pos) < BASE_POS_MAX)) #define IS_VALID_BASE_POS(pos) (((pos) >= BASE_POS_MIN) && ((pos) < BASE_POS_MAX))
extern tDigitPos selectedOperand; extern tDigitPos selectedOperand;
#define IS_SELECTED_OPERAND(pos) ((pos) == selectedOperand); #define IS_SELECTED_OPERAND(pos) ((pos) == selectedOperand)
typedef void (*operandChangeNotif)(tDigitPos pos); typedef void (*tOperandDigitChange)(tDigitPos pos, tDigit oldValue, tDigit newValue);
typedef void (*tSelectedOperandChange)(tDigitPos pos);
extern void clearDevice(void); extern void clearDevice(void);
extern void initDevice(operandChangeNotif callback); extern void initDevice(tOperandDigitChange callback1, tSelectedOperandChange callback2);
extern void incOperandPos(tDigitPos pos); extern void incOperandPos(tDigitPos pos);
extern void decOperandPos(tDigitPos pos); extern void decOperandPos(tDigitPos pos);

172
curtaUI.c
View File

@ -69,81 +69,161 @@ typedef int8_t tJoyPos;
#define SLIDER_Y_SPACING (SLIDER_HEIGHT + (2 * SLIDER_Y_OFFSET)) #define SLIDER_Y_SPACING (SLIDER_HEIGHT + (2 * SLIDER_Y_OFFSET))
static void printCounter(void) static char displayBuffer[] =
{ "\n\n"
tDigitPos pos; "Counter: 0 0 0 0 0 0 0 0\n"
" Result: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"
" ^";
printf("Counter: "); #define COUNTER_OFFSET 25
for(pos = NUM_COUNTER_DIGITS - 1; pos >= 0; pos--) { #define RESULT_OFFSET 50
printf(" %d", GET_COUNTER_DIGIT(pos)); #define BASE_OFFSET 103
static void playSound(int freq, int duration)
{
while (duration > 0) {
asm ("STA %w", 0xc030);
while (freq > 0) {
freq--;
}
duration--;
} }
printf("\n");
} }
static void printResult(void) static void updateCounter(void)
{ {
tDigitPos pos; tDigitPos pos;
char *ptr = &(displayBuffer[COUNTER_OFFSET]);
for(pos = NUM_COUNTER_DIGITS - 1; pos >= 0; pos--) {
*ptr = GET_COUNTER_DIGIT(pos) + '0';
ptr+=2;
}
}
static void updateResult(void)
{
tDigitPos pos;
char *ptr = &(displayBuffer[RESULT_OFFSET]);
printf(" Result:");
for(pos = NUM_RESULT_DIGITS - 1; pos >= 0; pos--) { for(pos = NUM_RESULT_DIGITS - 1; pos >= 0; pos--) {
printf(" %d", GET_RESULT_DIGIT(pos)); *ptr = GET_RESULT_DIGIT(pos) + '0';
ptr+=2;
} }
printf("\n ");
for(pos = 0; pos < NUM_RESULT_DIGITS - basePos; pos++) { ptr = &(displayBuffer[BASE_OFFSET]);
printf(" "); for(pos = BASE_POS_MAX - 1; pos >= BASE_POS_MIN; pos--) {
if (pos == basePos) {
*ptr = '^';
} else {
*ptr = ' ';
}
ptr+=2;
} }
printf(" ^\n");
} }
static void printState(void) static void printState(void)
{ {
printCounter(); updateCounter();
printResult(); updateResult();
puts(displayBuffer);
} }
static void drawOperand(tDigitPos pos) static void drawSlider(char xPos, tDigit digit, char color)
{
tgi_setcolor(color);
tgi_bar(xPos + SLIDER_X_OFFSET,
SLIDER_Y_BORDER + SLIDER_Y_OFFSET + (SLIDER_Y_SPACING * digit),
xPos + SLIDER_X_OFFSET + SLIDER_WIDTH,
SLIDER_Y_BORDER + SLIDER_Y_OFFSET + (SLIDER_Y_SPACING * digit) + SLIDER_HEIGHT);
}
static void drawBar(char xPos, tDigit digit, bool isSelected)
{
char barColor = SLIDER_BAR_COLOR;
char sliderColor = SLIDER_COLOR;
if (isSelected) {
barColor = SELECTED_SLIDER_BAR_COLOR;
sliderColor = SELECTED_SLIDER_COLOR;
}
// Draw slider bar
tgi_setcolor(barColor);
tgi_bar(xPos, SLIDER_Y_BORDER, xPos + SLIDER_BAR_WIDTH, SLIDER_Y_BORDER + SLIDER_BAR_HEIGHT);
// Draw slider
drawSlider(xPos, digit, sliderColor);
}
static void drawText(char xPos, tDigit digit)
{ {
char xPos;
char buffer[2]; char buffer[2];
tDigit digit;
if (!IS_VALID_OPERAND_POS(pos)) // Clear old text
return;
xPos = SLIDER_X_BORDER + (SLIDER_BAR_SPACING * (NUM_OPERAND_DIGITS - pos - 1));
digit = GET_OPERAND_DIGIT(pos);
// Clear old bar
tgi_setcolor(COLOR_BLACK); tgi_setcolor(COLOR_BLACK);
tgi_bar(xPos, 0, xPos + SLIDER_BAR_WIDTH, SLIDER_Y_BORDER + SLIDER_BAR_HEIGHT); tgi_bar(xPos, 0, xPos + SLIDER_BAR_WIDTH, SLIDER_Y_BORDER - 1);
// Draw text label // Draw text label
buffer[0] = digit + '0'; buffer[0] = digit + '0';
buffer[1] = '\0'; buffer[1] = '\0';
tgi_setcolor(OPERAND_COLOR); tgi_setcolor(OPERAND_COLOR);
tgi_outtextxy(xPos + OPERAND_OFFSET, 0, buffer); tgi_outtextxy(xPos + OPERAND_OFFSET, 0, buffer);
}
// Draw slider bar
if (selectedOperand == pos) {
tgi_setcolor(SELECTED_SLIDER_BAR_COLOR);
} else {
tgi_setcolor(SLIDER_BAR_COLOR);
}
tgi_bar(xPos, SLIDER_Y_BORDER, xPos + SLIDER_BAR_WIDTH, SLIDER_Y_BORDER + SLIDER_BAR_HEIGHT);
// Draw slider static void changeOperand(tDigitPos pos, tDigit oldValue, tDigit newValue)
if (selectedOperand == pos) { {
tgi_setcolor(SELECTED_SLIDER_COLOR); char xPos;
} else { char barColor = SELECTED_SLIDER_BAR_COLOR;
tgi_setcolor(SLIDER_COLOR); char sliderColor = SELECTED_SLIDER_COLOR;
}
tgi_bar(xPos + SLIDER_X_OFFSET, if (!IS_VALID_OPERAND_POS(pos))
SLIDER_Y_BORDER + SLIDER_Y_OFFSET + (SLIDER_Y_SPACING * digit), return;
xPos + SLIDER_X_OFFSET + SLIDER_WIDTH,
SLIDER_Y_BORDER + SLIDER_Y_OFFSET + (SLIDER_Y_SPACING * digit) + SLIDER_HEIGHT); xPos = SLIDER_X_BORDER + (SLIDER_BAR_SPACING * (NUM_OPERAND_DIGITS - pos - 1));
drawText(xPos, newValue);
drawSlider(xPos, oldValue, SELECTED_SLIDER_BAR_COLOR);
drawSlider(xPos, newValue, SELECTED_SLIDER_COLOR);
}
static void changeSelectedOperand(tDigitPos pos)
{
char xPos;
tDigit digit;
if (!IS_VALID_OPERAND_POS(pos))
return;
digit = GET_OPERAND_DIGIT(pos);
xPos = SLIDER_X_BORDER + (SLIDER_BAR_SPACING * (NUM_OPERAND_DIGITS - pos - 1));
drawBar(xPos, digit, IS_SELECTED_OPERAND(pos));
}
static void drawOperand(tDigitPos pos)
{
char xPos;
tDigit digit;
if (!IS_VALID_OPERAND_POS(pos))
return;
digit = GET_OPERAND_DIGIT(pos);
xPos = SLIDER_X_BORDER + (SLIDER_BAR_SPACING * (NUM_OPERAND_DIGITS - pos - 1));
drawText(xPos, digit);
drawBar(xPos, digit, IS_SELECTED_OPERAND(pos));
} }
@ -318,7 +398,7 @@ void initUI(void)
// Install drivers // Install drivers
tDigitPos pos; tDigitPos pos;
initDevice(drawOperand); initDevice(changeOperand, changeSelectedOperand);
joy_install(&a2e_stdjoy); joy_install(&a2e_stdjoy);
tgi_install(&a2e_hi); tgi_install(&a2e_hi);
@ -357,10 +437,12 @@ bool processNextEvent(void)
break; break;
case ACTION_OPERAND_INC: case ACTION_OPERAND_INC:
playSound(100, 100);
incOperandPos(selectedOperand); incOperandPos(selectedOperand);
break; break;
case ACTION_OPERAND_DEC: case ACTION_OPERAND_DEC:
playSound(100, 100);
decOperandPos(selectedOperand); decOperandPos(selectedOperand);
break; break;