plaats variabelen doet het programma hangen

algemene C code
Berichten: 49
Geregistreerd: 09 Mei 2015, 15:57

plaats variabelen doet het programma hangen

Berichtdoor doebi » 09 Feb 2017, 22:30

Hallo,

Ik ben bezig met een upgrade van een projectje voor buttons op een touchscreen te zetten en
deze af te toetsen.
Nu is er een probleem dat als ik bepaalde variabelen wil helemaal bovenaan het programma zetten
het programma "hangt" , het compileert wel.
Zet ik deze variabelen enkel in de setup of in de loop dat werkt het programma,
maar ik kan deze variabelen enkel dan in die functie gebruiken.
Ik wil ze dus globaal zetten.
Programmeeromgeving visual studio 2015 met visual micro module

Iemand een idee hoe dit moet , of wat de precieze reden is waardoor het programma hangt ?

Dit werkt (.ino file) ( maar ik kan de variabelen MyWindows en MyElement niet gebruiken in bv de loop functie) :
Code: Alles selecteren
#include "WindowEnviroment.h"



void setup()
{
   
   WindowEnviroment MyWindows;
   clsElementData*    MyElement;

   Serial.begin(9600);
   Serial.println("Setup");
   

   char firstlabel[LABELSIZE] = "Test";
   char secondlabel[LABELSIZE] = "2de";
   char thirdlabel[LABELSIZE] = "3de";
   MyWindows.initElement(1, firstlabel, 10, 10, 100, 100, 20, 100, 500, enStyle::rect);
   MyElement = MyWindows.initElement(2, secondlabel, 90, 140, 35, 35, 20, 100, 500, enStyle::roundRect);
   MyElement = MyWindows.initElement(3, thirdlabel, 140, 140, 120, 120, 2000, 900, 1500, enStyle::circle);
   MyWindows.drawAll();

   
  /* add setup code here */

}
int x = 0;
void loop()
{   
      
      

   

   Serial.println(x++);
  /* add main program code here */
   
}



Dit werkt niet (.ino file) : (compileert prima maar het programma "hangt")
Code: Alles selecteren
#include "WindowEnviroment.h"

WindowEnviroment MyWindows;
clsElementData*    MyElement;


void setup()
{
   


   Serial.begin(9600);
   Serial.println("Setup");
   

   char firstlabel[LABELSIZE] = "Test";
   char secondlabel[LABELSIZE] = "2de";
   char thirdlabel[LABELSIZE] = "3de";
   MyWindows.initElement(1, firstlabel, 10, 10, 100, 100, 20, 100, 500, enStyle::rect);
   MyElement = MyWindows.initElement(2, secondlabel, 90, 140, 35, 35, 20, 100, 500, enStyle::roundRect);
   MyElement = MyWindows.initElement(3, thirdlabel, 140, 140, 120, 120, 2000, 900, 1500, enStyle::circle);
   MyWindows.drawAll();

   
  /* add setup code here */

}
int x = 0;
void loop()
{   
      
      

   

   Serial.println(x++);
  /* add main program code here */
   
}



De .h file :
Code: Alles selecteren
#pragma once

#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>

#define TFT_CS 10
#define TFT_DC 9

struct strPoint {
public :
   int x;
   int y;
};

enum  enStyle {
   rect = 1,
   roundRect = 2,
   circle = 3,
   labelOnly = 4
};

const int LABELSIZE = 15;

class clsElementData {
public :
   int         ID;
   int         x;
   int         y;
   int         width;
   int         height;
   int         colorBack;
   int         colorText;
   int         colorBorder;
   char      label[LABELSIZE];
   enStyle      style;
   clsElementData *myChild;
   clsElementData *myParent;
};

class WindowEnviroment
{
public:
   WindowEnviroment();
   ~WindowEnviroment();
   clsElementData      *findFirst();
   clsElementData      *findNext();
   clsElementData      *findLast();
   clsElementData      *findID(int aID);
   clsElementData      *initElement(int aID, char *aLabel, int aX,int aY, int aWidth, int aHeight, int aTextColor, int aBackColor, int aBorderColor, enStyle aStyle);
   clsElementData      *scanTouchAll();
   bool            scanTouchCurrent(TS_Point *p);
   void            drawAll();
   void            drawCurrent();
   void            drawLabelCenter(int8_t aSize);
   Adafruit_FT6206      TheCTP = Adafruit_FT6206();
   Adafruit_ILI9341   TheTFT = Adafruit_ILI9341(TFT_CS, TFT_DC);
   clsElementData      *allElements;
   clsElementData      *currentElement;
};






De .cpp file :
Code: Alles selecteren
#include "WindowEnviroment.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>



WindowEnviroment::WindowEnviroment()
{
   TheTFT.begin();
   TheCTP.begin(40);
   currentElement = nullptr;                                 // starting with null current element
   allElements = nullptr;                                    // starting with null elements in the list
}

WindowEnviroment::~WindowEnviroment()
{
}



clsElementData * WindowEnviroment::findFirst()
{
   //   Function purpose :
   //   Set the pointer currentElement to the first element of the list (list = allElements)
   //   Returns :
   //   The currentElement
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr or the first element
   //   Use in combination with function findNext
   
   currentElement = allElements;                              // point the currentelement to the first element in the list
   
   return currentElement;                                    // function returns currentelement with points to the first element
}

clsElementData * WindowEnviroment::findNext()
{
   //   Function purpose :
   //   Set the pointer currentElement to the next element of the list (list = allElements) , the next element after the currentElement
   //  before the function call
   //   Returns :
   //   The currentElement
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr or the first element
   //   Use in combination with function findFirst

   if (currentElement != nullptr)                              // if the currentelement is not null
   {
      if (currentElement->myChild != nullptr)                     // and the currentelement child is not null
      {
         currentElement = currentElement->myChild;               // then currentelement becomes current element child
      }
      else
      {
         currentElement = nullptr;                           // if there is no child, currentElement should be nullptr
      }

   }
   return currentElement;                                    // function returs the current element
}

clsElementData * WindowEnviroment::findLast()
{
   //   Function purpose :
   //   Set the pointer currentElement to the last element of the list (list = allElements)
   //   Returns :
   //   The currentElement
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr or the last element

   if (findFirst() != nullptr)                                 // point currentelement to first element in the element list
   {
      while (currentElement->myChild != nullptr)                  // while the currentelement child is not null
      {
         findNext();                                       // point currentelement to the next element in the element list
      }
   }
   return currentElement;                                    // function returns the current element
}

clsElementData * WindowEnviroment::findID(int aID)
{
   //   Function purpose :
   //   Search for an element with a certain ID and point currentElement to it
   //   Returns :
   //   NullPtr if not found or a pointer to the found element
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr or the found element

   findFirst();                                          // get the first element in the list (allElements) and put in currentElement, if empty currentElement = nullptr
   while (currentElement != nullptr)                           // while there is a currentElement ( if its not nullptr )
   {
      if (currentElement->ID = aID)                           // if the ID on the currentElement is the search ID
      {
         break;                                          // then break the while loop
      }
      findNext();                                          // if not , find the next element in the list (allElements)
   }
   return currentElement;                                    // function returns the current element
}

clsElementData * WindowEnviroment::initElement(int aID , char * aLabel, int aX, int aY, int aWidth, int aHeight, int aTextColor, int aBackColor, int aBorderColor, enStyle aStyle)
{
   //   Function purpose :
   //   Set the pointer currentElement to a new created element of the list (list = allElements) and initialize all sorts of properties
   //   Returns :
   //   The currentElement
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr or the first element

   if (findLast() == nullptr)                                 // find last element and point current element to it, if it's null
   {
      allElements = new clsElementData;                        // then make a new element (a first) in the list (and point the list to it)
      currentElement = allElements;                           // set the current element to this new element
      currentElement->myParent = nullptr;                        // and there is no parent for this first element
   }
   else                                                // if not, currentelement points to the last element
   {
      currentElement->myChild = new clsElementData;               // make a new element on the child of the current element
      currentElement->myChild->myParent = currentElement;            // make that new element's parent point to the current element
      currentElement = currentElement->myChild;                  // make the current element the new created element
   }
   
   currentElement->ID = aID;                                 // with this ID, one can find the element in the list (allElements)
   currentElement->myChild = nullptr;                           // ok, now the new element has no child, so put it on null

   for (int i = 0; i < LABELSIZE; i++) {                        // for each character
      currentElement->label[i] = aLabel[i];                     // copy it
   };
   currentElement->style = aStyle;
   currentElement->colorText = aTextColor;
   currentElement->colorBack = aBackColor;
   currentElement->colorBorder = aBorderColor;
   currentElement->x = aX;
   currentElement->y = aY;

   currentElement->width = aWidth;
   currentElement->height = aHeight;
   



   return currentElement;                                    // functions returs the current element
}

clsElementData * WindowEnviroment::scanTouchAll()
{
   //   Function purpose :
   //   Set the pointer currentElement to the first element of the list (list = allElements)
   //   Returns :
   //   The currentElement
   //
   //   Remarks :
   //   The pointer currentElement can eighter be nullptr when there is no touch detected or no element is found in touch region
   //  or it will point to the touched element

   currentElement = nullptr;                                 // put the pointer to the current element on null
   if (TheCTP.touched() )                                    // the screen has been touched
   {
      TS_Point p = TheCTP.getPoint();                           // get the touch location

      
      if (findFirst() != nullptr) {                           // get first element and point to it with currentelement, and if it's not null
         while (currentElement != nullptr)                     // repeat while the currentelement is not null
         {
            if ( scanTouchCurrent(&p) )                        // if the currentelement matches the touchlocation
            {
               break;                                    // then exit this loop, while currentelement points to the element that matched the touched location
            }
            else                                       // if the currentelement does not match the touchlocation
            {
               findNext();                                 // then find the next element and point currentelement to it
            }
         }
      }
   }
   return currentElement;                                    // return null or a matching element
}

bool WindowEnviroment::scanTouchCurrent(TS_Point *p)
{
   //   Function purpose :
   //   Test if a touchpoint (TS) is in the region of the element pointed by currentElement
   //   Returns :
   //   true if the touchspoint is in the region of the element currentElement
   //
   //   Remarks :
   //   currentElement is not affected
   bool Res = false;                                       // set res to false

   int x1 = currentElement->x;                                 // for easy reading , put origin x in x1
   int y1 = currentElement->y;                                 // and origin y in y
   

   if (currentElement->style = enStyle::circle)                  // if the element is a circle,
   {
      x1 -= currentElement->width / 2;                        // then x1 should be subtracted by the half of the elements width
      y1 -= currentElement->height / 2;                        // and so does y1

   }
   
   int x2 = x1 + currentElement->width;                        // for easy reading, calculate x2
   int y2 = y1 + currentElement->height;                        // as well for y2

   if (p->x >= x1)                                          // if x touched location is greater or equal to x1
   {
      if (p->y >= y1)                                       // and if y touched locaction is greater or equal to y1
      {
         if (p->x <= x2)                                    // and that x is lesser or eaqual than x2
         {
            if (p->y <= y2)                                 // and y the same for y2
            {
               Res = true;                                 // then the result of this function should be true
            }
         }
      }
   }



   return Res;                                             // give result to the function
}
   


void  WindowEnviroment::drawAll()
{
   //   Function purpose :
   //   Make a loop in all the elements and call there drawing function
   //   Returns :
   //   nothing
   //
   //   Remarks :
   //   the pointer currentElement is affected

   // Serial.println("drawall enter");

   if (findFirst() != nullptr) {                              // find first element and point in to currentelement, and if this element is not null
      drawCurrent();                                       // draw this element
      while (findNext() != nullptr)                           // if get the next element,point in to currentelement, and if this is not null
      {
         drawCurrent();                                    // draw this element
      }
   }
   
   // Serial.println("Drawall exit");
                                                      // note that after this function call, elementcurrent points to null
}

void WindowEnviroment::drawCurrent()
{
   //   Function purpose :
   //   Draws the element currentElement
   //   Returns :
   //   nothing
   //
   //   Remarks : no check if the currentElement is nullptr !
   //   /
   switch ( currentElement->style)
   {
   case enStyle::rect:
      TheTFT.fillRect(currentElement->x + 1,currentElement->y + 1,
                   currentElement->width-2,currentElement->height-2,
                   currentElement->colorBack);
      TheTFT.drawRect(currentElement->x,currentElement->y,
                   currentElement->width,currentElement->height,
                   currentElement->colorBorder);
      TheTFT.setTextColor(currentElement->colorText, currentElement->colorBack);
      drawLabelCenter(3);
      break;
   case enStyle::roundRect:
      TheTFT.fillRoundRect(currentElement->x + 1, currentElement->y + 1,
         currentElement->width - 2, currentElement->height - 2,
         5,
         currentElement->colorBack);
      TheTFT.drawRoundRect(currentElement->x, currentElement->y,
         currentElement->width, currentElement->height,
         5,
         currentElement->colorBorder);
      TheTFT.setTextColor(currentElement->colorText, currentElement->colorBack);
      drawLabelCenter(3);
      break;
   case enStyle::circle:
      TheTFT.fillCircle(currentElement->x, currentElement->y,
         currentElement->width - 1,
         currentElement->colorBack);
      TheTFT.drawCircle(currentElement->x, currentElement->y,
         currentElement->width,
         currentElement->colorBorder);
      TheTFT.setTextColor(currentElement->colorText, currentElement->colorBack);
      drawLabelCenter(3);
      break;
   case enStyle::labelOnly:
      TheTFT.setTextColor(currentElement->colorText, currentElement->colorBack);
      drawLabelCenter(3);
      break;
   default:
      break;
   }
}

void WindowEnviroment::drawLabelCenter(int8_t aSize)
{
   //   Function purpose :
   //   Draws the label of the element currentElement in a certain size centred in its region
   //   Returns :
   //   nothing
   //
   //   Remarks :
   //   /

   TheTFT.setTextSize(aSize);
   int labelSize = strlen(currentElement->label);
   switch (currentElement->style) {
   case enStyle::rect:
         TheTFT.setCursor(currentElement->x + currentElement->width / 2 - (5 * aSize * ( labelSize / 2)),
                         currentElement->y + currentElement->height / 2 - (7 * aSize / 2));
         break;
   case enStyle::roundRect:
         TheTFT.setCursor(currentElement->x + currentElement->width / 2 - (5 * aSize * ( labelSize / 2)),
                         currentElement->y + currentElement->height / 2 - (7 * aSize / 2));
         break;
   case enStyle::circle:
         TheTFT.setCursor(currentElement->x - (5 * aSize * ( labelSize / 2)),
                         currentElement->y - (7 * aSize / 2));
         break;
   case enStyle::labelOnly:
         TheTFT.setCursor(currentElement->x + currentElement->width / 2 - (5 * aSize * (labelSize / 2)),
                      currentElement->y + currentElement->height / 2 - (7 * aSize / 2));
      break;
   }

   for (int i = 0; currentElement->label[i]  ; i++)
   {
      TheTFT.print(currentElement->label[i]);
   }
   
}


Advertisement

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 14 gasten