Search code examples
calgorithmembeddedmikroc

Can you help me to do this algorithm "Shifting a String to right or left"


I'm working on a project implementing a clock using PIC18, LCD, ... etc and I am using mikroC to implement this project.

However, I am not good at C and I am stuck at certain point. The clock has a menu with several options that will enable the user to set time, alarms, alarm sound, etc. The menu has the following:

1. Set Time
2. Add Alarm
3. Select Alarm
4. Add New Tone
5. Select Tone
6. EXIT

The clock has 3 pushbuttons, OK,RIGHT, and LEFT. When the clock power-on, it will show Set Time on LCD as default. I want to add a feature that when I press RIGHT pushbutton it should show Add Alarm but gradually not directly. I have 6 items in the menu so I have to do this gradually movement 12 times (6 for right and 6 for left). I have tried this as follows:

 Lcd_Out(2, 2, "   set Time   ");
 Delay_ms(50);
 Lcd_Out(2, 2, "  set Time    ");
 Delay_ms(50);
 Lcd_Out(2, 2, " set Time     ");
 Delay_ms(50);
 Lcd_Out(2, 2, " et Time      ");
 Delay_ms(50);
 Lcd_Out(2, 2, " t Time       ");
 Delay_ms(50);
 Lcd_Out(2, 2, " Time         ");
 Delay_ms(50);
 Lcd_Out(2, 2, " ime          ");
 Delay_ms(50);
 Lcd_Out(2, 2, " me           ");
 Delay_ms(50);
 Lcd_Out(2, 2, " e            ");
 Delay_ms(50);
 Lcd_Out(2, 2, "              ");
 Delay_ms(50);
 Lcd_Out(2, 2, "              ");
 Delay_ms(50);
 Lcd_Out(2, 2, "              ");
 Delay_ms(50);
 Lcd_Out(2, 2, "              ");
 Delay_ms(50);
 Lcd_Out(2, 2, "            A ");
 Delay_ms(50);
 Lcd_Out(2, 2, "           Ad ");
 Delay_ms(50);
 Lcd_Out(2, 2, "          Add ");
 Delay_ms(50);
 Lcd_Out(2, 2, "         Add  ");
 Delay_ms(50);
 Lcd_Out(2, 2, "        Add A ");
 Delay_ms(50);
 Lcd_Out(2, 2, "       Add Al ");
 Delay_ms(50);
 Lcd_Out(2, 2, "      Add Ala ");
 Delay_ms(50);
 Lcd_Out(2, 2, "     Add Alar ");
 Delay_ms(50);
 Lcd_Out(2, 2, "    Add Alarm ");
 Delay_ms(50);
 Lcd_Out(2, 2, "   Add Alarm  ");
 Delay_ms(50);
 Lcd_Out(2, 2, "  Add Alarm   ");

This was one movement and to do the others I need large code while the RAM of the PIC is limited. So, Can you guys help me solving this issue?


Solution

  • Here a variation avoiding all the memcpy, str*cpy orgy.

    #define STRSZ 14
    
    char str[STRZ*2+1] = "   set Time     Add Alarm   ";  /* The buffer must be writable */
    
    for (i = 0; i <= STR_SZ; i++) {            // Loop
      char save_ch = str[i + STRZ];            // Save the character at the end 
      str[i + STRZ] = 0;                       // Terminate the string
      Lcd_Out (2, 2, str + i);                 
      str[i + STRZ] = save_ch;                 // Restore buffer
      Delay_ms (50);
    }
    

    EDIT: shift to right

    for (i = STR_SZ; i >= 0; i--) {            // Loop
      char save_ch = str[i + STRZ];            // Save the character at the end 
      str[i + STRZ] = 0;                       // Terminate the string
      Lcd_Out (2, 2, str + i);                 
      str[i + STRZ] = save_ch;                 // Restore buffer
      Delay_ms (50);
    }
    

    On very small devices, avoiding unnecessary memory moves can be crucial