forked from pailpoe/MiniDro2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniDro2.ino
385 lines (344 loc) · 11.3 KB
/
MiniDro2.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
382
383
384
385
/*********************************************************************
Project Name : Mini Dro 2
Hard revision : V1.0
Soft revision : /
Description : Dro system for lathe or milling machine with 3 quadrature decoder, Oled SSD1306 display and 6 push buttons for navigation
Chip : STM32F103CBT6
freq uc : 72Mhz (use 8Mhz external oscillator with PLL )
Compiler : Arduino IDE 1.8.3
Author : G.Pailleret, 2020
Remark :
Revision :
*********************************************************************/
#include "src/GEM/GEM_u8g2.h"
#include "src/QuadDecoder/QuadDecoder.h"
#include "src/Keypad/Keypad.h"
#include <EEPROM.h>
//#define USE_KEYPAD_KEYBOARD
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#ifdef USE_KEYPAD_KEYBOARD
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'Z' ,'Z' ,GEM_KEY_UP ,'Z' },
{GEM_KEY_CANCEL ,GEM_KEY_LEFT ,GEM_KEY_OK ,GEM_KEY_RIGHT },
{'Z' ,'Z' ,GEM_KEY_DOWN ,'Z' },
{'Z' ,'Z' ,'Z' ,'Z' }
};
byte rowPins[ROWS] = {PA5, PA4, PA3, PA2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {PB0, PB1, PB10, PB11}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad ( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//customKeypad.getKey();
#endif
typedef struct
{
boolean Inverted_X;
boolean Inverted_Y;
boolean Inverted_Z;
boolean Diameter_Mode_Y;
int Reso_X;
int Reso_Y;
int Reso_Z;
} sConfigDro;
const sConfigDro csConfigDefault = {false,false,false,false,512,512,512};
// Variable
sConfigDro ConfigDro;
GEMItem menuItemDirX("X dir:", ConfigDro.Inverted_X);
GEMItem menuItemDirY("Y dir:", ConfigDro.Inverted_Y);
GEMItem menuItemDirZ("Z dir:", ConfigDro.Inverted_Z);
GEMItem menuItemDiamY("Diameter Y", ConfigDro.Diameter_Mode_Y);
GEMItem menuItemResoX("X step/mm:", ConfigDro.Reso_X);
GEMItem menuItemResoY("Y step/mm:", ConfigDro.Reso_Y);
GEMItem menuItemResoZ("Z step/mm:", ConfigDro.Reso_Z);
void ActionRestoreSettingsInFlash(); // Forward declaration
GEMItem menuItemButtonRestoreSettings("Restore settings", ActionRestoreSettingsInFlash);
void ActionSaveSettingsInFlash(); // Forward declaration
GEMItem menuItemButtonSaveSettings("Save settings", ActionSaveSettingsInFlash);
void ActionDro(); // Forward declaration
GEMItem menuItemButtonDro("DRO screen", ActionDro);
void ActionDebug(); // Forward declaration
GEMItem menuItemButtonDebug("Debug screen", ActionDebug);
//Main Page Menu
GEMPage menuPageMain("Dro Menu");
//Settings Page Menu
GEMPage menuPageSettings("Settings"); // Settings submenu
GEMItem menuItemMainSettings("Settings", menuPageSettings);
//For tool selection
byte ToolChoose = 0;
SelectOptionByte selectToolOptions[] = {{"Tool_0", 0}, {"Tool_1", 1}, {"Tool_2", 2}, {"Tool_3", 3}, {"Tool_4", 4}, {"Tool_5", 5}};
GEMSelect selectTool(sizeof(selectToolOptions)/sizeof(SelectOptionByte), selectToolOptions);
void applyTool(); // Forward declaration
GEMItem menuItemTool("Tool:", ToolChoose, selectTool, applyTool);
boolean RelativeMode = false;
void UpdateRelAxe();
GEMItem menuItemRelativeMode("Relative mode", RelativeMode,UpdateRelAxe);
// Create menu object of class GEM_u8g2. Supply its constructor with reference to u8g2 object we created earlier
GEM_u8g2 menu(u8g2);
//Quadrature decoder
void IT_Timer1_Overflow(); // Forward declaration
void IT_Timer2_Overflow(); // Forward declaration
void IT_Timer3_Overflow(); // Forward declaration
QuadDecoder Quad_Y(3,QuadDecoder::LinearEncoder,512,false,false,IT_Timer3_Overflow); //Timer 3
QuadDecoder Quad_Z(2,QuadDecoder::RotaryEncoder,512,true,false,IT_Timer2_Overflow); //Timer 2
QuadDecoder Quad_X(1,QuadDecoder::LinearEncoder,512,false,false,IT_Timer1_Overflow); //Timer 1
void IT_Timer1_Overflow(){Quad_X.IT_OverflowHardwareTimer();}
void IT_Timer2_Overflow(){Quad_Z.IT_OverflowHardwareTimer();}
void IT_Timer3_Overflow(){Quad_Y.IT_OverflowHardwareTimer();}
void setup() {
// U8g2 library init. Pass pin numbers the buttons are connected to.
// The push-buttons should be wired with pullup resistors (so the LOW means that the button is pressed)
#ifdef USE_KEYPAD_KEYBOARD
u8g2.begin();
#endif
#ifndef USE_KEYPAD_KEYBOARD
u8g2.begin(/*Select/OK=*/ PB14, /*Right/Next=*/ PB1, /*Left/Prev=*/ PB0, /*Up=*/ PB15, /*Down=*/ PB12, /*Home/Cancel=*/ PB13);
#endif
//Restore config
Restore_Config();
// Menu init, setup and draw
menu.init();
setupMenu();
//menu.drawMenu(); //Start with menu screen
ActionDro(); //Start with dro screen
//ActionDebug(); //Start with dro screen
}
void setupMenu() {
// Add menu items to menu page
menuPageMain.addMenuItem(menuItemButtonDro);
menuPageMain.addMenuItem(menuItemButtonDebug);
menuPageMain.addMenuItem(menuItemTool);
menuPageMain.addMenuItem(menuItemRelativeMode);
//Add Sub menu Settings
menuPageMain.addMenuItem(menuItemMainSettings);
menuPageSettings.addMenuItem(menuItemDirX);
menuPageSettings.addMenuItem(menuItemResoX);
menuPageSettings.addMenuItem(menuItemDirY);
menuPageSettings.addMenuItem(menuItemResoY);
menuPageSettings.addMenuItem(menuItemDirZ);
menuPageSettings.addMenuItem(menuItemResoZ);
menuPageSettings.addMenuItem(menuItemDiamY);
menuPageSettings.addMenuItem(menuItemButtonRestoreSettings);
menuPageSettings.addMenuItem(menuItemButtonSaveSettings);
// Specify parent menu page for the Settings menu page
menuPageSettings.setParentMenuPage(menuPageMain);
// Add menu page to menu and set it as current
menu.setMenuPageCurrent(menuPageMain);
}
void loop() {
// If menu is ready to accept button press...
if (menu.readyForKey()) {
// ...detect key press using U8g2 library
// and pass pressed button to menu
#ifndef USE_KEYPAD_KEYBOARD
menu.registerKeyPress(u8g2.getMenuEvent());
#endif
#ifdef USE_KEYPAD_KEYBOARD
menu.registerKeyPress(customKeypad.getKey());
#endif
}
}
// *** DRO context with axe display
void ActionDro() {
menu.context.loop = DroContextLoop;
menu.context.enter = DroContextEnter;
menu.context.exit = DroContextExit;
menu.context.allowExit = false; // Setting to false will require manual exit from the loop
menu.context.enter();
}
void DroContextEnter() {
// Clear sreen
u8g2.clear();
}
void DroContextLoop() {
// Detect key press manually using U8g2 library
#ifndef USE_KEYPAD_KEYBOARD
byte key = u8g2.getMenuEvent();
#endif
#ifdef USE_KEYPAD_KEYBOARD
byte key = customKeypad.getKey();
#endif
if (key == GEM_KEY_CANCEL)
{
// Exit animation routine if GEM_KEY_CANCEL key was pressed
menu.context.exit();
} else
{
if(key == GEM_KEY_UP)Quad_X.SetZeroActiveMode();
if(key == GEM_KEY_DOWN)Quad_Z.SetZeroActiveMode();
if(key == GEM_KEY_OK)Quad_Y.SetZeroActiveMode();
DisplayDrawInformations();
}
}
void DroContextExit()
{
menu.reInit();
menu.drawMenu();
menu.clearContext();
}
// *** Debug context
void ActionDebug()
{
menu.context.loop = DebugContextLoop;
menu.context.enter = DebugContextEnter;
menu.context.exit = DebugContextExit;
menu.context.allowExit = false; // Setting to false will require manual exit from the loop
menu.context.enter();
}
void DebugContextEnter() {
// Clear sreen
u8g2.clear();
}
void DebugContextLoop() {
// Detect key press manually using U8g2 library
#ifndef USE_KEYPAD_KEYBOARD
byte key = u8g2.getMenuEvent();
#endif
#ifdef USE_KEYPAD_KEYBOARD
byte key = customKeypad.getKey();
#endif
u8g2.firstPage();
do {
u8g2.setColorIndex(1);
u8g2.setFont(u8g2_font_profont10_mr); // choose a suitable font
char buffer[16];
sprintf(buffer,"Speed Z:%d",Quad_Z.GiveMeTheSpeed());
u8g2.drawStr(2,1,buffer);
sprintf(buffer,"millis():%ld",millis());
u8g2.drawStr(2,10,buffer);
sprintf(buffer,"pos Z:%d",Quad_Z.GetValuePos());
u8g2.drawStr(2,19,buffer);
} while (u8g2.nextPage());
if (key == GEM_KEY_CANCEL)
{
// Exit animation routine if GEM_KEY_CANCEL key was pressed
menu.context.exit();
}
}
void DebugContextExit()
{
menu.reInit();
menu.drawMenu();
menu.clearContext();
}
void Restore_Config()
{
//Read Config in Memory
ReadConfigInFlash(&ConfigDro);
//Dispatch the config
Dispatch_Config(&ConfigDro);
}
void SaveConfigInFlash(sConfigDro *pConf)
{
unsigned int uiCount;
char *pt;
EEPROM.format();
pt = (char*)pConf;
for(uiCount=0;uiCount<sizeof(sConfigDro);uiCount++)
{
EEPROM.write(uiCount,*pt);
pt++;
}
}
void ReadConfigInFlash(sConfigDro *pConf)
{
unsigned int uiCount;
uint16 uiState;
uint16 value;
char *pt;
uiState = EEPROM_OK;
pt = (char*)pConf;
for(uiCount=0;uiCount<sizeof(sConfigDro);uiCount++)
{
uiState |= EEPROM.read(uiCount,&value);
*pt = (char) value;
pt++;
}
if(uiState != EEPROM_OK)
{
//Problem, restore default
*pConf = csConfigDefault;
}
}
void Dispatch_Config(sConfigDro *pConf)
{
Quad_X.SetSens( pConf->Inverted_X );
Quad_Y.SetSens( pConf->Inverted_Y );
Quad_Z.SetSens( pConf->Inverted_Z );
Quad_Y.SetDiameterMode(pConf->Diameter_Mode_Y);
Quad_X.SetResolution(pConf->Reso_X);
Quad_Y.SetResolution(pConf->Reso_Y);
Quad_Z.SetResolution(pConf->Reso_Z);
}
void PrintInformationOnScreen( char* str)
{
u8g2.clearBuffer();
u8g2.setCursor(0, 0);
u8g2.print(str);
u8g2.sendBuffer();
}
void ActionSaveSettingsInFlash()
{
//Store config in memort
SaveConfigInFlash(&ConfigDro);
//Dispatch config to function
Dispatch_Config(&ConfigDro);
//PrintInformationOnScreen("Save in flash");
//delay(100);
}
void ActionRestoreSettingsInFlash()
{
//Save default config in flash
SaveConfigInFlash((sConfigDro*)&csConfigDefault);
Restore_Config();
}
void DisplayDrawInformations()
{
char buffer_x[16];
char buffer_y[16];
char buffer_z[16];
sprintf(buffer_x,"%+4.3f",Quad_X.GetValue());
sprintf(buffer_y,"%+4.3f",Quad_Y.GetValue());
sprintf(buffer_z,"%+4.3f",Quad_Z.GetValue());
u8g2.firstPage();
do {
u8g2.setColorIndex(1);
//u8g2.setFont(u8g2_font_t0_22_mr); // choose a suitable font
u8g2.setFont(u8g2_font_profont22_tf); // choose a suitable font
u8g2.drawStr(2,1,"X");
u8g2.setColorIndex(1);
u8g2.drawStr(20,1,buffer_x); // write something to the internal memory
u8g2.drawRFrame(19,0,108,18,3);
u8g2.drawStr(2,19,"Y");
u8g2.setColorIndex(1);
u8g2.drawStr(20,19,buffer_y); // write something to the internal memory
u8g2.drawRFrame(19,18,108,18,3);
u8g2.drawStr(2,37,"Z");
u8g2.setColorIndex(1);
u8g2.drawStr(20,37,buffer_z); // write something to the internal memory
u8g2.drawRFrame(19,36,108,18,3);
u8g2.setFont(u8g2_font_profont10_mr); // choose a suitable font
u8g2.drawStr(0,54,selectTool.getSelectedOptionName((byte*)&ToolChoose ));
if(RelativeMode==true)u8g2.drawStr(80,54,"Relative");
else u8g2.drawStr(80,54,"Absolute");
//u8g2.sendBuffer(); // transfer internal memory to the display
} while (u8g2.nextPage());
}
void UpdateRelAxe()
{
if( RelativeMode == true )
{
Quad_X.SetRelative();
Quad_Y.SetRelative();
Quad_Z.SetRelative();
}
else
{
Quad_X.SetAbsolut();
Quad_Y.SetAbsolut();
Quad_Z.SetAbsolut();
}
}
void applyTool()
{
}