Skip to content

Commit f7c5502

Browse files
LCD menu, DayTime class, CTRL menu
Added LCD Menu class to handle menu display, switching, etc. Added DayTime class to handle adding and subtracting time while correctly handing over and underflow. Replaced HA and RA with DayTime class as a first attempt. Added CTRL menu that allows moving both axes in both directions without changing time settings. Debounced menu keys that make single choices. Moved delay on keypress into each keypress case, since some needed to not delay. Added status to LCD while in a stepper loop. Moved writing HA to EEPROM to where HA menu is left (need to add check whether it's changed).
1 parent e3290c0 commit f7c5502

File tree

9 files changed

+640
-264
lines changed

9 files changed

+640
-264
lines changed

Software/Arduino code/OpenAstroTracker/a_inits.ino

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// If you really want to look through this code, i apologise for my terrible coding
22
//#include <SoftwareSerial.h>
3+
#include <stdarg.h>
34
#include <EEPROM.h>
45
#include <AccelStepper.h>
56
#include <LiquidCrystal.h>
@@ -33,7 +34,7 @@ LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
3334
#define Polaris_Menu 3
3435
#define Heat_Menu 4
3536
#define Calibration_Menu 5
36-
#define Last_Menu Calibration_Menu
37+
#define Control_Menu 6
3738

3839
int lcd_key = 0;
3940
int adc_key_in = 0;
@@ -48,8 +49,40 @@ int read_LCD_buttons() {
4849
//return btnNONE;
4950
}
5051

52+
// Adjust the given number by the given adjustment, wrap around the limits.
53+
// Limits are inclusive, so they represent the lowest and highest valid number.
54+
int adjustWrap(int current, int adjustBy, int minVal, int maxVal)
55+
{
56+
current += adjustBy;
57+
if (current > maxVal) current -= (maxVal + 1);
58+
if (current < minVal) current += (maxVal + 1);
59+
return current;
60+
}
61+
62+
// Adjust the given number by the given adjustment, clamping to the limits.
63+
// Limits are inclusive, so they represent the lowest and highest valid number.
64+
int adjustClamp(int current, int adjustBy, int minVal, int maxVal)
65+
{
66+
current += adjustBy;
67+
if (current > maxVal) current = maxVal;
68+
if (current < minVal) current = minVal;
69+
return current;
70+
}
71+
72+
int adjustSecond(int current, int adjustBy) {
73+
return adjustWrap(current, adjustBy, 0, 59);
74+
}
75+
76+
int adjustMinute(int current, int adjustBy) {
77+
return adjustWrap(current, adjustBy, 0, 59);
78+
}
79+
80+
int adjustHour(int current, int adjustBy) {
81+
return adjustWrap(current, adjustBy, 0, 23);
82+
}
5183

5284
String inString = "";
85+
int controlDisplay =0;
5386

5487
AccelStepper stepperRA(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
5588
AccelStepper stepperDEC(HALFSTEP, motorPin11, motorPin13, motorPin12, motorPin14);
@@ -66,11 +99,9 @@ String logString;
6699
//unsigned long timeWait;
67100
boolean isPulseGuiding = true;
68101

69-
unsigned long Zeit;
70102
float onehour;
71103

72104
boolean pcControl = false;
73-
int menu = HA_Menu;
74105
int currentSecs;
75106
int currentMins;
76107
float inputcal;
@@ -85,14 +116,14 @@ int tracking = 1;
85116
float trackingspeed;
86117

87118
//RA stuff
88-
float hourRA;
89-
float minRA = 0;
90-
float secRA = 0;
119+
//float hourRA;
120+
//float minRA = 0;
121+
//float secRA = 0;
91122
float moveRA;
92123
int RAselect;
93-
int hourRAprint;
94-
int minRAprint;
95-
int secRAprint;
124+
//int hourRAprint;
125+
//int minRAprint;
126+
//int secRAprint;
96127

97128
//DEC stuff
98129
int degreeDEC;
@@ -102,21 +133,16 @@ float moveDEC;
102133
int DECselect;
103134
int printdegDEC;
104135

105-
//Hour correction
106-
int hourHA = 0;
107-
int minHA = 0;
108136
int HAselect;
109-
int hHAcorrection;
110-
int mHAcorrection = -5 ; //wenn minus dann hHAcorrection -1
111-
int sHAcorrection;
112-
int hourHAzeit;
113-
int minHAzeit;
137+
//int hHAcorrection;
138+
//int mHAcorrection = -5 ; //wenn minus dann hHAcorrection -1
139+
//int sHAcorrection;
114140

115141
int heatselect;
116142
int RAheat = 0;
117143
int DECheat = 1;
118144

119-
//Stellarium
145+
//Stellarium
120146
char current_RA[9];
121147
char current_DEC[10];
122148
int HAh_save;
@@ -160,27 +186,5 @@ byte sec[] = {
160186
B00000
161187
};
162188

163-
byte RightArr[] = {
164-
B00000,
165-
B01000,
166-
B01100,
167-
B01110,
168-
B01100,
169-
B01000,
170-
B00000,
171-
B00000
172-
};
173-
174-
byte LeftArr[] = {
175-
B00000,
176-
B00010,
177-
B00110,
178-
B01110,
179-
B00110,
180-
B00010,
181-
B00000,
182-
B00000
183-
};
184-
185189
//debugging
186190
String inBT;
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
class MenuItem {
2+
String _display;
3+
int _id;
4+
MenuItem* _nextItem;
5+
public:
6+
MenuItem(String display, int id)
7+
{
8+
_display = display;
9+
_id = id;
10+
_nextItem = NULL;
11+
}
12+
13+
String display() {
14+
return _display;
15+
}
16+
int id() {
17+
return _id;
18+
}
19+
MenuItem* nextItem() {
20+
return _nextItem;
21+
}
22+
void setNextItem(MenuItem* next) {
23+
_nextItem = next;
24+
}
25+
};
26+
27+
class LcdMenu {
28+
private:
29+
LiquidCrystal* _lcd;
30+
MenuItem* _firstItem;
31+
MenuItem* _lastItem;
32+
int _activeId;
33+
int _longestDisplay;
34+
int _columns;
35+
36+
int _leftArrow = 3;
37+
int _rightArrow = 4;
38+
39+
public:
40+
LcdMenu(LiquidCrystal* lcd, int cols) {
41+
_activeId = 0;
42+
_lcd = lcd;
43+
_firstItem = NULL;
44+
_lastItem = NULL;
45+
_longestDisplay = 0;
46+
_columns = cols;
47+
lcd->createChar(_leftArrow, LeftArr);
48+
lcd->createChar(_rightArrow, RightArr);
49+
}
50+
51+
MenuItem* findById(int id)
52+
{
53+
MenuItem* item = _firstItem;
54+
while ((item != NULL) && (item->id() != id)) {
55+
item = item->nextItem();
56+
}
57+
return item;
58+
}
59+
60+
void addItem(String disp, int id) {
61+
if (_firstItem == NULL) {
62+
_firstItem = new MenuItem(disp, id);
63+
_lastItem = _firstItem;
64+
}
65+
else {
66+
MenuItem* newItem = new MenuItem(disp, id);
67+
_lastItem->setNextItem(newItem);
68+
_lastItem = newItem;
69+
}
70+
_longestDisplay = max(_longestDisplay, disp.length());
71+
72+
updateDisplay();
73+
}
74+
75+
void setActive(int id) {
76+
_activeId = id;
77+
}
78+
79+
void setNextActive() {
80+
if (_lastItem->id() == _activeId) {
81+
_activeId = _firstItem->id();
82+
}
83+
else {
84+
MenuItem *item = findById(_activeId);
85+
if (item != NULL) {
86+
_activeId = item->nextItem()->id();
87+
}
88+
}
89+
}
90+
91+
int getActive() {
92+
return _activeId;
93+
}
94+
95+
void updateDisplay() {
96+
_lcd->setCursor(0, 0);
97+
String menuString = "";
98+
MenuItem* item = _firstItem;
99+
int offsetToActive = 0;
100+
int offset = 0;
101+
while (item != NULL) {
102+
String itemString = "";
103+
if (item->id() == _activeId) {
104+
offsetToActive = offset;
105+
itemString = ">" + item->display() + "<";
106+
}
107+
else {
108+
itemString = " " + item->display() + " ";
109+
}
110+
menuString += itemString;
111+
offset += itemString.length();
112+
item = item->nextItem();
113+
}
114+
int margin = (_columns - (_longestDisplay)) / 2;
115+
int offsetIntoString = offsetToActive - margin;
116+
117+
while (offsetIntoString < 0) {
118+
menuString = " " + menuString;
119+
offsetIntoString++;
120+
}
121+
122+
String displayString = menuString.substring(offsetIntoString, offsetIntoString + _columns);
123+
while (displayString.length() < _columns)
124+
{
125+
displayString += " ";
126+
}
127+
printMenu(displayString);
128+
}
129+
130+
void printMenuArg(const char* input, ...) {
131+
va_list args;
132+
va_start(args, input);
133+
int leastCount = 0;
134+
for (const char* i = input; *i != 0; i++) {
135+
if (*i != '%') {
136+
leastCount++;
137+
if (*i == '>') {
138+
_lcd->write(_rightArrow);
139+
}
140+
else if (*i == '<') {
141+
_lcd->write(_leftArrow);
142+
}
143+
else {
144+
_lcd->print(*i);
145+
}
146+
continue;
147+
}
148+
i++;
149+
leastCount++;
150+
switch (*i) {
151+
case '%': _lcd->print('%'); break;
152+
case 'c':
153+
{
154+
byte b = (byte) (va_arg(args, int) && 0x00FF);
155+
_lcd->write((byte)b);
156+
}
157+
break;
158+
case 's': _lcd->print(va_arg(args, char*)); break;
159+
case 'd': _lcd->print((int)va_arg(args, int)); break;
160+
case 'f': _lcd->print((float)va_arg(args, float)); break;
161+
default: _lcd->print((char)*i); break;
162+
}
163+
}
164+
va_end(args);
165+
while (leastCount < _columns) {
166+
_lcd->print(" ");
167+
leastCount++;
168+
}
169+
}
170+
171+
// Print the given string to the lcd, substituting the special arrows and padding with spaces to the end
172+
void printMenu(String line)
173+
{
174+
int spaces = _columns - line.length();
175+
for (int i = 0; i < line.length(); i++) {
176+
if (line[i] == ' > ') {
177+
_lcd->write(_rightArrow);
178+
}
179+
else if (line[i] == ' < ') {
180+
_lcd->write(_leftArrow);
181+
}
182+
else {
183+
_lcd->print(line[i]);
184+
}
185+
}
186+
187+
// Clear the rest of the display
188+
while (spaces > 0) {
189+
_lcd->print(" ");
190+
}
191+
}
192+
193+
byte RightArr[8] = {
194+
B00000,
195+
B01000,
196+
B01100,
197+
B01110,
198+
B01100,
199+
B01000,
200+
B00000,
201+
B00000
202+
};
203+
204+
byte LeftArr[8] = {
205+
B00000,
206+
B00010,
207+
B00110,
208+
B01110,
209+
B00110,
210+
B00010,
211+
B00000,
212+
B00000
213+
};
214+
215+
};

0 commit comments

Comments
 (0)