diff --git a/ubitx_ui.cpp b/ubitx_ui.cpp index 722484b..907fbb2 100644 --- a/ubitx_ui.cpp +++ b/ubitx_ui.cpp @@ -4,68 +4,147 @@ #include "ubitx.h" #include "nano_gui.h" -#define BUTTON_SELECTED 1 +static const unsigned int COLOR_TEXT = DISPLAY_WHITE; +static const unsigned int COLOR_BACKGROUND = DISPLAY_BLACK; + +static const unsigned int COLOR_ACTIVE_VFO_TEXT = DISPLAY_WHITE; +static const unsigned int COLOR_ACTIVE_VFO_BACKGROUND = DISPLAY_DARKGREY; + +static const unsigned int COLOR_INACTIVE_VFO_TEXT = DISPLAY_GREEN; +static const unsigned int COLOR_INACTIVE_VFO_BACKGROUND = DISPLAY_DARKGREY; + +static const unsigned int COLOR_INACTIVE_TEXT = DISPLAY_GREEN; +static const unsigned int COLOR_INACTIVE_BACKGROUND = DISPLAY_DARKGREY; +static const unsigned int COLOR_INACTIVE_BORDER = DISPLAY_DARKGREY; + +static const unsigned int COLOR_ACTIVE_TEXT = DISPLAY_BLACK; +static const unsigned int COLOR_ACTIVE_BACKGROUND = DISPLAY_ORANGE; +static const unsigned int COLOR_ACTIVE_BORDER = DISPLAY_WHITE; + +static const unsigned int LAYOUT_VFO_LABEL_X = 0; +static const unsigned int LAYOUT_VFO_LABEL_Y = 10; +static const unsigned int LAYOUT_VFO_LABEL_WIDTH = 159; +static const unsigned int LAYOUT_VFO_LABEL_HEIGHT = 36; +static const unsigned int LAYOUT_VFO_LABEL_PITCH_X = 160; + +static const unsigned int LAYOUT_MODE_TEXT_X = 0; +static const unsigned int LAYOUT_MODE_TEXT_Y = 55; +static const unsigned int LAYOUT_MODE_TEXT_WIDTH = 320; +static const unsigned int LAYOUT_MODE_TEXT_HEIGHT = 30; + +static const unsigned int LAYOUT_BUTTON_X = 0; +static const unsigned int LAYOUT_BUTTON_Y = 80; +static const unsigned int LAYOUT_BUTTON_WIDTH = 60; +static const unsigned int LAYOUT_BUTTON_HEIGHT = 36; +static const unsigned int LAYOUT_BUTTON_PITCH_X = 64; +static const unsigned int LAYOUT_BUTTON_PITCH_Y = 40; + +static const unsigned int LAYOUT_CW_TEXT_X = 0; +static const unsigned int LAYOUT_CW_TEXT_Y = 201; +static const unsigned int LAYOUT_CW_TEXT_WIDTH = 320; +static const unsigned int LAYOUT_CW_TEXT_HEIGHT = 39; + +static const unsigned int LAYOUT_TX_X = 280; +static const unsigned int LAYOUT_TX_Y = 48; +static const unsigned int LAYOUT_TX_WIDTH = 37; +static const unsigned int LAYOUT_TX_HEIGHT = 28; + +enum btn_set_e { + BUTTON_VFOA, + BUTTON_VFOB, + BUTTON_RIT, + BUTTON_USB, + BUTTON_LSB, + BUTTON_CW, + BUTTON_SPL, + BUTTON_80, + BUTTON_40, + BUTTON_30, + BUTTON_20, + BUTTON_17, + BUTTON_15, + BUTTON_10, + BUTTON_WPM, + BUTTON_TON, + BUTTON_FRQ, + BUTTON_TOTAL +}; struct Button { int x, y, w, h; - char *text; - char *morse; + unsigned int id; + char text[5]; + char morse; }; -#define MAX_BUTTONS 17 -const struct Button btn_set[MAX_BUTTONS] PROGMEM = { -//const struct Button btn_set [] = { - { 0, 10, 159, 36, "VFOA", "A"}, - {160, 10, 159, 36, "VFOB", "B"}, +constexpr Button btn_set[BUTTON_TOTAL] PROGMEM = { + {LAYOUT_VFO_LABEL_X + 0*LAYOUT_VFO_LABEL_PITCH_X, LAYOUT_VFO_LABEL_Y, LAYOUT_VFO_LABEL_WIDTH, LAYOUT_VFO_LABEL_HEIGHT, BUTTON_VFOA, "VFOA", 'A'}, + {LAYOUT_VFO_LABEL_X + 1*LAYOUT_VFO_LABEL_PITCH_X, LAYOUT_VFO_LABEL_Y, LAYOUT_VFO_LABEL_WIDTH, LAYOUT_VFO_LABEL_HEIGHT, BUTTON_VFOB, "VFOB", 'B'}, - { 0, 80, 60, 36, "RIT", "R"}, - { 64, 80, 60, 36, "USB", "U"}, - {128, 80, 60, 36, "LSB", "L"}, - {192, 80, 60, 36, "CW", "M"}, - {256, 80, 60, 36, "SPL", "S"}, + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_RIT, "RIT", 'R'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_USB, "USB", 'U'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_LSB, "LSB", 'L'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_CW , "CW", 'M'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_SPL, "SPL", 'S'}, - { 0, 120, 60, 36, "80", "8"}, - { 64, 120, 60, 36, "40", "4"}, - {128, 120, 60, 36, "30", "3"}, - {192, 120, 60, 36, "20", "2"}, - {256, 120, 60, 36, "17", "7"}, + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_80, "80", '8'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_40, "40", '4'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_30, "30", '3'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_20, "20", '2'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_17, "17", '7'}, - { 0, 160, 60, 36, "15", "5"}, - { 64, 160, 60, 36, "10", "1"}, - {128, 160, 60, 36, "WPM", "W"}, - {192, 160, 60, 36, "TON", "T"}, - {256, 160, 60, 36, "FRQ", "F"}, + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_15 , "15", '5'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_10 , "10", '1'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_WPM, "WPM", 'W'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_TON, "TON", 'T'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, BUTTON_FRQ, "FRQ", 'F'}, }; -#define MAX_KEYS 17 -const struct Button keypad[MAX_KEYS] PROGMEM = { - { 0, 80, 60, 36, "1", "1"}, - { 64, 80, 60, 36, "2", "2"}, - {128, 80, 60, 36, "3", "3"}, - {192, 80, 60, 36, "", ""}, - {256, 80, 60, 36, "OK", "K"}, +static const unsigned int KEYS_OFFSET = 256;//Unique from buttons +enum keypad_e { + KEYS_1 = KEYS_OFFSET, + KEYS_2, + KEYS_3, + KEYS_BLANK_1, + KEYS_OK, + KEYS_4, + KEYS_5, + KEYS_6, + KEYS_0, + KEYS_BACKSPACE, + KEYS_7, + KEYS_8, + KEYS_9, + KEYS_BLANK_2, + KEYS_CANCEL, + KEYS_TOTAL = KEYS_CANCEL - KEYS_OFFSET + 1 +}; +constexpr Button keypad[KEYS_TOTAL] PROGMEM = { + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_1, "1", '1'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_2, "2", '2'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_3, "3", '3'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_BLANK_1, "", '\0'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 0*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_OK, "OK", 'K'}, - { 0, 120, 60, 36, "4", "4"}, - { 64, 120, 60, 36, "5", "5"}, - {128, 120, 60, 36, "6", "6"}, - {192, 120, 60, 36, "0", "0"}, - {256, 120, 60, 36, "<-", "B"}, + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_4, "4", '4'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_5, "5", '5'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_6, "6", '6'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_0, "0", '0'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 1*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_BACKSPACE, "<-", 'B'}, - { 0, 160, 60, 36, "7", "7"}, - { 64, 160, 60, 36, "8", "8"}, - {128, 160, 60, 36, "9", "9"}, - {192, 160, 60, 36, "", ""}, - {256, 160, 60, 36, "Can", "C"}, + {LAYOUT_BUTTON_X + 0*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_7, "7", '7'}, + {LAYOUT_BUTTON_X + 1*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_8, "8", '8'}, + {LAYOUT_BUTTON_X + 2*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_9, "9", '9'}, + {LAYOUT_BUTTON_X + 3*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_BLANK_2, "", '\0'}, + {LAYOUT_BUTTON_X + 4*LAYOUT_BUTTON_PITCH_X, LAYOUT_BUTTON_Y + 2*LAYOUT_BUTTON_PITCH_Y, LAYOUT_BUTTON_WIDTH, LAYOUT_BUTTON_HEIGHT, KEYS_CANCEL, "Can", 'C'}, }; -boolean getButton(char *text, struct Button *b){ - for (int i = 0; i < MAX_BUTTONS; i++){ - memcpy_P(b, btn_set + i, sizeof(struct Button)); - if (!strcmp(text, b->text)){ - return true; - } +boolean getButton(btn_set_e index, Button* button){ + if(BUTTON_TOTAL == index){ + return false; } - return false; + memcpy_P(button, &(btn_set[index]), sizeof(Button)); + return true; } @@ -84,60 +163,59 @@ void formatFreq(long f, char *buff) { //one mhz digit if less than 10 M, two digits if more if (f < 10000000l){ buff[0] = ' '; - strncat(buff, b, 4); + strncat(buff, b, 4); strcat(buff, "."); strncat(buff, &b[4], 2); } else { strncat(buff, b, 5); strcat(buff, "."); - strncat(buff, &b[5], 2); + strncat(buff, &b[5], 2); } } -void drawCommandbar(char *text){ - displayFillrect(30,45,280, 32, DISPLAY_NAVY); - displayRawText(text, 30, 45, DISPLAY_WHITE, DISPLAY_NAVY); +inline void drawCommandbar(char* text){ + displayText(text, LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); } /** A generic control to read variable values */ int getValueByKnob(int minimum, int maximum, int step_size, int initial, char* prefix, char *postfix) { - int knob = 0; - int knob_value; + int knob = 0; + int knob_value; - while (btnDown()) - active_delay(100); + while (btnDown()) + active_delay(100); - active_delay(200); - knob_value = initial; - - strcpy(b, prefix); - itoa(knob_value, c, 10); - strcat(b, c); - strcat(b, postfix); - drawCommandbar(b); + active_delay(200); + knob_value = initial; - while(!btnDown() && digitalRead(PTT) == HIGH){ + strcpy(b, prefix); + itoa(knob_value, c, 10); + strcat(b, c); + strcat(b, postfix); + drawCommandbar(b); - knob = enc_read(); - if (knob != 0){ - if (knob_value > minimum && knob < 0) - knob_value -= step_size; - if (knob_value < maximum && knob > 0) - knob_value += step_size; - - strcpy(b, prefix); - itoa(knob_value, c, 10); - strcat(b, c); - strcat(b, postfix); - drawCommandbar(b); - } - checkCAT(); + while(!btnDown() && digitalRead(PTT) == HIGH){ + knob = enc_read(); + if (knob != 0){ + if (knob_value > minimum && knob < 0) + knob_value -= step_size; + if (knob_value < maximum && knob > 0) + knob_value += step_size; + + strcpy(b, prefix); + itoa(knob_value, c, 10); + strcat(b, c); + strcat(b, postfix); + drawCommandbar(b); } - displayFillrect(30,41,280, 32, DISPLAY_NAVY); - return knob_value; + checkCAT(); + } + b[0] = 0; + drawCommandbar(b); + return knob_value; } void printCarrierFreq(unsigned long freq){ @@ -152,138 +230,168 @@ void printCarrierFreq(unsigned long freq){ strncat(c, &b[2], 3); strcat(c, "."); strncat(c, &b[5], 1); - displayText(c, 110, 100, 100, 30, DISPLAY_CYAN, DISPLAY_NAVY, DISPLAY_NAVY); + displayText(c, 110, 100, 100, 30, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); } void displayDialog(char *title, char *instructions){ - displayClear(DISPLAY_BLACK); - displayRect(10,10,300,220, DISPLAY_WHITE); - displayHline(20,45,280,DISPLAY_WHITE); - displayRect(12,12,296,216, DISPLAY_WHITE); - displayRawText(title, 20, 20, DISPLAY_CYAN, DISPLAY_NAVY); - displayRawText(instructions, 20, 200, DISPLAY_CYAN, DISPLAY_NAVY); + displayClear(COLOR_BACKGROUND); + displayRect(10,10,300,220, COLOR_TEXT); + displayHline(20,45,280,COLOR_TEXT); + displayRect(12,12,296,216, COLOR_TEXT); + displayRawText(title, 20, 20, COLOR_TEXT, COLOR_BACKGROUND); + displayRawText(instructions, 20, 200, COLOR_TEXT, COLOR_BACKGROUND); } - - - -char vfoDisplay[12]; void displayVFO(int vfo){ int x, y; - int displayColor, displayBorder; - Button b; + int displayColor, displayBackground, displayBorder; + Button button; + + if (splitOn){ + if (vfoActive == vfo){ + c[0] = 'R'; + } + else{ + c[0] = 'T'; + } + } + else{ + if(VFO_A == vfo){ + c[0] = 'A'; + } + else if(VFO_B == vfo){ + c[0] = 'B'; + } + else{ + c[0] = '?'; + } + } + c[1] = ':'; + if (vfo == VFO_A){ - getButton("VFOA", &b); - if (splitOn){ - if (vfoActive == VFO_A) - strcpy(c, "R:"); - else - strcpy(c, "T:"); - } - else - strcpy(c, "A:"); + getButton(BUTTON_VFOA, &button); + if (vfoActive == VFO_A){ formatFreq(frequency, c+2); - displayColor = DISPLAY_WHITE; - displayBorder = DISPLAY_BLACK; + displayColor = COLOR_ACTIVE_VFO_TEXT; + displayBackground = COLOR_ACTIVE_VFO_BACKGROUND; + displayBorder = COLOR_ACTIVE_BORDER; }else{ formatFreq(vfoA, c+2); - displayColor = DISPLAY_GREEN; - displayBorder = DISPLAY_BLACK; + displayColor = COLOR_INACTIVE_VFO_TEXT; + displayBackground = COLOR_INACTIVE_VFO_BACKGROUND; + displayBorder = COLOR_INACTIVE_BORDER; } } if (vfo == VFO_B){ - getButton("VFOB", &b); + getButton(BUTTON_VFOB, &button); - if (splitOn){ - if (vfoActive == VFO_B) - strcpy(c, "R:"); - else - strcpy(c, "T:"); - } - else - strcpy(c, "B:"); if (vfoActive == VFO_B){ formatFreq(frequency, c+2); - displayColor = DISPLAY_WHITE; - displayBorder = DISPLAY_WHITE; + displayColor = COLOR_ACTIVE_VFO_TEXT; + displayBackground = COLOR_ACTIVE_VFO_BACKGROUND; + displayBorder = COLOR_ACTIVE_BORDER; } else { - displayColor = DISPLAY_GREEN; - displayBorder = DISPLAY_BLACK; formatFreq(vfoB, c+2); + displayColor = COLOR_INACTIVE_VFO_TEXT; + displayBackground = COLOR_INACTIVE_VFO_BACKGROUND; + displayBorder = COLOR_INACTIVE_BORDER; } } - if (vfoDisplay[0] == 0){ - displayFillrect(b.x, b.y, b.w, b.h, DISPLAY_BLACK); - if (vfoActive == vfo) - displayRect(b.x, b.y, b.w , b.h, DISPLAY_WHITE); - else - displayRect(b.x, b.y, b.w , b.h, DISPLAY_NAVY); - } - x = b.x + 6; - y = b.y + 3; - - char *text = c; - - for (int i = 0; i <= strlen(c); i++){ - char digit = c[i]; - if (digit != vfoDisplay[i]){ - - displayFillrect(x, y, 15, b.h-6, DISPLAY_BLACK); - //checkCAT(); - - displayChar(x, y + TEXT_LINE_HEIGHT + 3, digit, displayColor, DISPLAY_BLACK); - checkCAT(); - } - if (digit == ':' || digit == '.') - x += 7; - else - x += 16; - text++; - }//end of the while loop of the characters to be printed - - strcpy(vfoDisplay, c); + displayText(c, button.x, button.y, button.w, button.h, displayColor, displayBackground, displayBorder); } -void btnDraw(struct Button *b){ - if (!strcmp(b->text, "VFOA")){ - memset(vfoDisplay, 0, sizeof(vfoDisplay)); - displayVFO(VFO_A); - } - else if(!strcmp(b->text, "VFOB")){ - memset(vfoDisplay, 0, sizeof(vfoDisplay)); - displayVFO(VFO_B); - } - else if ((!strcmp(b->text, "RIT") && ritOn == 1) || - (!strcmp(b->text, "USB") && isUSB == 1) || - (!strcmp(b->text, "LSB") && isUSB == 0) || - (!strcmp(b->text, "SPL") && splitOn == 1)) - displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_DARKGREY); - else if (!strcmp(b->text, "CW") && cwMode == 1) - displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_DARKGREY); - else - displayText(b->text, b->x, b->y, b->w, b->h, DISPLAY_GREEN, DISPLAY_BLACK, DISPLAY_DARKGREY); +void btnDrawActive(Button* button){ + displayText(button->text, button->x, button->y, button->w, button->h, COLOR_ACTIVE_TEXT, COLOR_ACTIVE_BACKGROUND, COLOR_INACTIVE_BORDER); +} +void btnDrawInactive(Button* button){ + displayText(button->text, button->x, button->y, button->w, button->h, COLOR_INACTIVE_TEXT, COLOR_INACTIVE_BACKGROUND, COLOR_INACTIVE_BORDER); +} + +void btnDraw(struct Button *button){ + switch(button->id){ + case BUTTON_VFOA: + { + displayVFO(VFO_A); + break; + } + case BUTTON_VFOB: + { + displayVFO(VFO_B); + break; + } + case BUTTON_RIT: + { + if(1 == ritOn){ + btnDrawActive(button); + } + else{ + btnDrawInactive(button); + } + break; + } + case BUTTON_USB: + { + if(1 == isUSB){ + btnDrawActive(button); + } + else{ + btnDrawInactive(button); + } + break; + } + case BUTTON_LSB: + { + if(0 == isUSB){ + btnDrawActive(button); + } + else{ + btnDrawInactive(button); + } + break; + } + case BUTTON_SPL: + { + if(1 == splitOn){ + btnDrawActive(button); + } + else{ + btnDrawInactive(button); + } + break; + } + case BUTTON_CW: + { + if(1 == cwMode){ + btnDrawActive(button); + } + else{ + btnDrawInactive(button); + } + break; + } + default: + { + btnDrawInactive(button); + break; + } + }//End switch } void displayRIT(){ - displayFillrect(0,41,320,30, DISPLAY_NAVY); + c[0] = 0; + displayFillrect(LAYOUT_MODE_TEXT_X,LAYOUT_MODE_TEXT_Y,LAYOUT_MODE_TEXT_WIDTH,LAYOUT_MODE_TEXT_HEIGHT, COLOR_BACKGROUND); if (ritOn){ - strcpy(c, "TX:"); + strcpy_P(c,(const char*)F("TX:")); formatFreq(ritTxFrequency, c+3); if (vfoActive == VFO_A) - displayText(c, 0, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY); + displayText(c, LAYOUT_MODE_TEXT_X + 0*LAYOUT_VFO_LABEL_PITCH_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); else - displayText(c, 160, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY); - } - else { - if (vfoActive == VFO_A) - displayText("", 0, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY); - else - displayText("", 160, 45,159, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY); + displayText(c, LAYOUT_MODE_TEXT_X + 1*LAYOUT_VFO_LABEL_PITCH_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); } } @@ -295,13 +403,13 @@ void fastTune(){ active_delay(50); active_delay(300); - displayRawText("Fast tune", 100, 55, DISPLAY_CYAN, DISPLAY_NAVY); + displayText("Fast tune", LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); while(1){ checkCAT(); //exit after debouncing the btnDown if (btnDown()){ - displayFillrect(100, 55, 120, 30, DISPLAY_NAVY); + displayFillrect(LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_BACKGROUND); //wait until the button is realsed and then return while(btnDown()) @@ -328,10 +436,10 @@ void enterFreq(){ //display all the buttons int f; - for (int i = 0; i < MAX_KEYS; i++){ - struct Button b; - memcpy_P(&b, keypad + i, sizeof(struct Button)); - btnDraw(&b); + for (int i = 0; i < KEYS_TOTAL; i++){ + Button button; + memcpy_P(&button, &(keypad[i]), sizeof(Button)); + btnDraw(&button); } int cursor_pos = 0; @@ -346,17 +454,18 @@ void enterFreq(){ scaleTouch(&ts_point); - int total = sizeof(btn_set)/sizeof(struct Button); - for (int i = 0; i < MAX_KEYS; i++){ - struct Button b; - memcpy_P(&b, keypad + i, sizeof(struct Button)); + for (int i = 0; i < KEYS_TOTAL; i++){ + Button button; + memcpy_P(&button, &(keypad[i]), sizeof(Button)); - int x2 = b.x + b.w; - int y2 = b.y + b.h; + int x2 = button.x + button.w; + int y2 = button.y + button.h; - if (b.x < ts_point.x && ts_point.x < x2 && - b.y < ts_point.y && ts_point.y < y2){ - if (!strcmp(b.text, "OK")){ + if(button.x < ts_point.x && ts_point.x < x2 && + button.y < ts_point.y && ts_point.y < y2){ + switch(button.id){ + case KEYS_OK: + { long f = atol(c); if(30000 >= f && f > 100){ frequency = f * 1000l; @@ -369,31 +478,55 @@ void enterFreq(){ } guiUpdate(); return; + break; } - else if (!strcmp(b.text, "<-")){ + + case KEYS_BACKSPACE: + { c[cursor_pos] = 0; - if (cursor_pos > 0) - cursor_pos--; + if (cursor_pos > 0){ + cursor_pos--; + } c[cursor_pos] = 0; + break; } - else if (!strcmp(b.text, "Can")){ + case KEYS_CANCEL: + { guiUpdate(); return; + break; } - else if('0' <= b.text[0] && b.text[0] <= '9'){ - c[cursor_pos++] = b.text[0]; + case KEYS_0: + case KEYS_1: + case KEYS_2: + case KEYS_3: + case KEYS_4: + case KEYS_5: + case KEYS_6: + case KEYS_7: + case KEYS_8: + case KEYS_9: + { + c[cursor_pos++] = button.text[0]; c[cursor_pos] = 0; + break; } - } - } // end of the button scanning loop + default: + { + //Serial.print(F("Unknown key id: ")); + //Serial.println(button.id); + break; + } + }//switch + }//if button hit test + }// end of the button scanning loop strcpy(b, c); strcat(b, " KHz"); - displayText(b, 0, 42, 320, 30, DISPLAY_WHITE, DISPLAY_NAVY, DISPLAY_NAVY); + displayText(b, LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); delay(300); while(readTouch()) checkCAT(); } // end of event loop : while(1) - } void drawCWStatus(){ @@ -405,15 +538,15 @@ void drawCWStatus(){ itoa(sideTone, c, 10); strcat(b, c); strcat(b, "hz"); - displayText(b, 0, 201, 320, 39, DISPLAY_CYAN, DISPLAY_NAVY, DISPLAY_NAVY); + displayText(b, LAYOUT_CW_TEXT_X, LAYOUT_CW_TEXT_Y, LAYOUT_CW_TEXT_WIDTH, LAYOUT_CW_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND); } void drawTx(){ if (inTx) - displayText("TX", 280, 48, 37, 28, DISPLAY_BLACK, DISPLAY_ORANGE, DISPLAY_BLUE); + displayText("TX", LAYOUT_TX_X, LAYOUT_TX_Y, LAYOUT_TX_WIDTH, LAYOUT_TX_HEIGHT, COLOR_ACTIVE_TEXT, COLOR_ACTIVE_BACKGROUND, COLOR_BACKGROUND); else - displayFillrect(280, 48, 37, 28, DISPLAY_NAVY); + displayFillrect(LAYOUT_TX_X, LAYOUT_TX_Y, LAYOUT_TX_WIDTH, LAYOUT_TX_HEIGHT, COLOR_BACKGROUND); } void drawStatusbar(){ drawCWStatus(); @@ -426,13 +559,10 @@ void guiUpdate(){ return; */ // use the current frequency as the VFO frequency for the active VFO - displayClear(DISPLAY_NAVY); + displayClear(COLOR_BACKGROUND); - memset(vfoDisplay, 0, 12); displayVFO(VFO_A); - checkCAT(); - memset(vfoDisplay, 0, 12); - displayVFO(VFO_B); + displayVFO(VFO_B); checkCAT(); displayRIT(); @@ -440,14 +570,14 @@ void guiUpdate(){ //force the display to refresh everything //display all the buttons - for (int i = 0; i < MAX_BUTTONS; i++){ - struct Button b; - memcpy_P(&b, btn_set + i, sizeof(struct Button)); - btnDraw(&b); + for (int i = 0; i < BUTTON_TOTAL; i++){ + Button button; + memcpy_P(&button, &(btn_set[i]), sizeof(Button)); + btnDraw(&button); checkCAT(); } drawStatusbar(); - checkCAT(); + checkCAT(); } @@ -520,60 +650,56 @@ int enc_read(void) { return(result); } -void ritToggle(struct Button *b){ +void ritToggle(struct Button *button){ if (ritOn == 0){ ritEnable(frequency); } else ritDisable(); - btnDraw(b); + btnDraw(button); displayRIT(); } -void splitToggle(struct Button *b){ +void splitToggle(Button *button){ if (splitOn) splitOn = 0; else splitOn = 1; - btnDraw(b); + btnDraw(button); //disable rit as well ritDisable(); - struct Button b2; - getButton("RIT", &b2); - btnDraw(&b2); + Button button2; + getButton(BUTTON_RIT, &button2); + btnDraw(&button2); displayRIT(); - memset(vfoDisplay, 0, sizeof(vfoDisplay)); displayVFO(VFO_A); - memset(vfoDisplay, 0, sizeof(vfoDisplay)); - displayVFO(VFO_B); + displayVFO(VFO_B); } void vfoReset(){ - Button b; + Button button; if (vfoActive = VFO_A) vfoB = vfoA; else vfoA = vfoB; if (splitOn){ - getButton("SPL", &b); - splitToggle(&b); + getButton(BUTTON_SPL, &button); + splitToggle(&button); } if (ritOn){ - getButton("RIT", &b); - ritToggle(&b); + getButton(BUTTON_RIT, &button); + ritToggle(&button); } - memset(vfoDisplay, 0, sizeof(vfoDisplay)); displayVFO(VFO_A); - memset(vfoDisplay, 0, sizeof(vfoDisplay)); - displayVFO(VFO_B); + displayVFO(VFO_B); saveVFOs(); } @@ -589,39 +715,36 @@ void cwToggle(struct Button *b){ btnDraw(b); } -void sidebandToggle(struct Button *b){ - if (!strcmp(b->text, "LSB")) +void sidebandToggle(Button* button){ + if(BUTTON_LSB == button->id) isUSB = 0; else isUSB = 1; - struct Button e; - getButton("USB", &e); - btnDraw(&e); - getButton("LSB", &e); - btnDraw(&e); + struct Button button2; + getButton(BUTTON_USB, &button2); + btnDraw(&button2); + getButton(BUTTON_LSB, &button2); + btnDraw(&button2); saveVFOs(); } void redrawVFOs(){ - - struct Button b; + struct Button button; ritDisable(); - getButton("RIT", &b); - btnDraw(&b); + getButton(BUTTON_RIT, &button); + btnDraw(&button); displayRIT(); - memset(vfoDisplay, 0, sizeof(vfoDisplay)); displayVFO(VFO_A); - memset(vfoDisplay, 0, sizeof(vfoDisplay)); displayVFO(VFO_B); //draw the lsb/usb buttons, the sidebands might have changed - getButton("LSB", &b); - btnDraw(&b); - getButton("USB", &b); - btnDraw(&b); + getButton(BUTTON_LSB, &button); + btnDraw(&button); + getButton(BUTTON_USB, &button); + btnDraw(&button); } @@ -694,64 +817,116 @@ void setCwTone(){ //save the setting EEPROM.put(CW_SIDETONE, sideTone); - displayFillrect(30,41,280, 32, DISPLAY_NAVY); + b[0] = 0; + drawCommandbar(b); drawStatusbar(); // printLine2(""); // updateDisplay(); } -void doCommand(struct Button *b){ - - if (!strcmp(b->text, "RIT")) - ritToggle(b); - else if (!strcmp(b->text, "LSB")) - sidebandToggle(b); - else if (!strcmp(b->text, "USB")) - sidebandToggle(b); - else if (!strcmp(b->text, "CW")) - cwToggle(b); - else if (!strcmp(b->text, "SPL")) - splitToggle(b); - else if (!strcmp(b->text, "VFOA")){ - if (vfoActive == VFO_A) - fastTune(); - else - switchVFO(VFO_A); +void doCommand(Button* button){ + switch(button->id){ + case BUTTON_RIT: + { + ritToggle(button); + break; + } + case BUTTON_LSB: + case BUTTON_USB: + { + sidebandToggle(button); + break; + } + case BUTTON_CW: + { + cwToggle(button); + break; + } + case BUTTON_SPL: + { + splitToggle(button); + break; + } + case BUTTON_VFOA: + { + if(VFO_A == vfoActive){ + fastTune(); + } + else{ + switchVFO(VFO_A); + } + break; + } + case BUTTON_VFOB: + { + if(VFO_B == vfoActive){ + fastTune(); + } + else{ + switchVFO(VFO_B); + } + break; + } + case BUTTON_80: + { + switchBand(3500000L); + break; + } + case BUTTON_40: + { + switchBand(7000000L); + break; + } + case BUTTON_30: + { + switchBand(10000000L); + break; + } + case BUTTON_20: + { + switchBand(14000000L); + break; + } + case BUTTON_17: + { + switchBand(18000000L); + break; + } + case BUTTON_15: + { + switchBand(21000000L); + break; + } + case BUTTON_10: + { + switchBand(28000000L); + break; + } + case BUTTON_FRQ: + { + enterFreq(); + break; + } + case BUTTON_WPM: + { + setCwSpeed(); + break; + } + case BUTTON_TON: + { + setCwTone(); + break; + } + default: + { + //Serial.print(F("Unknown command: ")); + //Serial.println(button.id); + break; + } } - else if (!strcmp(b->text, "VFOB")){ - if (vfoActive == VFO_B) - fastTune(); - else - switchVFO(VFO_B); - } - else if (!strcmp(b->text, "A=B")) - vfoReset(); - else if (!strcmp(b->text, "80")) - switchBand(3500000l); - else if (!strcmp(b->text, "40")) - switchBand(7000000l); - else if (!strcmp(b->text, "30")) - switchBand(10000000l); - else if (!strcmp(b->text, "20")) - switchBand(14000000l); - else if (!strcmp(b->text, "17")) - switchBand(18000000l); - else if (!strcmp(b->text, "15")) - switchBand(21000000l); - else if (!strcmp(b->text, "13")) - switchBand(24800000l); - else if (!strcmp(b->text, "10")) - switchBand(28000000l); - else if (!strcmp(b->text, "FRQ")) - enterFreq(); - else if (!strcmp(b->text, "WPM")) - setCwSpeed(); - else if (!strcmp(b->text, "TON")) - setCwTone(); } void checkTouch(){ - if (!readTouch()) return; @@ -762,17 +937,16 @@ void checkTouch(){ /* //debug code Serial.print(ts_point.x); Serial.print(' ');Serial.println(ts_point.y); */ - int total = sizeof(btn_set)/sizeof(struct Button); - for (int i = 0; i < MAX_BUTTONS; i++){ - struct Button b; - memcpy_P(&b, btn_set + i, sizeof(struct Button)); + for (int i = 0; i < BUTTON_TOTAL; i++){ + Button button; + memcpy_P(&button, &(btn_set[i]), sizeof(Button)); - int x2 = b.x + b.w; - int y2 = b.y + b.h; + int x2 = button.x + button.w; + int y2 = button.y + button.h; - if (b.x < ts_point.x && ts_point.x < x2 && - b.y < ts_point.y && ts_point.y < y2) - doCommand(&b); + if(button.x < ts_point.x && ts_point.x < x2 && + button.y < ts_point.y && ts_point.y < y2) + doCommand(&button); } } @@ -786,10 +960,10 @@ int btnDown(){ void drawFocus(int ibtn, int color){ - struct Button b; + Button button; - memcpy_P(&b, btn_set + ibtn, sizeof(struct Button)); - displayRect(b.x, b.y, b.w, b.h, color); + memcpy_P(&button, &(btn_set[ibtn]), sizeof(Button)); + displayRect(button.x, button.y, button.w, button.h, color); } void doCommands(){ @@ -807,22 +981,22 @@ void doCommands(){ //check if the knob's button was pressed btnState = btnDown(); if (btnState){ - struct Button b; - memcpy_P(&b, btn_set + select/10, sizeof(struct Button)); + Button button; + memcpy_P(&button, &(btn_set[select/10]), sizeof(Button)); - doCommand(&b); + doCommand(&button); //unfocus the buttons - drawFocus(select, DISPLAY_BLUE); + drawFocus(select, COLOR_INACTIVE_BORDER); if (vfoActive == VFO_A) - drawFocus(0, DISPLAY_WHITE); + drawFocus(BUTTON_VFOA, COLOR_ACTIVE_BORDER); else - drawFocus(1, DISPLAY_WHITE); + drawFocus(BUTTON_VFOB, COLOR_ACTIVE_BORDER); //wait for the button to be up and debounce while(btnDown()) active_delay(100); - active_delay(500); + active_delay(500); return; } @@ -834,7 +1008,7 @@ void doCommands(){ } if (i > 0){ - if (select + i < MAX_BUTTONS * 10) + if (select + i < BUTTON_TOTAL * 10) select += i; } if (i < 0 && select + i >= 0) @@ -844,8 +1018,8 @@ void doCommands(){ continue; //we are on a new button - drawFocus(prevButton, DISPLAY_BLUE); - drawFocus(select/10, DISPLAY_WHITE); + drawFocus(prevButton, COLOR_INACTIVE_BORDER); + drawFocus(select/10, COLOR_ACTIVE_BORDER); prevButton = select/10; } // guiUpdate();