-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec.ino
232 lines (209 loc) · 4.71 KB
/
ec.ino
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <Arduino.h>
#include "ec_conf.h"
#include <Float64.h>
#include <Math64.h>
#if defined(ARDUINO)
#include <avr/sleep.h>
#endif
#include "softkey.h"
#include "display.h"
#include "rpn.h"
Softkey skey;
display lcd;
//const byte pin_BL=10;
rpn sysrpn;
//Sleep sleep;
const int8_t rpin[7]={KEY_R0, KEY_R1, KEY_R2, KEY_R3, KEY_R4, KEY_R5, KEY_R6};
const int8_t cpin[]={KEY_C0,KEY_C1,KEY_C2,KEY_C3,KEY_C4};
static const char keyval[]="\
abcde\
fghij\
\r\r_E\010\
?789/\
S456*\
R123-\
!0.X+";
#if defined(TEST_SMALL) || !defined(ARDUINO)
static const char keychar[]="\
abcde\
fghij\
.....\
.klmn\
.opqr\
.stuv\
.wxyz\
";
#else
static const char *keychar=NULL;
#endif
int16_t sleeptimer=0;
bool sleep_req=false;
bool stop_req=false;
void gosleep();
void setup (void)
{
uint8_t eex_char[]={0,0xf,0x8,0xf,0x8,0xf,0,0};
#if defined(TEST_SMALL) || !defined(ARDUINO) //need serial port for testing
Serial.begin (115200);
#endif
lcd.clear();
lcd.createChar(EXPCHAR,eex_char);
lcd.blink();
sysrpn.begin(lcd);
pinMode(BACKLIGHT_PIN, OUTPUT);
//digitalWrite(pin_BL, HIGH);
analogWrite(BACKLIGHT_PIN, 100);
for(int i=0;i<7;i++){
pinMode(rpin[i], INPUT_PULLUP);
}
delay(100);
scankeys(); //prevent spurious startup keys
waitRelease(30); //ensure ON/OFF button is released
sleeptimer=0;
/*
lcd.print("Hello world!");
Serial.print("Math64::pi = ");
Serial.println(pi);
Serial.print("rootThree = ");
Serial.println(sqrt64(3.0));
Serial.print("sin: ");
Serial.println(sin64(atof64("1.2")));
Serial.print("atan: ");
Serial.println(atan64(0.33));
Serial.print("log: ");
Serial.println(log64(6.0));
Serial.print("exp: ");
Serial.println(exp64(5.2));
Serial.print("sqrt: ");
Serial.println(sqrt64(2.0));
for(int i=0;i<16;i++){
f64 d1 = pi;
d1.setDecs(i);
Serial.println(pi);
}
Serial.println(atof64("1e42"));
Serial.println(atof64("-5.7e4"));
Serial.println(atof64("5.7e-4"));
Serial.println(atof64("5.7e48"));
Serial.println(atof64("5.7e-48"));
//*/
} // end of setup
int8_t scankeys(void)
{
int8_t r,c;
bool keydown=false;
int8_t key=NO_KEY;
//lcd.setPinMode(INPUT); //columns go high Z
//delay(10);
for(c=0;c<5;c++){
digitalWrite(cpin[c],HIGH);
}
for(c=0;c<5;c++){
//pinMode(cpin[c],OUTPUT);
digitalWrite(cpin[c],LOW); //drive columns low one at a time
for(r=0;r<7;r++){
if(digitalRead(rpin[r]) == LOW){ //scan rows
keydown=true;
key=c+r*5;
goto xit; //return first key found
}
}
//pinMode(cpin[c],INPUT); //high Z
digitalWrite(cpin[c],HIGH);
}
xit:
//lcd.setPinMode(OUTPUT);//for lcd
return key;
}
//stop scanning while waiting for key release
void waitRelease(int key)
{
int c=key%5;
int r=key/5;
if(key<0)return;
for(int i=0;i<5;i++){
digitalWrite(cpin[i],i==c?LOW:HIGH);
}
while(digitalRead(rpin[r])==LOW)delay(10);
}
void loop(void)
{
char kv=NO_KEY, kc=0;
int8_t keypressed = scankeys();
if(keypressed > NO_KEY){
kv = keyval[keypressed];
kc = keychar[keypressed];
sleeptimer=0;
}
#if defined(TEST_SMALL) || !defined(ARDUINO)
else{
kv = kc = skey.getKey(); //use serial port if no hardware keyboard is available
}
#endif
if (kv != NO_KEY){
if(stop_req){
stop_req=false;
//sysrpn.stack_push();
//sysrpn.set_x(f64(sleeptimer));
}
else sysrpn.key_input(kv,kc);
}
else if(stop_req){
lcd.home();
lcd.print(f64(f64(sleeptimer)/f64(10)).toString());
//lcd.print(f64(sleeptimer).toString());
}
if(!stop_req && (sleep_req || sleeptimer>300)){
lcd.command(LCD_DISPLAYCONTROL|LCD_DISPLAYOFF);
analogWrite(BACKLIGHT_PIN,0);
while(!digitalRead(rpin[6]))delay(100); //wait for key release
gosleep();
delay(100);
sleeptimer=0;
//stop_req=false;
sleep_req=1;
sysrpn.key_input('!','.'); // complete the wake up
}
waitRelease(keypressed);
sleeptimer++;
delay(91);//approx 100ms loop
//while(scankeys()!=NO_KEY); //this doesn't work well
}
/*
int freeRam () {
#ifndef NOT_ARDUINO
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
#endif
return -1;
}
*/
#if defined(ARDUINO)
void gosleep()
{
digitalWrite(cpin[0],LOW);
for(int i=1;i<5;i++){
digitalWrite(cpin[i],HIGH);
}
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
ADCSRA &= ~(1<<ADEN); // adc off
EICRA=0; //low level on pin 2
EIMSK=1; //enable INT0
DIDR0=0x3f; //disable digital input on ADC 0..5
DIDR1=0x03; //disable digital input on AIN0/1
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
DIDR0 = DIDR1 = 0; //reenable digital input
}
ISR(INT0_vect)
{
EIMSK=0; //disable INT0
}
#else
void gosleep(){}
#endif