Conditional File Name

GPC2 script programming for Titan Two. Code examples, questions, requests.

Conditional File Name

Postby USER101 » Sat Dec 15, 2018 10:12 am

Hello,
Can I put the following in my code somewhere and have it record a file named mac_01.gmk?

Code: Select all
uint32 grp = 1;
macro_record("mac_0" + grp + ".gmk");
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby USER101 » Sat Dec 15, 2018 10:15 am

Not getting errors but when I run the following it doesn't seem to work. Not seeing it in macro explorer either.

macro_run("mac_0" + grp + ".gmk");
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby Scachi » Sat Dec 15, 2018 11:21 am

There are no string functions available, so this isn't going to work.
You may be able to merge them on your own.
Example code , doesn't check if the merged string fits into its destination and no idea if this is safe to use, but seems to be working:
Code: Select all
uint32 grp = 1;
char MacName[12];
 
init {
  // create a name of src1, number, fill missing digits with zero (four = 0001), append src2, all into dst/MacName
  NameCreate("mac_",grp,4,".gmk",&MacName);
}
 
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
  uint8 i=0;
 
  i=NameChar(i,src1,dst);
  i=NameInt(i,grp,digits,dst);
  i=NameChar(i,src2,dst);
 
  printf("macname/dst:");
  printf(dst);
}
 
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
  uint8 i;
  for (i=0;src[i]!='\0';i++) {
    printf("char: %c",src[i]); // printf current char
    //
    dst[index]=src[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
 
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
  uint8 i=0;
  uint8 iThis;
 
  // number of digits
  uint8 iDigits=1;
  while(Num/pow(10,iDigits)) iDigits++;
 
  // fill missing number of digits with 0
  for (i=iDigits;i<digits;i++) {
    dst[index]='0';
    printf("char: %c",'0'); // printf current char
    index++;
  }
 
  for (i=0;i<iDigits;i++) {
    iThis=Num/pow(10,(iDigits-(i+1)));
    Num=Num-iThis*pow(10,iDigits-i-1);
    dst[index]=iThis+48;
    printf("char: %c",iThis+48); // printf current char
    index++;
  }
  dst[index]='\0';
  return index;
}
 


Output:
Code: Select all
 
GVM Output: char: m
GVM Output: char: a
GVM Output: char: c
GVM Output: char: _
GVM Output: char: 0
GVM Output: char: 0
GVM Output: char: 0
GVM Output: char: 1
GVM Output: char: .
GVM Output: char: g
GVM Output: char: m
GVM Output: char: k
GVM Output: macname/dst:
GVM Output: mac_0001.gmk
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Conditional File Name

Postby USER101 » Sat Dec 15, 2018 6:59 pm

If I understand correctly

Code: Select all
macro_record(dst);


Should create and record to the file mac_0001.gmk and that's all based on the current grp variable?
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby Scachi » Sat Dec 15, 2018 7:14 pm

You have to use:
Code: Select all
macro_record(&MacName);

after you called
Code: Select all
NameCreate("mac_",grp,4,".gmk",&MacName);

as this write the name into MacName.

When grp is 1 it will result in "mac_0001.gmk"
When grp is 123 it will result in "mac_0123.gmk"

This code record 10 seconds and creates the file mac_0001.gmk:
Code: Select all
uint32 grp = 1;
char MacName[12];
 
init {
  // create a name of src1, number, fill missing digits with zero (four = 0001), append src2
  NameCreate("mac_",grp,4,".gmk",&MacName);
  macro_rec(&MacName); // start recording
}
 
main {
  if ( macro_time() > 10000 ) {
    macro_stop();
    printf("stopped recording");
  }
}
 
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
  uint8 i=0;
 
  i=NameChar(i,src1,dst);
  i=NameInt(i,grp,digits,dst);
  i=NameChar(i,src2,dst);
 
  printf("macname/dst:");
  printf(dst);
}
 
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
  uint8 i;
  for (i=0;src[i]!='\0';i++) {
    printf("char: %c",src[i]); // printf current char
    //
    dst[index]=src[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
 
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
  uint8 i=0;
  uint8 iThis;
 
  // number of digits
  uint8 iDigits=1;
  while(Num/pow(10,iDigits)) iDigits++;
 
  // fill missing number of digits with 0
  for (i=iDigits;i<digits;i++) {
    dst[index]='0';
    printf("char: %c",'0'); // printf current char
    index++;
  }
 
  for (i=0;i<iDigits;i++) {
    iThis=Num/pow(10,(iDigits-(i+1)));
    Num=Num-iThis*pow(10,iDigits-i-1);
    dst[index]=iThis+48;
    printf("char: %c",iThis+48); // printf current char
    index++;
  }
  dst[index]='\0';
  return index;
}
 

If you have trouble using this PM me your code and I'll try to put that into it.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Conditional File Name

Postby USER101 » Sat Dec 15, 2018 10:34 pm

That worked perfect. Thank You!!!
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby USER101 » Mon Dec 17, 2018 10:48 pm

Scachi wrote:You have to use:
Code: Select all
macro_record(&MacName);

after you called
Code: Select all
NameCreate("mac_",grp,4,".gmk",&MacName);

as this write the name into MacName.

When grp is 1 it will result in "mac_0001.gmk"
When grp is 123 it will result in "mac_0123.gmk"

This code record 10 seconds and creates the file mac_0001.gmk:
Code: Select all
uint32 grp = 1;
char MacName[12];
 
init {
  // create a name of src1, number, fill missing digits with zero (four = 0001), append src2
  NameCreate("mac_",grp,4,".gmk",&MacName);
  macro_rec(&MacName); // start recording
}
 
main {
  if ( macro_time() > 10000 ) {
    macro_stop();
    printf("stopped recording");
  }
}
 
void NameCreate(char *src1, uint32 grp, uint8 digits, char *src2, char *dst) {
  uint8 i=0;
 
  i=NameChar(i,src1,dst);
  i=NameInt(i,grp,digits,dst);
  i=NameChar(i,src2,dst);
 
  printf("macname/dst:");
  printf(dst);
}
 
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
  uint8 i;
  for (i=0;src[i]!='\0';i++) {
    printf("char: %c",src[i]); // printf current char
    //
    dst[index]=src[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
 
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
  uint8 i=0;
  uint8 iThis;
 
  // number of digits
  uint8 iDigits=1;
  while(Num/pow(10,iDigits)) iDigits++;
 
  // fill missing number of digits with 0
  for (i=iDigits;i<digits;i++) {
    dst[index]='0';
    printf("char: %c",'0'); // printf current char
    index++;
  }
 
  for (i=0;i<iDigits;i++) {
    iThis=Num/pow(10,(iDigits-(i+1)));
    Num=Num-iThis*pow(10,iDigits-i-1);
    dst[index]=iThis+48;
    printf("char: %c",iThis+48); // printf current char
    index++;
  }
  dst[index]='\0';
  return index;
}
 

If you have trouble using this PM me your code and I'll try to put that into it.


Quick question about your code. I see that your void statement and some other stuff are below your main. Is that because you are treating them like a function? Does that stuff need to be below the main and combo section for it to work properly? Mine is working baring a change to the the variable name but I have it after combo as well.
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby Scachi » Mon Dec 17, 2018 11:34 pm

Yes, they are functions.

You can mix the order with gpc2 / Titan Two. The T1 wasn't that flexible.
There is still an order on how they will be started when using all at the same time (init, same main loop run/same function):
Functions first, in the order how you start them, not how the are declared.
Combos in the order in how they are declared, not in the order how they are started in the main loop.

I call the cSecond combo first, it still runs the cFirst combo first as I have declared them in this order.
Code: Select all
 
init {
                                                // order
  printf("init entering");                        // 1.
  fSecond();                                      // 2.
  fFirst();                                       // 3.
  combo_run(cSecond);                             // 6.
  combo_run(cFirst);                              // 5.
  printf("init leaving");                         // 4.
}
 
void fFirst() {
   printf("function First");
 }
 
void fSecond() {
  printf("function Second");
}
 
void fThird() {
   printf("function Third run by combo Second")// 8.
 }
 
combo cFirst {
  printf("combo cFirst"); wait(0);                // 5.
  fFourth();                                      // 7.
}           
 
combo cSecond {           
  printf("combo cSecond"); wait(0);               // 6.
  fThird();                                       // 8.
}
 
void fFourth() {
  printf("function Fourth run by combo First");   // 7.
}
 
 


Output:
Code: Select all
GVM Output: init entering
GVM Output: function Second
GVM Output: function First
GVM Output: init leaving
GVM Output: combo cFirst
GVM Output: combo cSecond
GVM Output: function Fourth run by combo First
GVM Output: function Third run by combo Second
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Conditional File Name

Postby USER101 » Tue Dec 18, 2018 12:12 am

Thanks for the explanation, I have been reading both the T1 and T2 documentation so I can better tell when each type of code is being used in examples.

So I decided to try and make the entire name conditional.
Just trying to wrap my head around how you are composing the string. I made individual single digit variables for the first 4 digits and the last 4 digits dedicated to a variable from 0001-9999.. This compiles and runs but the output is not what I expected..
It is supposed to be "F1RA0001.gmk"
actual output/
GVM Output: XZ\
0001.gmk

Code: Select all
#pragma METAINFO("<author_name>", 1, 0, "")
#include "ColorLED.gph"
#include <xb1.gph>
#ifndef XB1_GPH_
#define XB1_GPH_
 
#define XB1_GUIDE       BUTTON_1
#define XB1_VIEW        BUTTON_2
#define XB1_MENU        BUTTON_3
#define XB1_RB          BUTTON_4
#define XB1_RT          BUTTON_5
#define XB1_RS          BUTTON_6
#define XB1_LB          BUTTON_7
#define XB1_LT          BUTTON_8
#define XB1_LS          BUTTON_9
#define XB1_UP          BUTTON_10
#define XB1_DOWN        BUTTON_11
#define XB1_LEFT        BUTTON_12
#define XB1_RIGHT       BUTTON_13
#define XB1_Y           BUTTON_14
#define XB1_B           BUTTON_15
#define XB1_A           BUTTON_16
#define XB1_X           BUTTON_17
#define XB1_P1          BUTTON_18
#define XB1_P2          BUTTON_19
#define XB1_P3          BUTTON_20
#define XB1_P4          BUTTON_21
#define XB1_RX          STICK_1_X
#define XB1_RY          STICK_1_Y
#define XB1_LX          STICK_2_X
#define XB1_LY          STICK_2_Y
 
#endif /* XB1_GPH_ */
#define irand(a,b) (((int)(rand() * (fix32)(b+1 - a))) + a)
 
        bool activity(){ // Checks if any buttons I/O, (1 thru 24) are active.
        int i = 25;
        while(i--){
            if(is_active(i)) return TRUE;
            }
        return FALSE
        }
 
        bool sleep = FALSE;
        uint8 com;
        uint32 sleep_timer;
        uint32 grp = 1;// up to four digit number
        uint32 mode = "F";// going to be a single letter
        uint32 group = 1; // going to be a number from 1-9
        uint32 type = "R";// going to be a single letter
        uint32 set = "A"; // going to be a single letter
 
char MacName[12];
 
init {
  // create a name of src1, number, fill missing digits with zero (four = 0001), append src2
  NameCreate(mode,group,type,set,grp,4,".gmk",&MacName);
}
 
 
 
main {
 
    if(activity()){ // Check if any button is being pressed.
    printf(&MacName);}
}
 
void NameCreate(uint8 mode, uint8 group, uint8 type, uint8 set, uint32 grp, uint8 digits, char *src2, char *dst) {
  uint8 i=0;
 
  i=NameMode(i,mode,dst);
  i=NameGroup(i,group,dst);
  i=NameType(i,type,dst);
  i=NameSet(i,set,dst);
  i=NameInt(i,grp,digits,dst);
  i=NameChar(i,src2,dst);
 
  printf("macname/dst:");
  printf(dst);
}
 
// add string
uint8 NameChar(uint8 index, char *src, char *dst) {
  uint8 i;
  for (i=0;src[i]!='\0';i++) {
    printf("char: %c",src[i]); // printf current char
    //
    dst[index]=src[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
uint8 NameMode(uint8 index, char mode, char *dst) {
  uint8 i;
  for (i=0;mode[i]!='\0';i++) {
    printf("char: %c",mode[i]); // printf current char
    //
    dst[index]=mode[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
uint8 NameSet(uint8 index, char set, char *dst) {
  uint8 i;
  for (i=0;set[i]!='\0';i++) {
    printf("char: %c",set[i]); // printf current char
    //
    dst[index]=set[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
uint8 NameGroup(uint8 index, char group, char *dst) {
  uint8 i;
  for (i=0;group[i]!='\0';i++) {
    printf("char: %c",group[i]); // printf current char
    //
    dst[index]=group[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
uint8 NameType(uint8 index, char type, char *dst) {
  uint8 i;
  for (i=0;type[i]!='\0';i++) {
    printf("char: %c",type[i]); // printf current char
    //
    dst[index]=type[i];
    index++;
  }
  dst[index]='\0';
  return index;
}
 
// number to char conversion and add to string
uint8 NameInt(uint8 index, int32 Num, uint8 digits, char *dst) {
  uint8 i=0;
  uint8 iThis;
 
  // number of digits
  uint8 iDigits=1;
  while(Num/pow(10,iDigits)) iDigits++;
 
  // fill missing number of digits with 0
  for (i=iDigits;i<digits;i++) {
    dst[index]='0';
    printf("char: %c",'0'); // printf current char
    index++;
  }
 
  for (i=0;i<iDigits;i++) {
    iThis=Num/pow(10,(iDigits-(i+1)));
    Num=Num-iThis*pow(10,iDigits-i-1);
    dst[index]=iThis+48;
    printf("char: %c",iThis+48); // printf current char
    index++;
  }
  dst[index]='\0';
  return index;
}
 
 
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: Conditional File Name

Postby Scachi » Tue Dec 18, 2018 9:48 am

I have uploaded a header file for easier usage and example code to the Online Resource, search for "StrCreate"
Here is some information about it: viewtopic.php?f=23&t=11154&p=76624#p76624

You don't want to use a separate function for each segment of your file name, you may only need a different/additional function when the data type of variable is a different one.
The header file provides three different functions for adding: char*, char, uint32
Examples to recognize the different types used:
char *stringlike = ".gmk";
char singlechar = 'c';
uint32 number = 1421;

You may need to read some general information about functions and data types to get a better understanding of it and their usage. I am not very good at explaining stuff like that.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Next

Return to GPC2 Script Programming

Who is online

Users browsing this forum: Baidu [Spider] and 109 guests

cron