Hi,
for checking how much RAM an object occupies I made a small test with following sketch. But I don't understand the result.
The test setup uses a touch display which all the lcd. calls are made for.
guiTest.ino:
#include "GSM_GP.h" // only for freeRam()
#include "guiFkt.h"
#define RST_PIN (8) // fuer Display
int16_t x=0,y=0;
boolean checkButton(uint16_t x, uint16_t y, uint16_t w, uint16_t h, char* txt) {
// malt einen Button "txt" bei (x,y) mit der Breite w und Hoehe h wartet auf
// einen Touch und gibt true zurueck, wenn er beruehrt wurde
Button ButtWeiter(x,y, w,h, txt);
Serial.print(F(", in checkButton: freeRam="));
Serial.print(gsm.freeRam());
lcd.lcd_z = 0; // damit wird auf ein neues Touch gewartet
while (!lcd.touchRead()) ; // warten auf Touch
while (lcd.touchRead()) ; // warten bis kein Touch mehr
if ( ButtWeiter.isHit(lcd.lcd_x,lcd.lcd_y)) {
ButtWeiter.~Button();
return( 1);
}
else {
ButtWeiter.~Button();
return( 0);
}
}
void setup() {
Serial.begin(9600);
while (!Serial); // needed for Leonardo only
Serial.println("guiTest neu ---------");
//init display
lcd.begin(SPI_CLOCK_DIV4, RST_PIN);
lcd.fillScreen(WHITE); // bereits in lcd.begin()
while(1) {
Serial.print(F("vor checkButton: freeRam="));
Serial.print(gsm.freeRam());
checkButton(100, 50, 50, 12, (char*)("weiter"));
Serial.print(F(", nach checkButton: freeRam="));
Serial.println(gsm.freeRam());
delay(2000);
Serial.println(F("nochmal"));
}
}
void loop() {
}
guiFkt.cpp:
#include "guiFkt.h"
DisplaySPI lcd; //SPI (GLCD-Shield or MI0283QT Adapter v2)
Button::Button(uint16_t px, uint16_t py, uint16_t wd, uint16_t ht, char* title)
{
uint16_t tw;
lcd.drawRect(px, py, wd, ht, BLACK);
tw = strlen(title) * FONT_WIDTH;
if (tw > wd - 2) {
tw = w / FONT_WIDTH;
title[tw] = 0; // String kuerzen
tw *= FONT_WIDTH;
}
lcd.drawText(px + (wd - tw) / 2, py + (ht - FONT_WIDTH) / 2, title, BLACK, WHITE, 1);
x=px; y=py;
w=wd; h=ht;
}
Button::~Button()
{
lcd.fillRect(x,y, w,h, WHITE);
}
boolean Button::isHit(uint16_t hx, uint16_t hy)
{
if ((hx>=x) && (hx<=x+w-1) && (hy>=y) && (hy<=y+h-1))
return(true);
else return(false);
}
guiFkt.h:
#ifndef GUI_FKT_H
#define GUI_FKT_H
#include <Wire.h>
#include <SPI.h>
#include <digitalWriteFast.h>
#include <DisplaySPI.h>
#include <fonts.h>
// Farben in RGB565:
#define BLACK 0
#define WHITE RGB(255,255,255) // 0xFFFF
#define RED RGB(255, 0, 0) // 0xF800
#define GREEN RGB( 0,255, 0) // 0x07E0
#define BLUE RGB( 0, 0,255) // 0x001F
#define YELLOW RGB(200,200, 0) // 0xCE40
#define NAVY RGB( 0, 0,123) // 0x000F
#define ORANGE RGB(255,165, 0) // 0xFD20
#define MAROON RGB(123, 0, 0) // 0x7800
#define PURPLE RGB(123, 0, 123) // 0x780F
#define OLIVE RGB(123,125, 0) // 0x7BE0
#define CYAN RGB( 0,255,255) // 0x07FF
#define MAGENTA RGB(255, 0,255) // 0xF81F
#define DARKGREEN RGB(0, 125, 0) // 0x03E0
#define DARKCYAN RGB(0, 125, 123) // 0x03EF
#define LIGHTGREY RGB(198,195,198) // 0xC618
#define DARKGREY RGB(123,125,123) // 0x7BEF
extern DisplaySPI lcd; //SPI (GLCD-Shield or MI0283QT Adapter v2)
class Button
{
public:
Button(uint16_t px, uint16_t py, uint16_t wd, uint16_t ht, char* title);
~Button();
boolean isHit(uint16_t hx, uint16_t hy);
private:
uint16_t x,y;
uint16_t w,h;
};
#endif
The function freeRam() is contained in a separate file, GSM_GP.h. The code is from this forum:
int GSM_GP::freeRam (void) {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
The output from the sketch is:
vor checkButton: freeRam=7197, in checkButton: freeRam=7205, nach checkButton: freeRam=7197
I expected less free RAM within the function checkButton() because a function call takes some bytes from RAM and the object ButtWeiter also needs some RAM. But it is vice versa.
I don't understand why, or is something wrong with freeRam()?