-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
381 lines (351 loc) · 15.7 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/***************************************************
Rotary cellphone original firmware written by Justine Haupt.
MIT license, all text above must be included in any redistribution.
1/12/2020: v1.0
1/13/2020: v1.1
1/18/2020: v1.1.1
Cellphone program for ATMega2560-based board controlling the Adafruit FONA v1. Adafruit libraries are not used for the FONA
but this does use the Adafruit 2.13" Tri-Color E-Ink display with the associated libraries. The license from Lady Ada for
that part of the program is as follows:
***************************************************
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************
NOTES:
Regarding references to "ModeSwitch_631". The mode switch is a SP3T switch which changes the operating mode of the phone. The primary mode, as listed in this code,
is "631", which is my area code. In this mode, 631 is prepended automatically to any 7-digit numbers dialed. All instances of this must be changed to your preferred
area code. The other two modes are "NP" and "Alt". NP stands for "no prepend", to enter full 10-digit numbers, and "Alt" is the alternate mode. To minimize the total
number of switches, the function of the bottom four buttons on the phone changes depending on the position of the mode switch.
Arduino IDE needs board support for 2560V. For this, add "MegaCore" board configuration from here: https://github.com/MCUdude/MegaCore
Also add the Adafruit EOD and the GxEPD2 libraries, which can be added from within the Arduino IDE by going to tools > Manage Libraries...
Use an AVR-ISP-MK2 to flash firmware. This is much easier than dealing with the USB port for programming. Just plug the programmer into the ICSP
header and use "Ctrl+Shift+U" within the Arduino IDE to compile and upload. No need to pick a dev assignment for the USB port.
The board settings under Tools in the Arduino IDE should be as follows (after installing the above MegaCore thingy):
Board: ATmega2560
Clock: External 8MHz
BOD: BOD 2.7v
Compiler LTO: LTO enabled
Pinout: Arduino MEGA pinout
Bootloader: Yes (UART0)
****************************************************/
#include <avr/io.h>
#include <SoftwareSerial.h>
#include "Adafruit_EPD.h"
#include <GxEPD2_BW.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMono9pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSerifItalic9pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <Fonts/FreeSansOblique9pt7b.h>
//For Adafruit e-ink display:
#define ENABLE_GxEPD2_GFX 0
#define MAX_HEIGHT(EPD) (EPD::HEIGHT <= 800 / (EPD::WIDTH / 8) ? EPD::HEIGHT : 800 / (EPD::WIDTH / 8))
GxEPD2_BW<GxEPD2_213_flex, MAX_HEIGHT(GxEPD2_213_flex)> display(GxEPD2_213_flex(25, 26, 28, 29)); //Pin order is ECS, D/C, RESET, BUSY. For Adafruit 2.13" Flexible Monochrome EPD (AKA GDEW0213I5F)
//Define variables:
const byte nChars = 32;
byte n = 1; //For counting up numbers from the rotary dial for entering a digit in the phone number
byte k = 0; //For specifying the digit in a phone number
unsigned long TimeSinceLastPulse = 0; //used to see if enough of a delay has happened since the last pulse from the rotary dial to consider the sequence complete.
char ReceivedChars[32]; // an array to store strings received over RS232 from FONA
String buffer; // String object buffer for incoming messages from FONA (Steve's method)
byte PNumber[30]; // an array to store phone numbers as they're dialed with the rotary dial
bool NewData = false; // a flag to indicate whether a string has been received over RS232
bool StillOn = false; // a flag to indicate that th
bool StartTimeSinceLastPulse = false; //This gets sets to "true" the first time the rotary dial is used.
bool CallOn = false; //Set to "true" when a call is in progress, to determine the function of the "call_startedn" pin.
bool newrotaryinput = false;
float fholder1;
int iholder;
int clvl; //call level storage integer
int rlvl; //ring level storage integer
float BattLevel;
float SigLevel;
int lhlf;
int rhlf;
int pagenum; //holder for page number
int mode; //1 = 631, 2 = NP, 3 = Alt. Marks the mode the phone's currently in. Needed for certain things.
//Define general output pins
const byte StatusLED = 13;
const byte HookLED = 49;
const byte BGLED1 = 7; //bargraph LED 0 (D15)
const byte BGLED2 = 6; //bargraph LED 1 (D14)
const byte BGLED3 = 16; //bargraph LED 2 (D13)
const byte BGLED4 = 17; //bargraph LED 3 (D12)
const byte BGLED5 = 14; //bargraph LED 4 (D11)
const byte BGLED6 = 15; //bargraph LED 5 (D10)
const byte BGLED7 = 42; //bargraph LED 6 (D9)
const byte BGLED8 = 43; //bargraph LED 7 (D8)
const byte BGLED9 = 47; //bargraph LED 8 (D7)
const byte BGLED10 = 48; //bargraph LED 9 (D6)
const byte FONAWake = A0;
const byte eink_ENA = 31;
const byte Tx = 9; //ATmega Tx pin (FONA Rx pin)
//Define input pin
const byte HookButton = 33; //Make call, hangup call, or answer incoming call (momentary switch shorts to gnd)
const byte ClearButton = 36; //Clear number currently in dialout buffer from rotary dial (momentary switch gnd)
const byte SignalButton = 34; //Hold to check signal strengh
const byte BatteryButton = 37; //Hold to check battery level
const byte SaButton = 35; //Hold to check battery level
const byte FnButton = 32; //Hold to check battery level
const byte ModeSwitch_631 = 20; //SP3T switch positioned to append a certain area code to all calls
const byte ModeSwitch_NP = 19; //SP3T switch posiioned to "No Prepend" mode, in which a full 10-digit phone number is needed.
const byte ModeSwitch_alt = 18; //SP3T switch positioned to switch to taking the alternate function of each button.
const byte RotaryPulseIn = 39; //The pin that reads the state of the rotary dial.
const byte PowerState = A2; //Reads the power state of the FONA module, 1=ON
const byte Rx = 12; //ATmega Rx pin (FONA Tx pin)
SoftwareSerial FONAserial(Rx, Tx); //ATmega receive pin, ATmega transmit pin
void setup() {
// Set output pin functions
pinMode(StatusLED, OUTPUT);
pinMode(HookLED, OUTPUT);
pinMode(BGLED1, OUTPUT);
pinMode(BGLED2, OUTPUT);
pinMode(BGLED3, OUTPUT);
pinMode(BGLED4, OUTPUT);
pinMode(BGLED5, OUTPUT);
pinMode(BGLED6, OUTPUT);
pinMode(BGLED7, OUTPUT);
pinMode(BGLED8, OUTPUT);
pinMode(BGLED9, OUTPUT);
pinMode(BGLED10, OUTPUT);
pinMode(FONAWake, OUTPUT);
pinMode(eink_ENA, OUTPUT);
// Define input pin functions
pinMode(RotaryPulseIn, INPUT_PULLUP);
pinMode(HookButton, INPUT_PULLUP);
pinMode(ClearButton, INPUT_PULLUP);
pinMode(SignalButton, INPUT_PULLUP);
pinMode(BatteryButton, INPUT_PULLUP);
pinMode(SaButton, INPUT_PULLUP);
pinMode(FnButton, INPUT_PULLUP);
pinMode(ModeSwitch_631, INPUT_PULLUP);
pinMode(ModeSwitch_NP, INPUT_PULLUP);
pinMode(ModeSwitch_alt, INPUT_PULLUP);
pinMode(PowerState, INPUT);
digitalWrite(eink_ENA, HIGH); //Pull the enable pin up on the e-ink display
digitalWrite(FONAWake, HIGH); //Default state for the FONA Power pin input
delay(500);
Serial.begin(9600); //USB serial port
//Check if FONA is ON, turn on if necessary.
if (digitalRead(PowerState) == LOW) {
digitalWrite(FONAWake, LOW); //Holding LOW for 64ms typ toggles FONA power state
delay(128); //use twice the time to be sure
digitalWrite(FONAWake, HIGH);
while (PowerState == LOW) {} //Wait for FONA to power on
}
delay(6000); //Wait for FONA to initialise
setFONAbaud(); //Test and set FONA baud rate if necessary
FONAserial.println("AT+CVHU=0"); //Sets voice "hang up" control so that "ATH" disconnects voice calls.
delay(80);
FONAserial.println("AT+CSDVC=3"); //Set audio output channel. 3 is the speaker.
delay(80);
FONAserial.println("AT+VMUTE=0"); //Set speaker mute to OFF
delay(80);
FONAserial.println("AT+CRXGAIN=10000"); // Set Rx Gain, which affects the speaker volume during calls. This is a good value for use of the speaker as a handset.
delay(80);
FONAserial.println("AT+CLVL=3"); //Set volume (0-8)
delay(80);
FONAserial.println("AT+CRSL=8"); //Ringer volume (0-8)
n = 0; //Starting phone number digit value is 0
k = 0; //Starting phone number digit position is 1
display.init(115200);
display.setRotation(0);
display.setTextColor(GxEPD_BLACK);
pinMode(Rx, INPUT_PULLUP); // required because the display uses the SPI bus which sets this to output!
digitalWrite(StatusLED, HIGH);
delay(500);
digitalWrite(StatusLED, LOW);
ClearBuffer();
}
void loop() {
if (CallOn == true) {
digitalWrite(HookLED, HIGH);
}
else if (CallOn == false) {
digitalWrite(HookLED, LOW);
}
if (digitalRead(ModeSwitch_631) == LOW) {
mode = 1;
}
else if (digitalRead(ModeSwitch_NP) == LOW) {
mode = 2;
}
else if (digitalRead(ModeSwitch_alt) == LOW) {
mode = 3;
}
//*********************ROTARY DIAL INPUT & FUNCTION BUTTON*******************************************************************************
if (digitalRead(ModeSwitch_631) == LOW || digitalRead(ModeSwitch_NP) == LOW) {
if (digitalRead(FnButton) == LOW) {
StarPoundRotaryIn(); //This function doesn't loop on it's own, but rather depends on the main "void loop" to do its work.
}
else {
RotaryIn(); //This function doesn't loop on it's own, but rather depends on the main "void loop" to do its work.
}
}
else {
RotaryIn(); //This function doesn't loop on it's own, but rather depends on the main "void loop" to do its work.
}
//*********************CLEAR (C) BUTTON**************************************************************************
if (digitalRead(ClearButton) == LOW) {
delay(200);
//IF THE CLEAR BUTTON IS STILL DEPRESSED, CLEAR THE BUFFER
if (digitalRead(ClearButton) == LOW) {
ClearBuffer(); //Clear whatever number has been entered so far
digitalWrite(StatusLED, HIGH);
delay(100);
digitalWrite(StatusLED, LOW);
delay(500);
}
}
//*********************HOOK (H) BUTTON***************************************************************************
if (digitalRead(HookButton) == LOW) { //Check CALL button
//PLACE A CALL IF THE ROTARY HAS BEEN USED & AT LEAST 7 DIGITS WERE DIALED
if (newrotaryinput = true && PNumber[6] != 99) {
if (digitalRead(ModeSwitch_631) == LOW) { //Check mode switch
MakeCall631(); //Make a call using the number stored in the buffer as input from the rotary dial, prepended with 631
}
else { //If in either No-Prepend or Alt mode
MakeCall(); //Make a call using the number stored in the buffer as input from the rotary dial
}
}
//ANSWER INCOMING CALL IF CALLON = FALSE
else if (CallOn == false) {
FONAserial.println("ATA");
//CallOn = true; //!!!FIX!!!. The problem with turning the CallOn flag ON is that there's no check to see if a call was actually picked up.
}
delay(400);
//IF STILL HOLDING THE HOOK BUTTON BY ITSELF, HANGUP CALL REGARDLESS OF CALLON STATE
if (digitalRead(HookButton) == LOW) {
FONAserial.println("ATH");
delay(100);
FONAserial.println("AT+CHUP");
if (CallOn == true) { //If CallOn = true, make it false. This is just for the hook LED.
CallOn = false;
}
}
}
//*********************S BUTTON**********************************************************************************
if (digitalRead(SignalButton) == LOW) { //If the signal strength button is being depressed, go to the function that displays the signal strength with the LEDs
//IF IN 631 MODE CHECK SIGNAL STRENGTH
if (digitalRead(ModeSwitch_631) == LOW) {
mode = 1;
SignalStrength();
}
//IF IN NP MODE CALL SPEED DIAL 1
else if (digitalRead(ModeSwitch_NP) == LOW) {
mode = 2;
ClearBufferSilent();
pagenum = 5;
PNumber[0] = 4;
RetrieveContact();
MakeCall();
}
//IF IN ALT MODE, SET CALL VOLUME
else if (digitalRead(ModeSwitch_alt) == LOW) {
mode = 3;
ClearBufferSilent();
while (digitalRead(SignalButton) == LOW) {
RotaryIn();
}
if (PNumber[0] != 99) {
clvl = PNumber[0];
FONAserial.print("AT+CLVL=");
FONAserial.println(clvl);
BarGraphSlow(PNumber[0]);
delay(100);
BarGraphSlow(PNumber[0]);
delay(100);
BarGraphSlow(PNumber[0]);
ClearBufferSilent();
}
}
}
//*********************B BUTTON**********************************************************************************
if (digitalRead(BatteryButton) == LOW) {
//IF IN 631 MODE, CHECK BATTERY LEVEL
if (digitalRead(ModeSwitch_631) == LOW) {
mode = 1;
BatteryLevel();
}
//IF IN NP MODE, CALL SPEED DIAL 2
else if (digitalRead(ModeSwitch_NP) == LOW) {
mode = 2;
ClearBufferSilent();
pagenum = 3;
PNumber[0] = 4;
RetrieveContact();
MakeCall();
}
//IF IN ALT MODE, SET RING VOLUME
else if (digitalRead(ModeSwitch_alt) == LOW) {
mode = 3;
ClearBufferSilent();
while (digitalRead(BatteryButton) == LOW) {
RotaryIn();
}
if (PNumber[0] != 99) {
rlvl = PNumber[0];
FONAserial.print("AT+CRSL=");
FONAserial.println(rlvl);
BarGraphSlow(PNumber[0]);
delay(100);
BarGraphSlow(PNumber[0]);
delay(100);
BarGraphSlow(PNumber[0]);
ClearBufferSilent();
}
}
}
//*********************Sa BUTTON**********************************************************************************
if (digitalRead(SaButton) == LOW) { //I couldn't think of a name for this button. Sa doesn't have particular meaning.
//IF IN 631 MODE, DO SOMETHING TBD
if (digitalRead(ModeSwitch_631) == LOW) {
mode = 1;
//placeholder
}
//IF IN NP MODE, CALL VOICEMAIL
else if (digitalRead(ModeSwitch_NP) == LOW) {
mode = 2;
ClearBufferSilent();
pagenum = 8;
PNumber[0] = 4;
RetrieveContact();
MakeCall();
}
//IF IN ALT MODE, CALL CONTACT FROM CURRENT PAGE
else if (digitalRead(ModeSwitch_alt) == LOW) {
mode = 3;
ClearBufferSilent();
newrotaryinput = false;
while (digitalRead(SaButton) == LOW) {
RotaryIn();
}
if (PNumber[0] != 99) {
RetrieveContact();
MakeCall();
newrotaryinput = false;
ClearBufferSilent();
}
}
}
//*********************ALT MODE (IN GENERAL)**********************************************************************************
//In addition to the ALT MODE stuff specific to each button this section is needed for general stuff.
//PREVENT CONTACTS PAGE FROM UPDATING IF THE ROTARY DIAL WAS USED BEFORE SWITCHING TO ALT MODE
if (digitalRead(ModeSwitch_alt) == LOW) {
if ((mode == 1) || (mode == 2)) {
ClearBufferSilent();
newrotaryinput = false; //Prevents display from updating upon entering ALT mode if a number was dialed previously but not used for another function.
}
mode = 3;
//DISPLAY CONTACTS PAGES
if (newrotaryinput == true) {
DisplayContacts();
newrotaryinput = false;
ClearBufferSilent();
}
}
}