-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.c
executable file
·191 lines (166 loc) · 3.58 KB
/
alg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
source file for alg mode for the ePaper graphing
calculator project
Author: Ethan Golla
Date: Nov 29
*/
#include "alg.h"
#include "easi.h"
#include "easi_globals.h"
#include "epdgl.h"
#include "AlphaValues.h"
#include "ExpressionTree.h"
#include "voltmeter.h"
#include <stdint.h>
//Globals
ExpressionTree alg_exp;
#define ALG_INPUT_IDX 13
#define MAX_HIST (ALG_INPUT_IDX - 1)
ExpressionTree history[MAX_HIST];
static int history_size = 0;
//Declarations
static void alg_draw_expression(ExpressionTree *exp, int loc_on_screen);
static void alg_draw(void);
void alg_update_history(ExpressionTree *exp);
text_config_t exp_fnt = {&Consolas20, EPD_BLACK};
static bool NEW_EXPRESSION = true;
void alg_init(void){
NEW_EXPRESSION = true;
}
void alg_handle_input(key_t k){
bool update = true;
if (NEW_EXPRESSION) {
alg_clear();
NEW_EXPRESSION = false;
}
switch(k){
case N0:
case N1:
case N2:
case N3:
case N4:
case N5:
case N6:
case N7:
case N8:
case N9:
case DEC:
case SIGN:
case ADD:
case SUB:
case MUL:
case DIV:
case A:
case B:
case C:
case D:
case E:
case F:
case G:
case H:
case I:
case J:
case K:
case L:
case M:
case N:
case O:
case P:
case Q:
case R:
case S:
case T:
case U:
case V:
case W:
case X:
case Y:
case Z:
case PARENTH:
case POW:
case EE:
case ROOT:
case LOG:
case SIN:
case COS:
case TAN:
case BACKSPACE:
case LEFT:
case RIGHT:
ExpressionTree_ModifyExpression(&alg_exp, k);
break;
case ENTER:
if (ExpressionTree_Evaluate(&alg_exp) == 0) {
alg_update_history(&alg_exp);
alg_clear();
}
break;
default:
update = false;
break;
}
if (update) {
alg_draw_expression(&alg_exp, ALG_INPUT_IDX); //New expression always bottommost location
}
}
void alg_clear(void){
ExpressionTree_Clear(&alg_exp);
}
double
alg_get_val()
{
if (history_size == 0) return 0;
return history[history_size-1].result;
}
void
alg_ans()
{
if (NEW_EXPRESSION) {
alg_clear();
NEW_EXPRESSION = false;
}
set_key_alpha_value(A, alg_get_val());
ExpressionTree_ModifyExpression(&alg_exp, A);
alg_draw_expression(&alg_exp, ALG_INPUT_IDX);
}
void
alg_volt()
{
if (NEW_EXPRESSION) {
alg_clear();
NEW_EXPRESSION = false;
}
set_key_alpha_value(V, volt_get_val());
ExpressionTree_ModifyExpression(&alg_exp, V);
alg_draw_expression(&alg_exp, ALG_INPUT_IDX);
}
//loc_on_screen: 0 top, 5 bottom
static void alg_draw_expression(ExpressionTree *exp, int loc_on_screen){
Expression_ToString(&alg_exp);
uint16_t py = 25 + 25 * loc_on_screen;
epdgl_fill_rect(10, py, 290, 25, EPD_WHITE); // clear off area
epdgl_set_cursor(10, py); // set cursor coordinates
epdgl_draw_string(exp->exp_string, &exp_fnt); // draw string
}
static void alg_draw(void){
alg_draw_expression(&alg_exp, ALG_INPUT_IDX);
int hist_idx = history_size - 1;
while(hist_idx >= 0){
alg_draw_expression(&history[hist_idx], hist_idx);
hist_idx--;
}
}
void alg_update_history(ExpressionTree *exp){
Expression_ToString(exp);
if(history_size < MAX_HIST){
history[history_size] = *exp;
history_size++;
}
else{
for(int i = 1; i < MAX_HIST; i++){
history[i - 1] = history[i];
}
history[MAX_HIST-1] = *exp;
}
alg_draw();
}