#include <stdio.h>
#include <string.h>
#define DELAY_TIME 200
#define delay_time 10
#define ZERO 0x01
#define ONE 0x02
#define TWO 0x04
#define THREE 0x08
#define FOUR 0x10
#define FIVE 0x20
#define SIX 0x40
#define SEVEN 0x80
#define EIGHT 0x01
#define NINE 0x02
#define error 0xae
#define DISNUM0 0x30
#define DISNUM1 0x31
#define DISNUM2 0x32
#define DISNUM3 0x33
#define DISNUM4 0x34
#define DISNUM5 0x35
#define DISNUM6 0x36
#define DISNUM7 0x37
#define DISNUM8 0x38
#define DISNUM9 0x39
#define DISNUMA 0x2b
#define DISNUMB 0x2d
#define DISNUMC 0X01
#define DISNUMD 0x25
#define DISNUMM 0x2a
#define DISNUMH 0x01
#define DELAY 1000000
volatile char keypad_array[4][8] = {
{DISNUM1, DISNUM2, error, DISNUM3, error, error, error, DISNUMA},
{DISNUM4, DISNUM5, error, DISNUM6, error, error, error, DISNUMB},
{DISNUM7, DISNUM8, error, DISNUM9, error, error, error, DISNUMC},
{DISNUMM, DISNUM0, error, DISNUMH, error, error, error, DISNUMD}};
volatile int keypad_num[4][8] = {
{'1', '2', '0', '3', '0', '0', '0', '+'},
{'4', '5', '0', '6', '0', '0', '0', '-'},
{'7', '8', '0', '9', '0', '0', '0', ':'},
{'*', '0', '0', '=', '0', '0', '0', '/'}
};
void lcd_cmd(unsigned char ch);
void lcd_data(unsigned char ch);
void lcd_string_print(unsigned char *ch);
void lcd_string_leftrotate(unsigned char *str);
void lcd_string_rightrotate(unsigned char *str);
void travel_display(unsigned char till);
void keypad_input_calculation();
void calculation(unsigned char *str);
void setup() {
volatile unsigned char *dirl, *dirk, *dirf, *outf, *outl, *outk, *dirc, *inc;
dirf = (volatile unsigned char *)0x30; *dirf = 0xFF;
dirk = (volatile unsigned char *)0x107; *dirk = 0xFF;
dirl = (volatile unsigned char *)0x10A; *dirl = 0xFF;
dirc = (volatile unsigned char *)0x27; *dirc = 0x00;
outf = (volatile unsigned char *)0x31;
outk = (volatile unsigned char *)0x108;
outl = (volatile unsigned char *)0x10b;
inc = (volatile unsigned char *)0x26;
}
void loop()
{
lcd_cmd(0x0c);
lcd_cmd(0x80);
keypad_input_calculation();
}
void intToString(int number, char *str)
{
sprintf(str, "%d", number);//direct function to
}
void lcd_string_leftrotate(unsigned char *str)
{
int len = strlen((char *)str);
if (len == 0) return;
char temp = str[0];
for (int i = 0; i < len - 1; i++) {
str[i] = str[i + 1];
}
str[len - 1] = temp;
}
void lcd_string_rightrotate(unsigned char *str)
{
int len = strlen((char *)str);
if (len == 0) return;
char temp = str[len - 1];
for (int i = len - 2; i >= 0; i--) {
str[i + 1] = str[i];
}
str[0] = temp;
}
void lcd_cmd(unsigned char ch)
{
volatile unsigned char *outf = (volatile unsigned char *)0x31;
volatile unsigned char *outk = (volatile unsigned char *)0x108;
*outk = ch;
*outf = 0x02;
delay(delay_time);
*outf = 0x00;
}
void lcd_data(unsigned char ch)
{
volatile unsigned char *outf = (volatile unsigned char *)0x31;
volatile unsigned char *outk = (volatile unsigned char *)0x108;
*outk = ch;
*outf = 0x03;
delay(delay_time);
*outf = 0x01;
}
void lcd_string_print(unsigned char *str)
{
for (unsigned int i = 0; str[i] != '\0'; i++) {
lcd_data(str[i]);
}
}
void lcd_string_print_till(unsigned char *str, unsigned int j)
{
for (unsigned int i = 0; i < j; i++) {
lcd_data(str[i]);
}
}
void travel_display(unsigned char till)
{
for (int i = 0; i < till; i++) {
lcd_data(' ');
}
}
void keypad_input_calculation()
{
volatile unsigned char *outk, *inc, *outl;
inc = (unsigned char *)0x26;
outl = (unsigned char *)0x10b;
outk = (volatile unsigned char *)0x108;
int j = 0;
volatile char buffer[100];
while (1) {
for (volatile long i = 0; i < 4; i++) {
*outl = 1 << i;
volatile unsigned char y = *inc;
if (y != 0) {
if (i == 2 && y == 8) {
lcd_cmd(0x01);
memset(buffer, 0, sizeof(buffer));
j=0;
}
else if (i == 3 && y == 4) {
lcd_cmd(0xc0);
calculation(buffer);
}
else {
*outk = keypad_array[i][y - 1];
if (keypad_num[i][y - 1] != ':' && keypad_num[i][y - 1] != '=') {
buffer[j] = keypad_num[i][y - 1];
j++;
}
lcd_data(*outk);
delay(DELAY_TIME);
}
}
}
}
}
void calculation(unsigned char *str)
{
unsigned char t_strf[100], t_str1[100], t_str2[100];
unsigned char temp_opr;
long num1, num2, ans;
long i = 0, j = 0;
while (str[i] != '+' && str[i] != '*' && str[i] != '-' && str[i] != '/' && str[i] != '\0') {
t_str1[i] = str[i];
i++;
}
t_str1[i] = '\0';
temp_opr = str[i];
i++;
while (str[i] != '\0') {
t_str2[j] = str[i];
i++;
j++;
}
t_str2[j] = '\0';
num1 = atoi((char *)t_str1);
num2 = atoi((char *)t_str2);
switch (temp_opr) {
case '+':
ans = num1 + num2;
break;
case '-':
ans = num1 - num2;
break;
case '*':
ans = num1 * num2;
break;
case '/':
ans = num1 / num2;
break;
default:
ans = 0;
break;
}
intToString(ans, (char *)t_strf);
lcd_cmd(0xc0);
// travel_display(4);
lcd_string_print(t_strf);
}