Skip to content

Commit 59347af

Browse files
authored
Merge pull request afarhan#3 from reedbn/menu-refactor
Menu refactor
2 parents 7b2057e + 1f033b6 commit 59347af

File tree

11 files changed

+516
-421
lines changed

11 files changed

+516
-421
lines changed

PDQ_MinLib/PDQ_GFX.h

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class PDQ_GFX : public Print {
145145
static void drawChar(coord_t x, coord_t y, unsigned char c, color_t color, color_t bg, uint8_t size);
146146
static void drawCharGFX(coord_t x, coord_t y, unsigned char c, color_t color, color_t bg, uint8_t size);
147147
static inline void setCursor(coord_t x, coord_t y);
148+
static inline void setBound(coord_t x, coord_t y);
148149
static inline void setTextColor(color_t c);
149150
static inline void setTextColor(color_t c, color_t bg);
150151
static inline void setTextSize(uint8_t s);
@@ -157,8 +158,8 @@ class PDQ_GFX : public Print {
157158
static inline uint8_t getRotation() __attribute__ ((always_inline)) { return rotation; }
158159
static inline coord_t getCursorX() __attribute__ ((always_inline)) { return cursor_x; }
159160
static inline coord_t getCursorY() __attribute__ ((always_inline)) { return cursor_y; }
160-
static inline void getTextBounds(char *string, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
161-
static inline void getTextBounds(const __FlashStringHelper *s, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
161+
static inline void getTextBounds(char *string, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h, coord_t wi = _width);
162+
static inline void getTextBounds(const __FlashStringHelper *s, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h, coord_t wi = _width);
162163

163164
virtual size_t write(uint8_t); // used by Arduino "Print.h" (and the one required virtual function)
164165

@@ -167,6 +168,7 @@ class PDQ_GFX : public Print {
167168
static coord_t WIDTH, HEIGHT; // This is the 'raw' display w/h - never changes
168169
static coord_t _width, _height; // Display w/h as modified by current rotation
169170
static coord_t cursor_x, cursor_y;
171+
static coord_t bound_x1, bound_x2;
170172
static color_t textcolor, textbgcolor;
171173
static uint8_t textsize;
172174
static uint8_t rotation;
@@ -216,6 +218,10 @@ int16_t PDQ_GFX<HW>::cursor_x;
216218
template<class HW>
217219
int16_t PDQ_GFX<HW>::cursor_y;
218220
template<class HW>
221+
int16_t PDQ_GFX<HW>::bound_x1;
222+
template<class HW>
223+
int16_t PDQ_GFX<HW>::bound_x2;
224+
template<class HW>
219225
color_t PDQ_GFX<HW>::textcolor;
220226
template<class HW>
221227
color_t PDQ_GFX<HW>::textbgcolor;
@@ -239,6 +245,8 @@ PDQ_GFX<HW>::PDQ_GFX(coord_t w, coord_t h)
239245
_height = (int16_t)h;
240246
cursor_x = 0;
241247
cursor_y = 0;
248+
bound_x1 = 0;
249+
bound_x2 = WIDTH;
242250
rotation = 0;
243251
textsize = 1;
244252
textcolor = 0xffff;
@@ -740,7 +748,7 @@ size_t PDQ_GFX<HW>::write(uint8_t c)
740748
{
741749
if(c == '\n')
742750
{
743-
cursor_x = 0;
751+
cursor_x = bound_x1;
744752
cursor_y += (coord_t)textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
745753
}
746754
else if (c != '\r')
@@ -756,10 +764,10 @@ size_t PDQ_GFX<HW>::write(uint8_t c)
756764
if ((w > 0) && (h > 0))
757765
{
758766
coord_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
759-
if(wrap && ((cursor_x + textsize * (xo + w)) >= _width))
767+
if(wrap && ((cursor_x + textsize * (xo + w)) >= bound_x2))
760768
{
761769
// Drawing character would go off right edge; wrap to new line
762-
cursor_x = 0;
770+
cursor_x = bound_x1;
763771
cursor_y += (coord_t)textsize *
764772
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
765773
}
@@ -970,6 +978,13 @@ void PDQ_GFX<HW>::setCursor(coord_t x, coord_t y)
970978
cursor_y = (int16_t)y;
971979
}
972980

981+
template<class HW>
982+
void PDQ_GFX<HW>::setBound(coord_t x1, coord_t x2)
983+
{
984+
bound_x1 = (int16_t)x1;
985+
bound_x2 = (int16_t)x2;
986+
}
987+
973988
template<class HW>
974989
void PDQ_GFX<HW>::setTextSize(uint8_t s)
975990
{
@@ -1046,10 +1061,13 @@ void PDQ_GFX<HW>::setFont(const GFXfont *f)
10461061

10471062
// Pass string and a cursor position, returns UL corner and W,H.
10481063
template<class HW>
1049-
void PDQ_GFX<HW>::getTextBounds(char *str, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
1064+
void PDQ_GFX<HW>::getTextBounds(char *str, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h, coord_t wi)
10501065
{
10511066
uint8_t c; // Current character
10521067

1068+
coord_t xs = x;
1069+
coord_t xe = xs+wi;
1070+
10531071
*x1 = x;
10541072
*y1 = y;
10551073
*w = *h = 0;
@@ -1080,9 +1098,9 @@ void PDQ_GFX<HW>::getTextBounds(char *str, coord_t x, coord_t y, int16_t *x1, in
10801098
xa = pgm_read_byte(&glyph->xAdvance);
10811099
xo = pgm_read_byte(&glyph->xOffset);
10821100
yo = pgm_read_byte(&glyph->yOffset);
1083-
if (wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) // Line wrap
1101+
if (wrap && ((x + (((int16_t)xo + gw) * ts)) >= xe)) // Line wrap
10841102
{
1085-
x = 0; // Reset x to 0
1103+
x = xs; // Reset x to 0
10861104
y += ya; // Advance y by 1 line
10871105
}
10881106
gx1 = x + xo * ts;
@@ -1103,7 +1121,7 @@ void PDQ_GFX<HW>::getTextBounds(char *str, coord_t x, coord_t y, int16_t *x1, in
11031121
}
11041122
else // Newline
11051123
{
1106-
x = 0; // Reset x
1124+
x = xs; // Reset x
11071125
y += ya; // Advance y by 1 line
11081126
}
11091127
}
@@ -1158,10 +1176,12 @@ void PDQ_GFX<HW>::getTextBounds(char *str, coord_t x, coord_t y, int16_t *x1, in
11581176

11591177
// Same as above, but for PROGMEM strings
11601178
template<class HW>
1161-
void PDQ_GFX<HW>::getTextBounds(const __FlashStringHelper *str, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
1179+
void PDQ_GFX<HW>::getTextBounds(const __FlashStringHelper *str, coord_t x, coord_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h, coord_t wi)
11621180
{
11631181
uint8_t *s = (uint8_t *)str;
11641182
uint8_t c;
1183+
coord_t xs = x;
1184+
coord_t xe = xs+wi;
11651185

11661186
*x1 = x;
11671187
*y1 = y;
@@ -1194,9 +1214,9 @@ void PDQ_GFX<HW>::getTextBounds(const __FlashStringHelper *str, coord_t x, coord
11941214
xa = pgm_read_byte(&glyph->xAdvance);
11951215
xo = pgm_read_byte(&glyph->xOffset);
11961216
yo = pgm_read_byte(&glyph->yOffset);
1197-
if (wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) // Line wrap
1217+
if (wrap && ((x + (((int16_t)xo + gw) * ts)) >= xe)) // Line wrap
11981218
{
1199-
x = 0; // Reset x to 0
1219+
x = xs; // Reset x to 0
12001220
y += ya; // Advance y by 1 line
12011221
}
12021222
gx1 = x + xo * ts;
@@ -1217,7 +1237,7 @@ void PDQ_GFX<HW>::getTextBounds(const __FlashStringHelper *str, coord_t x, coord
12171237
}
12181238
else // Newline
12191239
{
1220-
x = 0; // Reset x
1240+
x = xs; // Reset x
12211241
y += ya; // Advance y by 1 line
12221242
}
12231243
}

morse.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ static const PROGMEM struct Morse morse_table[] = {
5656
{'?', 0x8c}, // 10001100
5757
};
5858

59-
static void morseLetter(char c){
59+
static void morseLetter(char c, uint16_t dit_duration_ms){
6060
unsigned char mask = 0x80;
6161

6262
//handle space character as three dashes
6363
if (c == ' '){
64-
active_delay(9 * globalSettings.cwDitDurationMs);
64+
active_delay(7 * dit_duration_ms);
6565
//Serial.print(' ');
6666
return;
6767
}
@@ -81,36 +81,28 @@ static void morseLetter(char c){
8181
while(mask){
8282
tone(CW_TONE, globalSettings.cwSideToneFreq,10000);
8383
if (mask & code){
84-
delay(3 * globalSettings.cwDitDurationMs);
84+
delay(3 * dit_duration_ms);
8585
//Serial.print('-');
8686
}
8787
else{
88-
delay(globalSettings.cwDitDurationMs);
88+
delay(dit_duration_ms);
8989
//Serial.print('.');
9090
}
9191
//Serial.print('#');
9292
noTone(CW_TONE);
93-
delay(globalSettings.cwDitDurationMs); // space between dots and dashes
93+
delay(dit_duration_ms); // space between dots and dashes
9494
mask = mask >> 1;
9595
}
9696
//Serial.println('@');
97-
delay(2*globalSettings.cwDitDurationMs); // space between letters is a dash (3 dots), one dot's space has already been sent
97+
delay(2*dit_duration_ms); // space between letters is a dash (3 dots), one dot's space has already been sent
9898
break;//We've played the letter, so don't bother checking the rest of the list
9999
}
100100
}
101101
}
102102

103-
void morseText(char *text){
104-
// while (1){
105-
noTone(CW_TONE);
106-
delay(1000);
107-
tone(CW_TONE, 600);
108-
delay(1000);
109-
// }
110-
111-
//Serial.println(globalSettings.cwSideToneFreq);
103+
void morseText(char *text, uint16_t dit_duration_ms){
112104
while(*text){
113-
morseLetter(*text++);
105+
morseLetter(*text++, dit_duration_ms);
114106
}
115107
}
116108

morse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
#include "settings.h"
12
//sends out morse code at the speed set by cwSpeed
2-
extern int cwSpeed; //this is actuall the dot period in milliseconds
3-
void morseText(char *text);
3+
void morseText(char *text, uint16_t dit_duration_ms = globalSettings.cwDitDurationMs);

nano_gui.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void displayInit(void){
178178

179179
tft.begin();
180180
tft.setFont(ubitx_font);
181-
tft.setTextWrap(false);
181+
tft.setTextWrap(true);
182182
tft.setTextColor(DISPLAY_GREEN,DISPLAY_BLACK);
183183
tft.setTextSize(1);
184184
tft.setRotation(1);
@@ -217,6 +217,14 @@ void displayChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t
217217
void displayRawText(char *text, int x1, int y1, int color, int background){
218218
tft.setTextColor(color,background);
219219
tft.setCursor(x1,y1);
220+
tft.setBound(0,320);
221+
tft.print(text);
222+
}
223+
224+
void displayRawText(char *text, int x1, int y1, int w, int color, int background){
225+
tft.setTextColor(color,background);
226+
tft.setCursor(x1,y1);
227+
tft.setBound(x1,x1+w);
220228
tft.print(text);
221229
}
222230

@@ -228,10 +236,10 @@ void displayText(char *text, int x1, int y1, int w, int h, int color, int backgr
228236
int16_t y1_out;
229237
uint16_t width_out;
230238
uint16_t height_out;
231-
tft.getTextBounds(text,x1,y1,&x1_out,&y1_out,&width_out,&height_out);
232-
x1 += (w - ( width_out + (x1_out-x1)))/2;
233-
y1 += h - (h - height_out)/2;
234-
displayRawText(text,x1,y1,color,background);
239+
tft.getTextBounds(text,x1,y1,&x1_out,&y1_out,&width_out,&height_out,w);
240+
x1 += (w - ( (int32_t)width_out + (x1_out-x1)))/2;
241+
y1 += (ubitx_font->yAdvance + h - ( (int32_t)height_out + (y1_out-y1)))/2;
242+
displayRawText(text,x1,y1,w,color,background);
235243
}
236244

237245
void setupTouch(){

nano_gui.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ void formatFreq(uint32_t freq, char* buff, uint16_t buff_size);
2323
/* touch functions */
2424
boolean readTouch();
2525

26-
void setupTouch();
2726
void scaleTouch(struct Point *p);
2827

2928
// Color definitions

0 commit comments

Comments
 (0)