From 2e18c5d14c515e72ec63a4521b1229f921e468d5 Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Fri, 20 Jul 2012 15:19:02 -0500 Subject: [PATCH] Improve performance, start of some sound --- curtaModel.c | 30 ++++++--- curtaModel.h | 7 ++- curtaUI.c | 172 +++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 151 insertions(+), 58 deletions(-) diff --git a/curtaModel.c b/curtaModel.c index a246045..834d278 100644 --- a/curtaModel.c +++ b/curtaModel.c @@ -20,7 +20,8 @@ tDigitPos basePos; tDigitPos selectedOperand; -static operandChangeNotif operandCallback = NULL; +static tOperandDigitChange operandDigitCallback = NULL; +static tSelectedOperandChange selectedOperandCallback = NULL; void clearDevice(void) @@ -36,11 +37,12 @@ void clearDevice(void) } -void initDevice(operandChangeNotif callback) +void initDevice(tOperandDigitChange callback1, tSelectedOperandChange callback2) { tDigitPos pos; - operandCallback = callback; + operandDigitCallback = callback1; + selectedOperandCallback = callback2; clearDevice(); basePos = 0; @@ -54,29 +56,37 @@ void initDevice(operandChangeNotif callback) void incOperandPos(tDigitPos pos) { + tDigit oldValue; + if (!IS_VALID_OPERAND_POS(pos)) return; if (operand[pos] == 9) return; + oldValue = operand[pos]; operand[pos]++; - if (operandCallback != NULL) - operandCallback(pos); + + if (operandDigitCallback != NULL) + operandDigitCallback(pos, oldValue, operand[pos]); } void decOperandPos(tDigitPos pos) { + tDigit oldValue; + if (!IS_VALID_OPERAND_POS(pos)) return; if (operand[pos] == 0) return; + oldValue = 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; - if (operandCallback != NULL) { - operandCallback(oldPos); - operandCallback(newPos); + if (selectedOperandCallback != NULL) { + selectedOperandCallback(oldPos); + selectedOperandCallback(newPos); } } diff --git a/curtaModel.h b/curtaModel.h index 2e55823..ddc5a79 100644 --- a/curtaModel.h +++ b/curtaModel.h @@ -40,13 +40,14 @@ extern tDigitPos basePos; #define IS_VALID_BASE_POS(pos) (((pos) >= BASE_POS_MIN) && ((pos) < BASE_POS_MAX)) 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 initDevice(operandChangeNotif callback); +extern void initDevice(tOperandDigitChange callback1, tSelectedOperandChange callback2); extern void incOperandPos(tDigitPos pos); extern void decOperandPos(tDigitPos pos); diff --git a/curtaUI.c b/curtaUI.c index 934144b..5a5b851 100644 --- a/curtaUI.c +++ b/curtaUI.c @@ -69,81 +69,161 @@ typedef int8_t tJoyPos; #define SLIDER_Y_SPACING (SLIDER_HEIGHT + (2 * SLIDER_Y_OFFSET)) -static void printCounter(void) -{ - tDigitPos pos; +static char displayBuffer[] = +"\n\n" +"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: "); - for(pos = NUM_COUNTER_DIGITS - 1; pos >= 0; pos--) { - printf(" %d", GET_COUNTER_DIGIT(pos)); +#define COUNTER_OFFSET 25 +#define RESULT_OFFSET 50 +#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; + 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--) { - 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++) { - printf(" "); + + ptr = &(displayBuffer[BASE_OFFSET]); + 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) { - printCounter(); - printResult(); + updateCounter(); + 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]; - tDigit digit; - if (!IS_VALID_OPERAND_POS(pos)) - return; - - xPos = SLIDER_X_BORDER + (SLIDER_BAR_SPACING * (NUM_OPERAND_DIGITS - pos - 1)); - digit = GET_OPERAND_DIGIT(pos); - - // Clear old bar + // Clear old text 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 buffer[0] = digit + '0'; buffer[1] = '\0'; tgi_setcolor(OPERAND_COLOR); 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 - if (selectedOperand == pos) { - tgi_setcolor(SELECTED_SLIDER_COLOR); - } else { - tgi_setcolor(SLIDER_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 changeOperand(tDigitPos pos, tDigit oldValue, tDigit newValue) +{ + char xPos; + char barColor = SELECTED_SLIDER_BAR_COLOR; + char sliderColor = SELECTED_SLIDER_COLOR; + + if (!IS_VALID_OPERAND_POS(pos)) + return; + + 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 tDigitPos pos; - initDevice(drawOperand); + initDevice(changeOperand, changeSelectedOperand); joy_install(&a2e_stdjoy); tgi_install(&a2e_hi); @@ -357,10 +437,12 @@ bool processNextEvent(void) break; case ACTION_OPERAND_INC: + playSound(100, 100); incOperandPos(selectedOperand); break; case ACTION_OPERAND_DEC: + playSound(100, 100); decOperandPos(selectedOperand); break;