This commit is contained in:
sherlock 2025-10-29 12:25:39 +05:30
parent 085c1023a6
commit 6a914a6eaa

View file

@ -94,9 +94,20 @@ int result = 0;
unsigned int current_base = MODE_DEC; unsigned int current_base = MODE_DEC;
unsigned int operation = OP_NONE; unsigned int operation = OP_NONE;
unsigned char result_displayed = 0; unsigned char result_displayed = 0;
unsigned int key = 0xFF;
unsigned int last_key = 0xFF;
unsigned int stable = 0;
unsigned int mode_held = 0;
unsigned int mode_press_counter = 0;
unsigned int key_pressed_with_mode = 0;
unsigned int mode_current = 0;
int is_operator_key = 0;
int operator_mode = 0;
unsigned int seg_pattern = 0;
void delay(volatile unsigned int d) { void delay(volatile unsigned int d) {
while(d--) __NOP(); while(d--)
__NOP();
} }
void lcd_delay(unsigned long r) { void lcd_delay(unsigned long r) {
@ -213,21 +224,22 @@ void display_status(void){
if(operation != OP_NONE) { if(operation != OP_NONE) {
// Show operation pending // Show operation pending
lcd_print_str("Op:"); lcd_print_str("Op:");
if(operation == OP_ADD) lcd_data('+'); if(operation == OP_ADD)
else if(operation == OP_SUB) lcd_data('-'); lcd_data('+');
else if(operation == OP_MUL) lcd_data('*'); else if(operation == OP_SUB)
lcd_data('-');
else if(operation == OP_MUL)
lcd_data('*');
lcd_print_str(" Val:"); lcd_print_str(" Val:");
lcd_print_num(stored_num, current_base); lcd_print_num(stored_num, current_base);
lcd_print_str(" "); lcd_print_str(" ");
} } else if(result_displayed) {
else if(result_displayed){
// Show result // Show result
lcd_print_str("Res: "); lcd_print_str("Res: ");
lcd_print_num(result, current_base); lcd_print_num(result, current_base);
lcd_print_str(" "); lcd_print_str(" ");
} } else {
else {
// Show current input // Show current input
lcd_print_str("Inp: "); lcd_print_str("Inp: ");
lcd_print_num(input_num, current_base); lcd_print_num(input_num, current_base);
@ -264,10 +276,14 @@ unsigned int is_mode_button_pressed(void){
} }
unsigned int is_valid_digit(unsigned int key) { unsigned int is_valid_digit(unsigned int key) {
if(key >= 16) return 0; if(key >= 16)
if(current_base == MODE_BIN && key >= 2) return 0; return 0;
if(current_base == MODE_OCT && key >= 8) return 0; if(current_base == MODE_BIN && key >= 2)
if(current_base == MODE_DEC && key >= 10) return 0; return 0;
if(current_base == MODE_OCT && key >= 8)
return 0;
if(current_base == MODE_DEC && key >= 10)
return 0;
return 1; return 1;
} }
@ -292,12 +308,6 @@ void change_mode(void){
} }
int main(void) { int main(void) {
unsigned int key, last_key = 0xFF;
unsigned int stable = 0;
unsigned int mode_held = 0;
unsigned int mode_press_counter = 0;
unsigned int key_pressed_with_mode = 0;
// Configure pins // Configure pins
LPC_PINCON->PINSEL0 = 0; LPC_PINCON->PINSEL0 = 0;
LPC_PINCON->PINSEL1 = 0; LPC_PINCON->PINSEL1 = 0;
@ -325,14 +335,15 @@ int main(void){
for(;;) { for(;;) {
// Check if MODE button is currently pressed // Check if MODE button is currently pressed
unsigned int mode_current = is_mode_button_pressed(); mode_current = is_mode_button_pressed();
// Scan keypad // Scan keypad
key = scan_keypad(); key = scan_keypad();
// Debounce keypad // Debounce keypad
if(key == last_key) { if(key == last_key) {
if(stable < 5) stable++; if(stable < 5)
stable++;
} else { } else {
last_key = key; last_key = key;
stable = 0; stable = 0;
@ -344,7 +355,8 @@ int main(void){
mode_held = 1; mode_held = 1;
} else { } else {
// MODE button released // MODE button released
if(mode_held && !key_pressed_with_mode && mode_press_counter < 50){ if(mode_held && !key_pressed_with_mode &&
mode_press_counter < 50) {
// Short press without key combo = change mode // Short press without key combo = change mode
change_mode(); change_mode();
} }
@ -358,11 +370,11 @@ int main(void){
// Key pressed and stable // Key pressed and stable
// Check if this is an operator key (A-E) // Check if this is an operator key (A-E)
int is_operator_key = (key >= 10 && key <= 14); is_operator_key = (key >= 10 && key <= 14);
// In HEX mode, operators require MODE button // In HEX mode, operators require MODE button
// In other modes, operator keys work directly // In other modes, operator keys work directly
int operator_mode = 0; operator_mode = 0;
if(current_base == MODE_HEX) { if(current_base == MODE_HEX) {
operator_mode = (mode_held && is_operator_key); operator_mode = (mode_held && is_operator_key);
} else { } else {
@ -379,22 +391,19 @@ int main(void){
input_num = 0; input_num = 0;
result_displayed = 0; result_displayed = 0;
display_status(); display_status();
} } else if(key == 11) { // B = Subtraction
else if(key == 11){ // B = Subtraction
stored_num = input_num; stored_num = input_num;
operation = OP_SUB; operation = OP_SUB;
input_num = 0; input_num = 0;
result_displayed = 0; result_displayed = 0;
display_status(); display_status();
} } else if(key == 12) { // C = Multiplication
else if(key == 12){ // C = Multiplication
stored_num = input_num; stored_num = input_num;
operation = OP_MUL; operation = OP_MUL;
input_num = 0; input_num = 0;
result_displayed = 0; result_displayed = 0;
display_status(); display_status();
} } else if(key == 13) { // D = Equals
else if(key == 13){ // D = Equals
if(operation == OP_ADD) if(operation == OP_ADD)
result = stored_num + input_num; result = stored_num + input_num;
else if(operation == OP_SUB) else if(operation == OP_SUB)
@ -408,8 +417,7 @@ int main(void){
operation = OP_NONE; operation = OP_NONE;
result_displayed = 1; result_displayed = 1;
display_status(); display_status();
} } else if(key == 14) { // E = Clear
else if(key == 14){ // E = Clear
input_num = 0; input_num = 0;
stored_num = 0; stored_num = 0;
operation = OP_NONE; operation = OP_NONE;
@ -417,14 +425,15 @@ int main(void){
result_displayed = 0; result_displayed = 0;
display_status(); display_status();
} }
} } else {
else {
// DIGIT INPUT MODE // DIGIT INPUT MODE
if(is_valid_digit(key)) { if(is_valid_digit(key)) {
result_displayed = 0; result_displayed = 0;
input_num = input_num * current_base + key; input_num = input_num * current_base + key;
if(input_num > 32767) input_num = input_num % 32768; if(input_num > 32767)
if(input_num < -32768) input_num = -32768; input_num = input_num % 32768;
if(input_num < -32768)
input_num = -32768;
display_status(); display_status();
} }
} }
@ -433,18 +442,14 @@ int main(void){
} }
// ===== STATE MACHINE DISPLAY ON 7-SEGMENT ===== // ===== STATE MACHINE DISPLAY ON 7-SEGMENT =====
unsigned int seg_pattern;
if(operation == OP_NONE && result_displayed == 0) { if(operation == OP_NONE && result_displayed == 0) {
seg_pattern = 0x06; // Display '1' seg_pattern = 0x06; // Display '1'
} } else if(operation != OP_NONE) {
else if(operation != OP_NONE) {
seg_pattern = 0x5B | 0x80; // Display '2' with DP seg_pattern = 0x5B | 0x80; // Display '2' with DP
} } else if(result_displayed == 1) {
else if(result_displayed == 1) {
seg_pattern = 0x4F | 0x80; // Display '3' with DP seg_pattern = 0x4F | 0x80; // Display '3' with DP
} } else {
else {
seg_pattern = 0x3F; // Display '0' seg_pattern = 0x3F; // Display '0'
} }
@ -455,4 +460,6 @@ int main(void){
delay(3000); delay(3000);
} }
return 0;
} }