Warning: Illegal string offset 'name' in [path]/includes/functions.php on line 6439
C++ -
+
1 2 3

thread: C++

  1. #21
    Senior Member
    Mar 2011
    1,119

    Lasu un klusībā ceru ka es arī kādreiz iebraukšu šajās lietās. Pagaidām ,gan vēl pa "smilšu kasti" vien rušinos. Kaut kas reizēm arī sanāk

  2. #22
    Senior Member
    Nov 2009
    Jēkabpils
    2,046

    Zin, teikšu kā ir.Šitā lieta ievelk. Un, ja tas, diemžēl, nav maizesdarbs, tad reizēm process kļūst svarīgāks par rezultātu . It kā ir daudz "meņušku" atmegām GitHubā un citur, bet man vajadzēja uzrakstīt savu. Ar enkoderu vai trim pogām. Un lai viss iespējamais glabājas iekš PROGMEM.
    :
    #include <avr/pgmspace.h>
    #include "MenuTree.h"
    
    enum button_t
    {
      BTN_NEXT   = 1,
      BTN_PREV   = 2,
      BTN_ENTER  = 3,
    };
    
    #define MENU_LEVEL_COUNT  4
    #define LCD_ROW_COUNT 4
     
    uint8_t activeItem = 0;  
    uint8_t Level = 0;
    
    uint8_t LevelSelectedItem[MENU_LEVEL_COUNT] = {N, N, N, N};
    uint8_t LevelCursorPos[MENU_LEVEL_COUNT] = {0, 0, 0, 0};
    uint8_t LevelFirstItem[MENU_LEVEL_COUNT] = {0, 0, 0, 0};
    
    uint8_t ScreenItems[LCD_ROW_COUNT];
     
    uint8_t ScreenCursorPos = N;
    
    uint8_t getNext(uint8_t item){return pgm_read_byte(&MenuItems[item].next);}
    uint8_t getPrev(uint8_t item){return pgm_read_byte(&MenuItems[item].prev);}
    uint8_t getChild(uint8_t Item){return pgm_read_byte(&MenuItems[Item].child);}
    uint8_t getParent(uint8_t Item){return pgm_read_byte(&MenuItems[Item].parent);}
    
    void printItemTitle(uint8_t Item)
    {
      const char *iname = (const char*) pgm_read_word(&title[Item]);
      while (char cchar = pgm_read_byte(iname++)) Serial.print((char)cchar);
      if(getChild(Item) < N)
        Serial.println(" > ");
      else
        Serial.println("");
    }
    
    void renderMenu(button_t button)
    {
        uint8_t X = 0;
        uint8_t i;
        if(LevelSelectedItem[Level] == activeItem)
        {
            switch (button)
            {
              case BTN_NEXT:
                  if(getChild(activeItem) == N)
                  {
                      //pagaidām nedaram neko
                  }
                  else
                  {
                      if(Level < MENU_LEVEL_COUNT) Level++;
                      activeItem = getChild(activeItem);
    
                      ScreenCursorPos++;
                  }
                  break;
              case BTN_PREV:
                  if(getPrev(activeItem) == N)
                  {
                      //pagaidām nedaram neko
                  }
                  else
                  {
                      //pagaidām nedaram neko
                  }
                  break;
              case BTN_ENTER:
                  LevelSelectedItem[Level] = N;
    
                  if(activeItem == 0)
                      ScreenCursorPos = N;
                  else
                    ScreenCursorPos = LevelCursorPos[Level];
                
                  ScreenItems[0] = LevelFirstItem[Level];
                  if(activeItem == 0)
                      ScreenItems[1] = getNext(ScreenItems[0]);
                  else
                      ScreenItems[1] = getChild(ScreenItems[0]);
                  for(i = 2; i < LCD_ROW_COUNT; i++)
                  {
                      ScreenItems[i] = getNext(ScreenItems[i-1]);
                  }
                  break;
              default: break;
            }
        }
        else
        {
            switch (button)
            {
              case BTN_NEXT:
                  if(getNext(activeItem) == N)
                  {
                      //pagaidām nedaram neko
                  }
                  else
                  {
                      activeItem = getNext(activeItem);
                      if(ScreenCursorPos < LCD_ROW_COUNT - 1)
                        ScreenCursorPos++;
                      else
                      {     
                          if(ScreenItems[0] == LevelSelectedItem[Level - 1])
                          {
    
                            ScreenItems[0] = getChild(ScreenItems[0]);
                            for(i = 1; i < LCD_ROW_COUNT; i++)
                            {
                                ScreenItems[i] = getNext(ScreenItems[i-1]);
                            }
                        }
                        else
                        {
                            ScreenItems[0] = getNext(ScreenItems[0]);
                            for(i = 1; i < LCD_ROW_COUNT; i++)
                            {
                                ScreenItems[i] = getNext(ScreenItems[i-1]);
                            }
                        }
                      }
                  }
                  break;
              case BTN_PREV:
                  if(getPrev(activeItem) == N)
                  {
                      if(getParent(activeItem) == N)
                      {
                          //Pagaidām nedaram neko;
                      }
                      else
                      {
                          if(Level > 0) Level--;
                          activeItem = getParent(activeItem);
     
                          ScreenCursorPos = 0;
     
                          ScreenItems[0] = activeItem;
                          ScreenItems[1] = getChild(ScreenItems[0]);
                          for(i = 2; i < LCD_ROW_COUNT; i++)
                          {
                              ScreenItems[i] = getNext(ScreenItems[i-1]);
                          }
                      }
                  }
                  else
                  {
                      activeItem = getPrev(activeItem);
    
                      if(ScreenCursorPos > 0)
                        ScreenCursorPos--;
                      else
                      {                    
                        ScreenItems[0] = activeItem;
                        for(i = 1; i < LCD_ROW_COUNT; i++)
                        {
                            ScreenItems[i] = getNext(ScreenItems[i-1]);
                        }
                      }
                  }
                  break;
              case BTN_ENTER:
                  if(getChild(activeItem) < N)
                  {
                      LevelSelectedItem[Level] = activeItem;
                      LevelCursorPos[Level] = ScreenCursorPos;
                      LevelFirstItem[Level] = ScreenItems[0]; //ScreenFirstItem;
        
                      ScreenCursorPos = 0;
                      ScreenItems[0] = activeItem;
                          ScreenItems[1] = getChild(ScreenItems[0]);
                      for(i = 2; i < LCD_ROW_COUNT; i++)
                      {
                      ScreenItems[i] = getNext(ScreenItems[i-1]);
                      }
                  }
                  else
                  {
                        //Pagaidām nedaram neko
                  }
                  break;
              default: break;
            }
        }
    
        for(i = 0; i < LCD_ROW_COUNT; i++)
        {
          if(i == ScreenCursorPos)
          {
             Serial.print(" > ");
             printItemTitle(activeItem);
          }
          else
          {
             Serial.print("   ");
             printItemTitle(ScreenItems[i]);
          }
        }
        Serial.println("");
    }
    
     
    
    void setup() {
      Serial.begin(115200);
      uint8_t i;
      ScreenItems[0] = 0;
      for(i = 1; i < LCD_ROW_COUNT; i++)
      {
        ScreenItems[i] = N;
      }
      for(i = 0; i < LCD_ROW_COUNT; i++)
      {
        if(i == ScreenCursorPos)
          Serial.print(" > ");
        else
          Serial.print("   ");
        printItemTitle(ScreenItems[i]);
      }
      Serial.println("");
    }
    
    void loop() {
      button_t button;
      if (Serial.available())
      {
        switch (Serial.read())
        {
          case 's':
            //Serial.println("NEXT");
            button = BTN_NEXT; break;
          case 'a':
            //Serial.println("PREV");
            button = BTN_PREV; break;
          case 'z':
            //Serial.println("ENTER");
            button = BTN_ENTER; break;
          default: break;
        }
        renderMenu(button);
      }
    }
    Izskatās diezgan briesmīgi, bet strādā. Nu vot tagad vajag piemuhļīt attiecīgajiem punktiem funkcijas klāt. Kaut kas sāk sanākt. Nu i ķeru blusas.

    p.s. velns nevar kā pievienoto failu ielikt. Rāda "invalid file".

  3. #23
    Senior Member
    Nov 2009
    Jēkabpils
    2,046

    un pati meņuška MenuTree.h
    :
    struct menuItem_t
    {
      /*const char *name;*/
      uint8_t next;
      uint8_t prev;
      uint8_t child;
      uint8_t parent;
    };
    
    #define N 35
    
    const char root[] PROGMEM = "LEVEL_ROOT";
    const char lv10[] PROGMEM = "Level_1.0";
    const char lv11[] PROGMEM = "Level_1.1";
    const char lv12[] PROGMEM = "Level_1.2";
    const char lv13[] PROGMEM = "Level_1.3";
    const char lv14[] PROGMEM = "Level_1.4";
    const char lv15[] PROGMEM = "Level_1.5";
    const char lv16[] PROGMEM = "Level_1.6";
    const char lv17[] PROGMEM = "Level_1.7";
    const char lv100[] PROGMEM = "Level_1.0.0";
    const char lv101[] PROGMEM = "Level_1.0.1";
    const char lv102[] PROGMEM = "Level_1.0.2";
    const char lv103[] PROGMEM = "Level_1.0.3";
    const char lv104[] PROGMEM = "Level_1.0.4";
    const char lv105[] PROGMEM = "Level_1.0.5";
    const char lv106[] PROGMEM = "Level_1.0.6";
    const char lv110[] PROGMEM = "Level_1.1.0";
    const char lv111[] PROGMEM = "Level_1.1.1";
    const char lv112[] PROGMEM = "Level_1.1.2";
    const char lv113[] PROGMEM = "Level_1.1.3";
    const char lv114[] PROGMEM = "Level_1.1.4";
    const char lv115[] PROGMEM = "Level_1.1.5";
    const char lv116[] PROGMEM = "Level_1.1.6";
    const char lv120[] PROGMEM = "Level_1.2.0";
    const char lv121[] PROGMEM = "Level_1.2.1";
    const char lv122[] PROGMEM = "Level_1.2.2";
    const char lv123[] PROGMEM = "Level_1.2.3";
    const char lv124[] PROGMEM = "Level_1.2.4";
    const char lv125[] PROGMEM = "Level_1.2.5";
    const char lv1210[] PROGMEM = "Level_1.2.1.0";
    const char lv1211[] PROGMEM = "Level_1.2.1.1";
    const char lv1212[] PROGMEM = "Level_1.2.1.2";
    const char lv1213[] PROGMEM = "Level_1.2.1.3";
    const char lv1214[] PROGMEM = "Level_1.2.1.4";
    const char lv1215[] PROGMEM = "Level_1.2.1.5";
    const char empty[] PROGMEM = "";
    
    const char* const title[] PROGMEM = {root,
                                         lv10,
                                         lv100, lv101, lv102, lv103, lv104, lv105, lv106,
                                         lv11,
                                         lv110, lv111, lv112, lv113, lv114, lv115, lv116,
                                         lv12,
                                         lv120, lv121,
                                         lv1210, lv1211, lv1212, lv1213, lv1214, lv1215,
                                         lv122, lv123, lv124, lv125,
                                         lv13, lv14, lv15, lv16, lv17,
                                         empty
                                        };
    
    
    const menuItem_t MenuItems[] PROGMEM =
    {
      {/*root,*/    N, N, 1, N},        //0
      {/*lv10,*/      9, N, 2, 0},      //1
      {/*{lv100,*/      3, N, N, 1},    //2
      {/*lv101,*/       4, 2, N, N},    //3
      {/*lv102,*/       5, 3, N, N},    //4
      {/*lv103,*/       6, 4, N, N},    //5
      {/*lv104,*/       7, 5, N, N},    //6
      {/*lv105,*/       8, 6, N, N},    //7
      {/*lv106,*/       N, 7, N, N},    //8
      {/*lv11,*/      17, 1, 10, 0},    //9
      {/*lv110,*/       11, N, N, 9},   //10
      {/*lv111,*/       12, 10, N, N},  //11
      {/*lv112,*/       13, 11, N, N},  //12
      {/*lv113,*/       14, 12, N, N},  //13
      {/*lv114,*/       15, 13, N, N},  //14
      {/*lv115,*/       16, 14, N, N},  //15
      {/*lv116,*/       N, 15, N, N},   //16
      {/*lv12,*/      30, 9, 18, 0},    //17
      {/*lv120,*/       19, N, N, 17},  //18
      {/*lv121,*/       26, 18, 20, N}, //19
      {/*lv1210,*/        21, N, N, 19},//20
      {/*lv1211,*/        22, 20, N, N},//21
      {/*lv1212,*/        23, 21, N, N},//22
      {/*lv1213,*/        24, 22, N, N},//23
      {/*lv1214,*/        25, 23, N, N},//24
      {/*lv1215,*/        N, 24, N, N}, //25
      {/*lv122,*/       27, 19, N, N},  //26
      {/*lv123,*/       28, 26, N, N},  //27
      {/*lv124,*/       29, 27, N, N},  //28
      {/*lv125,*/       N, 28, N, N},   //29
      {/*lv13,*/      31, 17, N, N},    //30
      {/*lv14,*/      32, 30, N, N},    //31
      {/*lv15,*/      33, 31, N, N},    //32
      {/*lv16,*/      34, 32, N, N},    //33
      {/*lv17,*/      N, 33, N, N},     //34
      {/*empty,*/     N, N, N, N},    //35
    };

  4. #24
    Senior Member
    Nov 2009
    Jēkabpils
    2,046

    :
    #include <stdio.h>
    #include <stdint.h>
    
    typedef uint8_t(*ptr2func1)(uint8_t);
    typedef uint8_t(*ptr2func2)(uint8_t, uint8_t);
    
    uint8_t func1(uint8_t val){return ++val;}
    uint8_t func3(uint8_t val){return --val;}
    uint8_t func2(uint8_t val1, uint8_t val2){return val1+val2;}
    
    typedef struct{
        uint8_t id;
        union{
            ptr2func1 pf1;
            ptr2func2 pf2;
        };
        uint8_t value;
    }Item;
    
    int main(int argc, char **argv)
    {
        Item item0 = {0, {&func1}, 10};
        //Item item0 = {1, {&func2}, 15}; /*nestrādā*/
        Item item1 = {1, {.pf2 = &func2}, 15};
        Item item2 = {1, {&func3}, 15};
        
        printf("%d\n", (*item0.pf1)(10));
        printf("%d\n", (*item1.pf2)(12, 6));
        printf("%d\n", (*item2.pf1)(14));
        
        //printf("hello world\n");
        return 0;
    }
    Jā, nu it kā izoperēju mandeles caur pakaļu, bet tas nekā nedod.
    Pirmkārt jau nesaprotu, kāpēc negrib strādāt rinda:
    :
    Item item0 = {1, {&func2}, 15}; /*nestrādā*/
    It kā pēc pamācībām vajadzēja strādāt. Nu bet ok, to var apiet. Sliktais stāsts ir tas, ka lai gan es iebaroju funkcijas ar dažādiem prototipiem, tad kad vēlāk vajadzēs rakstīt kaut kādu funkciju, kura ņems vērtības no kaut kāda
    :
     Item *currentItem...
    būs jāzina, kura prototipa funkcija kurā brīdī būs "currentItem"sastāvā. Bet tas galīgi neder. Viss ir slikti
    Lai gan atmeļprojektos, jau tā izvēlne nav nekāda briesmīgā , un, varbūt, ka pietiek, ja piem. vienā "submenu" zarā punktos izpildāmās funkcijas ir ar vienādiem prototipiem.

+