Code:

#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <fxcg/rtc.h>
#include <fxcg/system.h>
#include <stdlib.h>
#include <string.h>

#define MENU_MATRIX_CODE 0x0409
#define EXE_MATRIX_CODE  0x0302

int flashState = 0;

void flashRealIcon(void) {
  flashState = !flashState;
  DefineStatusAreaFlags(flashState ? 4 : 5, SAF_SETUP_COMPLEX_MODE, NULL, NULL);
  DisplayStatusArea();
  Bdisp_PutDisp_DD();
}

void drawRightAligned(int y, const char* text) {
  int len = strlen(text);
  int px = 384 - len * 12;
  PrintXY(px / 6, y, text, TEXT_MODE_NORMAL, TEXT_COLOR_WHITE);
}

void main(void) {
  char *buffer = (char *)malloc(256);
  buffer[0] = '\0';
  int start = 0;
  int cursor = 0;

  char* history[8] = { NULL };
  int lineCount = 0;

  DrawFrame(0);
  DisplayMBString((unsigned char *)buffer, start, cursor, 1, 1);
  Bdisp_PutDisp_DD();

  char timerID = Timer_Install(0, flashRealIcon, 780);
  if (timerID != -1)
    Timer_Start(timerID);

  while (1) {
    int key = 0;
    int result = GetKey(&key);

    int col = (key >> 8) & 0xFF;
    int row = key & 0xFF;
    unsigned short matrixCode = (col << 8) | row;

    if (matrixCode == MENU_MATRIX_CODE) {
      break;
    }

    if (matrixCode == EXE_MATRIX_CODE) {
      if (lineCount < 8 && strlen(buffer) > 0) {
        history[lineCount] = strdup(buffer); 
        lineCount++;
        buffer[0] = '\0'; 
        cursor = 0;
        start = 0;
      }
    } else if (result == 1 && key < 30000) {
      cursor = EditMBStringChar((unsigned char *)buffer, 256, cursor, key);
    } else {
      EditMBStringCtrl((unsigned char *)buffer, 256, &start, &cursor, &key, 1, 1 + lineCount);
    }

    Bdisp_AllClr_VRAM();
    DrawFrame(0);


    for (int i = 0; i < lineCount; i++) {
      drawRightAligned(1 + i, history[i]);
    }

 
    DisplayMBString((unsigned char *)buffer, start, cursor, 1, 1 + lineCount);

    DisplayStatusArea();
    Bdisp_PutDisp_DD();
  }

  for (int i = 0; i < lineCount; i++) free(history[i]);
  free(buffer);
  Bdisp_AllClr_VRAM();
  Bdisp_PutDisp_DD();
}


This is code has Real flashing and a black frame and has basic text input however what I am trying to achieve is that by clicking EXE it re prints what I originally wrote in my first line back down one line below on the rightmost side however upon clicking EXE nothing happens.

Any help/guidance on the issue appreciated.
critical wrote:

This is code has Real flashing and a black frame and has basic text input however what I am trying to achieve is that by clicking EXE it re prints what I originally wrote in my first line back down one line below on the rightmost side however upon clicking EXE nothing happens.

Any help/guidance on the issue appreciated.


There's a few issues with the design here:
1. Have a look at the documentation for PrintXY: https://prizm.cemetech.net/Syscalls/PrintXY/
Coordinates for PrintXY aren't in pixels, the colour is the foreground colour not background, and the text should be prepended with two spaces. So, drawRightAligned should be:

Code:
void drawRightAligned(int y, const char *text) {
  int len = strlen(text) - 2; // Ignore two spaces at start
  PrintXY(21 - len, y, text, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
}


2. Given the two spaces needed, your history insertion code should be:

Code:
char *historyBuf = (char *)malloc(strlen(buffer) + 3); // + 1 '\0' + 2 spaces
historyBuf[0] = ' ';
historyBuf[1] = ' ';
strcpy(historyBuf + 2, buffer);

history[lineCount] = historyBuf;

lineCount++;
buffer[0] = '\0';
cursor = 0;
start = 0;


3. There's no need to use matrix codes here. GetKey keys are easily interfaced with:

Code:
int charKey = GetKey(&key);

if (key == KEY_CTRL_EXE && lineCount < 8 && strlen(buffer) > 0) {
  char *historyBuf = (char *)malloc(strlen(buffer) + 3);
  historyBuf[0] = ' ';
  historyBuf[1] = ' ';
  strcpy(historyBuf + 2, buffer);

  history[lineCount] = historyBuf;

  lineCount++;
  buffer[0] = '\0';
  cursor = 0;
  start = 0;
} else if (charKey == 1) {
  cursor = EditMBStringChar((unsigned char *)buffer, 256, cursor, key);
} else {
  EditMBStringCtrl((unsigned char *)buffer, 256, &start, &cursor, &key, 1,
                    1 + lineCount);
}


All put together results in:

Code:
#include <fxcg/display.h>
#include <fxcg/keyboard.h>
#include <fxcg/rtc.h>
#include <fxcg/system.h>
#include <stdlib.h>
#include <string.h>

int flashState = 0;

void flashRealIcon(void) {
  flashState = !flashState;
  DefineStatusAreaFlags(flashState ? 4 : 5, SAF_SETUP_COMPLEX_MODE, NULL, NULL);
  DisplayStatusArea();
  Bdisp_PutDisp_DD();
}

void drawRightAligned(int y, const char *text) {
  int len = strlen(text) - 2; // Ignore two spaces at start
  PrintXY(21 - len, y, text, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
}

void main(void) {
  char *buffer = (char *)malloc(256);
  buffer[0] = '\0';

  int start = 0;
  int cursor = 0;
  int key = 0;

  char *history[8] = {NULL};
  int lineCount = 0;

  DrawFrame(0);
  DisplayMBString((unsigned char *)buffer, start, cursor, 1, 1);
  Bdisp_PutDisp_DD();

  char timerID = Timer_Install(0, flashRealIcon, 780);
  if (timerID != -1)
    Timer_Start(timerID);

  while (1) {
    int charKey = GetKey(&key);

    if (key == KEY_CTRL_EXE && lineCount < 8 && strlen(buffer) > 0) {
      char *historyBuf = (char *)malloc(strlen(buffer) + 3);
      historyBuf[0] = ' ';
      historyBuf[1] = ' ';
      strcpy(historyBuf + 2, buffer);

      history[lineCount] = historyBuf;

      lineCount++;
      buffer[0] = '\0';
      cursor = 0;
      start = 0;
    } else if (charKey == 1) {
      cursor = EditMBStringChar((unsigned char *)buffer, 256, cursor, key);
    } else {
      EditMBStringCtrl((unsigned char *)buffer, 256, &start, &cursor, &key, 1,
                       1 + lineCount);
    }

    Bdisp_AllClr_VRAM();
    DrawFrame(0);

    for (int i = 0; i < lineCount; i++) {
      drawRightAligned(1 + i, history[i]);
    }

    DisplayMBString((unsigned char *)buffer, start, cursor, 1, 1 + lineCount);
    DisplayStatusArea();
    Bdisp_PutDisp_DD();
  }

  for (int i = 0; i < lineCount; i++)
    free(history[i]);

  free(buffer);
  Bdisp_AllClr_VRAM();
  Bdisp_PutDisp_DD();
}
Thanks for the help
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement