DWrite.gph : Display Text & Numbers via the Segment Display

Documentation, usage tips and configuration guides for Titan Two scripts and bytecodes published by the community users.

DWrite.gph : Display Text & Numbers via the Segment Display

Postby Scachi » Wed Dec 05, 2018 6:29 pm

**Updated: Added support for negative numbers and fix32 numbers

This header file adds two functions to display "Some Text" or (int32) Numbers via the Titan Two Segment Display.
Text speed and some other things are configurable.

It is available as a header file "DWrite.gph" only or as "DWrite_Example" that includes a gpc script for example usage.

Usage in your own script:
Download the Headerfile from the Online Resource.

Place this .gph file into the same folder where your script file is.
Add this line to the top of your script:
Code: Select all
#include "DWrite.gph"


! Do not call the functions on every interaction of main !
Only call it when you want to start the output of the text.

Basic Example:
Code: Select all
 
#include "DWrite.gph"
 
char *SomeText[] = {"First","Another word","number 12.5"};
int8 SomeNum=56;
 
init {
  // only call the DWrite function on event_press / release or a toggle state change.
  // calling it directly in the main loop will show the first character only all the time
 
  DWriteTxt("Hello");
  //DWriteTxt("This Is The Text To Display");
  //DWriteTxt(SomeText[0]);
  //DWriteTxt(SomeText[1]);
  //DWriteTxt(SomeText[2]);
  //DWriteNum(12457);
  //DWriteNum(SomeNum);
}
 
main {
  // dpad-up
  if (event_active(BUTTON_10)) DWriteTxt("abc");
  if (event_active(BUTTON_11)) DWriteNum(123);
  if (event_active(BUTTON_12)) DWriteTxt(SomeText[1]);
  if (event_active(BUTTON_13)) DWriteNum(SomeNum);
}
 
Last edited by Scachi on Mon Mar 04, 2019 1:03 pm, edited 1 time in total.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Chaosrocket » Wed Feb 06, 2019 8:20 am

Totally Awesome
User avatar
Chaosrocket
Sergeant Major
Sergeant Major
 
Posts: 96
Joined: Mon Jun 13, 2016 9:10 pm

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Scachi » Mon Mar 04, 2019 1:04 pm

**Updated: Added support for negative numbers and fix32 numbers
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Convivium » Sun Mar 31, 2019 4:25 am

Scachi wrote:**Updated: Added support for negative numbers and fix32 numbers


So I am kinda new to the T2 and trying to figure it out on my own but I am having trouble with this.

What part of the GPH file would I need to place inside my GPC script to allow the T2 to display a 2 digit # when a button is released when the T2 is only connected to the console?

Would this be close?

Code: Select all
// ---- Configuration ----
#define DWrite_MaxWrite  0     // when to stop textoutput: 0=complete text, 1=first whitespace, 2 or more for stop after n characters
#define DWrite_Wait      500   // 500ms for each character to display
#define DWrite_WaitNext  80    // 50ms before next character to display
#define DWrite_WaitNextD NULL  // the mask from display.gph to show between characters, default is none/NULL;
#define DWrite_Def       SEG_D // the mask from display.gph to use for unknown characters, SEG_D is the bottom segment
#define DWrite_Ws        NULL  // the mask from display.gph to use for whitespace, default is none/NULL;
//
#define DWRITE_NS // no special character like: dot slash underline (only a few supported atm)
#define DWRITE_LC // lowercase only (default) (converts upper to lower)
#define DWRITE_UC // uppercase only (converts lower to upper)
#define DWRITE_MC // uppercase and lowercase
#define DWRITE_DI // disable function DWriteInt(int32 Num) (number to char conversion output)
#define DWRITE_DF // disable function DWriteFix(fix32 Num) (fix32 numer to char conversion output)
 
*** Notes END ***/
 
#ifndef DISPLAY_GPH_
#include <display.gph>
#endif
 
#ifndef DWrite_GPH_
#define DWrite_GPH_
 
// display options
//
 
//
#ifndef DWrite_Ws           
  #define DWrite_Ws  NULL    // mask (from display.gph) to display for whitespace
#endif                 
//
#ifndef DWrite_MaxWrite           
  #define DWrite_MaxWrite 0  // show maximum of selected characters
//                           // 0 = complete
//                           // 1 = stop at first whitespace
//                           // 2 and above = stop after n characters
#endif
//
#ifndef DWrite_Wait         
  #define DWrite_Wait 450    // delay between each character during text display via segment led
#endif
//
#ifndef DWrite_WaitNext
  #define DWrite_WaitNext 60        // delay between each character during text display via segment led
#endif
//
#ifndef DWrite_WaitNextD
  #define DWrite_WaitNextD NULL    // mask (from display.gph) to display during wait time between characters, default NULL
#endif                 
 
 
// char -> bitmask mapping for output of names via the segment display
//
// Special Chars
 
//
// Numbers & Letters
// default to lowercase
 
 
// lowercase & uppercase (default)
#ifdef DWRITE_MC
#undef DWRITE_UC // just to avoid errors
 
uint8 DWriteCr[]={
    // 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
      _0_,_1_,_2_,_3_,_4_,_5_,_6_,_7_,_8_,_9_,
    //10 , 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
      _a_,_b_,_c_,_d_,_E_,_F_,_g_,_h_,_i_,_J_,_k_,_l_,_m_,_n_,_o_,_p_,_q_,_r_,_s_,_t_,_u_,_v_,_w_,_x_,_y_,_z_,
    //36 , 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
      _A_,_B_,_C_,_D_,_E_,_F_,_G_,_H_,_I_,_J_,_K_,_L_,_M_,_N_,_O_,_P_,_Q_,_R_,_S_,_T_,_U_,_V_,_W_,_X_,_Y_,_Z_,
};
#endif
//
// lowercase only
 
//
// uppercase only
#ifdef DWRITE_UC
uint8 DWriteCr[]={
    // 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
      _0_,_1_,_2_,_3_,_4_,_5_,_6_,_7_,_8_,_9_,
    //10 , 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
      _A_,_B_,_C_,_D_,_E_,_F_,_G_,_H_,_I_,_J_,_K_,_L_,_M_,_N_,_O_,_P_,_Q_,_R_,_S_,_T_,_U_,_V_,_W_,_X_,_Y_,_Z_,
};
#endif
 
// own control/helper flags/variables
//
bool    DWrite_On;    // state of text output
int8    DWrite_Idx; // the current active character beeing displayed
char    *DWriteT;   // the string to display
 
#if !defined(DWRITE_DI) || !defined(DWRITE_DF)
#define DWriteNum(a) DWriteInt(a) // backward compatibility
    char    DWriteN[13];// int32/fix32 convert to char for diplay output
    uint8        DWiPos;            // current position writing/pointing to
#endif
 
 
 
main {
  if (DWrite_On) combo_run(cDisplayText); // write the text
}
 
 
#if !defined(DWRITE_DI) || !defined(DWRITE_DF)
// used by all number to text conversion
void DWriteNr(int32 Num, uint8 oPos) {
    //printf("parameter Num: %ld",Num);
    uint8 iDigits=1;
  while(Num/pow(10,iDigits)) iDigits++;
 
  uint8 iThis;
  for (DWiPos=0;DWiPos<iDigits;DWiPos++) {
    iThis=Num/pow(10,(iDigits-(DWiPos+1)));
    Num=Num-iThis*pow(10,iDigits-DWiPos-1);
    DWriteN[DWiPos+oPos]=iThis+48;
  }
    DWiPos+=oPos;
    DWriteN[DWiPos]='\0';
}
 
bool DWriteNegative(int32 Num) {
    if (Num<0) {
        DWriteN[0]='-';    return TRUE;
    }
    return FALSE;
}
#endif
 
// int32 number to text conversion for output
#ifndef DWRITE_DI
void DWriteInt(int32 Num) {
    DWriteNr(abs(Num),DWriteNegative(Num));
  DWriteTxt(&DWriteN);
    //printf(&DWriteN);
}
#endif
 
// fix32 to text conversion for output
#ifndef DWRITE_DF
void DWriteFix(fix32 Num,uint8 prec) {
    //### first part before .
    DWriteNr((int32)abs(Num),DWriteNegative((int32)Num)); //printf(&DWriteN);
    DWriteN[DWiPos]='.'; //printf(&DWriteN);
    DWiPos++;    //printf("DWiPos: %d",DWiPos);
    //### second part after .
    Num=abs(Num);
    Num=Num-floor(Num); //printf("rest: %f",Num);
    // round to given precision
    Num=round(Num*pow(10f,(fix32)clamp(prec,1,4))); //printf(". : %f",Num);
    DWriteNr((int32)Num,DWiPos);
  DWriteTxt(&DWriteN);
    //printf(&DWriteN);
}
#endif
 
 
// start displaying text via segment display
void DWriteTxt(char *text) {
  DWriteT=text;
  //printf(text);
  DWrite_On=FALSE; // stop first
  combo_stop(cDisplayText);
  combo_stop(cDWriteEnd);
  DWrite_Idx=0;     // reset to display first character
  DWrite_On=TRUE;    // start displaying
}
 
// display the text via segment display
combo cDisplayText {
  DWriteWrite(DWriteT);
  wait(0);
  wait(DWrite_Wait);
    display_overlay(DWrite_WaitNextD,DWrite_WaitNext);
    wait(DWrite_WaitNext);
}
 
 
// write out all characters up to selected limit/condition
void DWriteWrite(char *text) {
  int8 C=text[DWrite_Idx]; // get current char
  uint8 Mask;
 
  // convert to mask array position //ascii 0:48, A:65, a:97
  //                                //index 0     10/36 10
  // default
  Mask=DWrite_Def;
  // lowercase or uppercase
#if defined(DWRITE_LC) || defined(DWRITE_UC)
  if (C >= 97)      Mask=DWriteCr[C-87]; // a..z
  else if (C >= 65) Mask=DWriteCr[C-55]; // uppercase to lowercase
  else if (C >= 48) Mask=DWriteCr[C-48]; // 0..9
#endif
  //
  // lower and uppercase
#ifdef DWRITE_MC
  if (C >= 97 && C < 123)     Mask=DWriteCr[C-87]; // a..z
  else if (C >= 65 && C < 91) Mask=DWriteCr[C-29]; // A..Z
  else if (C >= 48 && C < 58 ) Mask=DWriteCr[C-48]; // 0..9
#endif
  // space
  else if (C == 32) Mask=DWrite_Ws;
  // special chars if not disabled
#ifndef DWRITE_DS
  else if (C >= 45 && C < 48) Mask=DWriteCrS[C-45]; // // -./_
#endif
 
//uint8 DWriteCrS[]={ 
            // 0:- ,  1: .    ,       2: /      , 62, 63:_
//            SEG_G,BOTTOM_DOT,SEG_B|SEG_G|SEG_E,NULL,SEG_D,
  display_overlay(Mask,DWrite_Wait);
  //display_overlay(DWriteCr[C],DWrite_Wait);
  //printf("C: %d,char: %c %d",C,name[DisplayIdx],name[DisplayIdx]);
  DWrite_Idx++;
 
  //if ( DWrite_MaxWrite==1 && text[DWrite_Idx] == ' ' ) printf("space");
 
  if (
       (text[DWrite_Idx] == '\0')                   // always stop at end of string
    || (DWrite_MaxWrite==1 && text[DWrite_Idx] == ' ' )  // stop at first space
    || (DWrite_MaxWrite>=2 && DWrite_MaxWrite==DWrite_Idx  )  // stop after showing selected amount of characters
  ) {
    DWrite_On=FALSE;             // end
    combo_run(cDWriteEnd); // end wait
  }
}
 
// extra pause when end is reached
combo cDWriteEnd {
  wait(DWrite_Wait);
  display_overlay(NULL,DWrite_Wait*2);
  wait(0);
  wait(DWrite_Wait);
}
 
#endif
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Scachi » Sun Mar 31, 2019 5:48 am

Do not edit it or use parts of it only as this is not how you use header files.
..it is noted at the top of the file you have edited:
Code: Select all
//
//
// NOTHING TO EDIT HERE IN THIS FILE

Use the header file as described in the first post.

Change the basic example to use event_release instead of event_active.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Convivium » Sun Mar 31, 2019 2:42 pm

Scachi wrote:Do not edit it or use parts of it only as this is not how you use header files.
..it is noted at the top of the file you have edited:
Code: Select all
//
//
// NOTHING TO EDIT HERE IN THIS FILE

Use the header file as described in the first post.

Change the basic example to use event_release instead of event_active.



Thanks!
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Convivium » Wed Apr 03, 2019 8:21 pm

Scachi wrote:Do not edit it or use parts of it only as this is not how you use header files.
..it is noted at the top of the file you have edited:
Code: Select all
//
//
// NOTHING TO EDIT HERE IN THIS FILE

Use the header file as described in the first post.

Change the basic example to use event_release instead of event_active.



I got it working. Thanks for your help and the time you put in to making the Header file.

Question: is there anyway to have the integers/text display twice instead of only one time then resetting to the slot #?

Let me know.

Also, I got this, but it wasn't a warning and it did not affect anything with the display output working properly. (below)

Screen Shot 2019-04-03 at 4.14.31 PM.png
Screen Shot 2019-04-03 at 4.14.31 PM.png (21.67 KiB) Viewed 2957 times
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Scachi » Thu Apr 04, 2019 5:28 am

"Unreferenced" is something that is available but is not used at the moment. Getting rid of those unreferenced stuff can reduce bytecode / free up space.
The function "DWriteFix" is used to write fix32 decimals like 12.45 via the segment display.
My header file supports some switches to disables some functions or configure display options without having to edit the header file itself.
You can disable the function DWriteFix by adding this line above the #include line of the header file, so you have two lines now for the specific header file (still nothing to edit in the header file itself):
Code: Select all
 
#define DWRITE_DF // disables function DWriteFix(fix32)
#include "DWrite.gph"
 

When done right the "unreferenced" message should no longer show up as you have disabled the function.

Displaying the text twice isn't supported by the header file. This would make it hard to read the output correctly if the number is "2" and is displayed twice without the user knowing it. he will think it is "22" instead.

I don't know where/how you are using it to output some numbers. It might be easer to use L3/LS or some other button to output the number again instead of displaying it twice always

You can adjust the output speed and pause between the single numbers/text with more #define line before the #include of the header file too, again without editing the header file, you can configure its display options:

Example:
Code: Select all
 
#define DWrite_Wait      600   // time in ms for each character to display
#define DWrite_WaitNext  100  // pause time in ms before next character to display
#define DWrite_WaitNextD NULL  // the mask from display.gph to show between characters, default is none/NULL
#define DWRITE_DF               // disables function DWriteFix(fix32)
#include "DWrite.gph"
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Convivium » Thu Apr 04, 2019 4:24 pm

Scachi wrote:"Unreferenced" is something that is available but is not used at the moment. Getting rid of those unreferenced stuff can reduce bytecode / free up space.
The function "DWriteFix" is used to write fix32 decimals like 12.45 via the segment display.
My header file supports some switches to disables some functions or configure display options without having to edit the header file itself.
You can disable the function DWriteFix by adding this line above the #include line of the header file, so you have two lines now for the specific header file (still nothing to edit in the header file itself):
Code: Select all
 
#define DWRITE_DF // disables function DWriteFix(fix32)
#include "DWrite.gph"
 

When done right the "unreferenced" message should no longer show up as you have disabled the function.

Displaying the text twice isn't supported by the header file. This would make it hard to read the output correctly if the number is "2" and is displayed twice without the user knowing it. he will think it is "22" instead.

I don't know where/how you are using it to output some numbers. It might be easer to use L3/LS or some other button to output the number again instead of displaying it twice always

You can adjust the output speed and pause between the single numbers/text with more #define line before the #include of the header file too, again without editing the header file, you can configure its display options:

Example:
Code: Select all
 
#define DWrite_Wait      600   // time in ms for each character to display
#define DWrite_WaitNext  100  // pause time in ms before next character to display
#define DWrite_WaitNextD NULL  // the mask from display.gph to show between characters, default is none/NULL
#define DWRITE_DF               // disables function DWriteFix(fix32)
#include "DWrite.gph"
 


Thanks for the #define. Super helpful explanation, as well.

I did edit some of the display and pause values above the header.

It works great!

Thank you.
User avatar
Convivium
Master Sergeant
Master Sergeant
 
Posts: 26
Joined: Wed Mar 27, 2019 2:45 am
Location: Clemson, SC

Re: DWrite.gph : Display Text & Numbers via the Segment Disp

Postby Scachi » Fri Apr 05, 2019 8:52 am

Small bugfix and added two more #define configuration options. redownload the example or header file for them to work.
The end combo didn't work at correctly before, returning to fast, showing the slot number quickly after displaying the numbers/text.
Code: Select all
 
#define DWrite_WaitEnd   300     // time at the end of the text to wait before showing the slot number again
#define DWrite_WaitEndD  NULL // the mask from display.gph to show at the end of text before showing the slot nr, default is none/NULL
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany


Return to User's Script Documentation

Who is online

Users browsing this forum: No registered users and 72 guests