Release 1.01

Dependents:   mbed_escm2000

EditAddressMenu.cpp

Committer:
foxbrianr
Date:
2019-09-12
Revision:
4:7226c43320b5
Child:
5:9f4d4f8ffc00

File content as of revision 4:7226c43320b5:

#include "mbed.h"
#include "EditAddressMenu.h"
#include "TimeUtilities.h"
#include "ESCMControlApp.h"

#define DEFAULT_DISPLAY 0
#define SELECT_ADDRESS  1
#define EDIT_ADDRESS    2


string valid_characters = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 " };

EditAddressMenu::EditAddressMenu(char* id): Menu(id)
{

    active_selection = 0;
    active_address   = 0;
    active_position  = 0;

    row=0;
    column=0;


}



void EditAddressMenu::init()
{
    active_selection = 0;
    active_address   = 0;
    active_position  = 0;
    update_needed    = 1;
    
    top = active_address;
    bottom  = active_address +2;
}

void EditAddressMenu::display(LCD * lcd) 
{
    // paging
    if (active_address < top )
    {
        top    = active_address ;
        bottom = active_address + 2;
        update_needed=1;
    }
    else if (active_address > bottom)
    {
        top    = active_address - 2;
        bottom = active_address;
        update_needed=1;
    } 
    else
    {
        
    }
    
    
    if (update_needed) {
        
        lcd->cls();
        switch(active_selection) {
            // -----------------------------------------------------------------
            //Select Address
            // -----------------------------------------------------------------
            case SELECT_ADDRESS:
                {       
                    lcd->locate(0,0);
                    lcd->printf("Select Address :");
                    lcd->locate(1,0);
                    
                    Address *addr = &addressMap.addresses[active_address];
                    
                    lcd->printf(" %02d | %-20s", 
                        addr->address ,  
                        addr->description );
                }
                break;
                
            // -----------------------------------------------------------------
            // Select Char
            // -----------------------------------------------------------------
            case EDIT_ADDRESS:
            
                {
                    Address *addr = &addressMap.addresses[active_address];
                    
                    lcd->locate(0,0);
                    lcd->printf("Edit Text (%02d):" ,  addr->address  );
                    lcd->locate(1,0);
                    lcd->printf("Current Text : [%-20s]",  addr->description );
                    lcd->locate(2,0);
                    lcd->printf("Updated Text : [%-20s]",  tmp_description );
                    lcd->locate(3,active_position+16);
                    lcd->printf("^" );
                }
                break;
                
            // -----------------------------------------------------------------
            // Display
            // -----------------------------------------------------------------
            default:
#if 1
                lcd->locate(0,0);
                lcd->printf("Address Map:");
#else
                lcd->locate(0,0);
                lcd->printf("index=%d, (%d %d)", active_address, top, bottom);
#endif
                // update display
                for(int i=0; i<3; i++) {

                    int index = top + i;
                    int line  = (1+i);

                    int selected = active_address == index;

                    if(selected) {
                        lcd->locate(line,0);
                        lcd->printf(">");
                    }
                    
                    
                    if (index < MAX_ADDRESSES) 
                    {
                        Address *a = &addressMap.addresses[index];
                        lcd->locate(line,0);
                        lcd->printf("%s%02d | %-20s",
                                    ((selected)?">":" "),
                                    a->address,
                                    a->description  );
                    }
                }
                
                if (strlen(addressMap.addresses[active_address].description) > 0)
                {
                    escmController.say("%s is open" , 
                        addressMap.addresses[active_address].description );
                }
                else
                {
                    escmController.say("Unit %d is open" , 
                        addressMap.addresses[active_address].address );
                }

                break;
        };
        update_needed=0;
    }
    displayCurrentTime(lcd);
}

void EditAddressMenu::pressMode()
{
    // toggle active menu 
    switch(active_selection) {
        case SELECT_ADDRESS:
        
            // copy the current string to an editable copy
            strcpy( tmp_description , 
                addressMap.addresses[active_address].description);
                
            active_selection = EDIT_ADDRESS;
            active_position = 0;
            
            break;
        case EDIT_ADDRESS:
            active_selection = DEFAULT_DISPLAY;
            
            // -------------------------------------------------------
            // save the editable copy back into address map
            strcpy(
                addressMap.addresses[active_address].description
                , tmp_description);
            addressMap.save();    
            // -------------------------------------------------------
            active_position = 0;
            break;
        default:
            active_selection = SELECT_ADDRESS;
            break;
    };

    update_needed = 1;
}

void EditAddressMenu::pressSet()
{
    // set button advances to next character position OR
    // goes back to normal
    switch(active_selection) {
        
        case DEFAULT_DISPLAY:
        case SELECT_ADDRESS:
        
            
            // copy the current string to an editable copy
            strcpy( tmp_description , 
                addressMap.addresses[active_address].description);
                
            active_selection = EDIT_ADDRESS;
            active_position = 0;
            
            break;

        case EDIT_ADDRESS:
            active_position++;
            
            if ( active_position > MAX_ADDR_LENGTH )
            {
                
                // -------------------------------------------------------
                // save the editable copy back into address map
                strcpy(
                    addressMap.addresses[active_address].description
                    , tmp_description);
                
                addressMap.save();    
                // -------------------------------------------------------
                active_position = 0;
            }
            
            break;

        default:
            break;
    };

    update_needed = 1;
}


void EditAddressMenu:: nextValidChar (char * value , int direction)
{
    char c = *value;
    
    size_t index = valid_characters.find(c); 

    if ( index ==  string::npos )
    {
        index = 0;
    }
    else 
    {
        index += direction;
        
        if (index<0) 
            index = valid_characters.size()-1;
        if (index >= valid_characters.size())
            index = 0;
        
    }
    
    *value = valid_characters[index]; 
}


void EditAddressMenu::pressUp()
{
    // either advances the pointer to current object in list OR
    // character in array
    switch(active_selection) {
        case SELECT_ADDRESS:
            active_address--;
            update_needed = 1;
            break;
        case EDIT_ADDRESS:
            nextValidChar (&tmp_description[active_position],1);
            update_needed = 1;
            break;

        default:
            active_address--;
            update_needed = 1;
            break;

    }
    
    LIMIT(active_address  , 0, MAX_ADDRESSES  - 1);
    LIMIT(active_position , 0, MAX_ADDR_LENGTH -1);  

}

void EditAddressMenu::pressDown()
{
    // either advances the pointer to current object in list OR
    // character in array
    switch(active_selection) {
            
        case SELECT_ADDRESS:
            active_address++;
            update_needed = 1;
            break;

        case EDIT_ADDRESS:
            nextValidChar (&tmp_description[active_position],-1);
            //active_position--;
            update_needed = 1;
            break;

        default:
            active_address++;
            update_needed = 1;
            break;

    }
    
    
    
    LIMIT(active_address  , 0, MAX_ADDRESSES  - 1);
    LIMIT(active_position , 0, MAX_ADDR_LENGTH -1);

}