Write variable to card - Script Writing - CHDK Forum

Write variable to card

  • 6 Replies
  • 4952 Views
Write variable to card
« on: 29 / May / 2010, 20:37:48 »
Advertisements
This might be something obvious and I'm just missing it, but is there a way to write a variable to the SD card in uBasic so that I could read it again after a camera power cycle?  I'd like to save a configuration and load it each time at script start up.

*

Offline reyalp

  • ******
  • 14125
Re: Write variable to card
« Reply #1 on: 29 / May / 2010, 20:53:08 »
This might be something obvious and I'm just missing it, but is there a way to write a variable to the SD card in uBasic so that I could read it again after a camera power cycle?  I'd like to save a configuration and load it each time at script start up.
Nope. You can do all kinds of file IO in lua however. See http://chdk.wikia.com/wiki/LUA#LUA_standard_libraries:_io.2C_os_and_string

The lib is generally compatible with the lua documented standard.

Doing general file IO in ubasic would be a bit difficult, because of the limited datatypes. I suppose commands like "save variable" and "load variable" would be fairly easy, but IMO lua is a better choice for anything but trivial scripts.
Don't forget what the H stands for.

Re: Write variable to card
« Reply #2 on: 29 / May / 2010, 21:01:09 »
Using lua would be great, however I'm using SDM for the playback_mode and movie_mode commands, which are necessary.  I can't find any equivalent in lua...

*

Offline reyalp

  • ******
  • 14125
Re: Write variable to card
« Reply #3 on: 29 / May / 2010, 21:44:43 »
Using lua would be great, however I'm using SDM for the playback_mode and movie_mode commands, which are necessary.  I can't find any equivalent in lua...
See http://chdk.wikia.com/wiki/CHDK_firmware_usage/MoreBest#set_record.28state.29 and
http://chdk.wikia.com/wiki/LUA/Scripts:Standard/Lualib/Capmode

Example at http://chdk.wikia.com/wiki/LUA/Scripts:Standard/Test/Setmode

Or get Microfunguy to add file ops to ubasic.
Don't forget what the H stands for.

Re: Write variable to card
« Reply #4 on: 30 / May / 2010, 05:30:53 »
get Microfunguy to add file ops to ubasic.

Too difficult for me ...

What variable do you want to save ?

Are parameter sets of any use ?

See :-

http://stereo.jpn.org/eng/sdm/ubasic.htm


Incidentally, I think you can now switch to record and playback using CHDK.


David

Re: Write variable to card
« Reply #5 on: 18 / June / 2010, 16:06:48 »
Thanks for the help on this one.

I've gone the route of adding some commands to SDM uBasic.  One will write a variable to the card, the other will recall that variable.  If anyone's interested, I've posted the functions below.

Code: [Select]
static void set_config_statement()
{
int fd;
char config[1];
accept(TOKENIZER_SET_CONFIG);
config[0] = (char)expr();
// write config to file
fd = open("A/CHDK/OBC.CFG", O_WRONLY|O_CREAT, 0777);
if (fd>0)
{
write(fd, config, 1);
close(fd);
}
accept_cr();
}

static void get_config_statement()
{
int fd, var, rd;
char config[1];
accept(TOKENIZER_GET_CONFIG);
var = tokenizer_variable_num();
accept(TOKENIZER_VARIABLE);
// read config from file
fd = open("A/CHDK/OBC.CFG", O_RDONLY, 0777);
if (fd>0)
{
rd = read(fd, config, 1);
if (rd>0)
{
// put config into variable
ubasic_set_variable(var, (int)config[0]);
}
close(fd);
}
accept_cr();
}

Cheers,

Peter

Re: Write variable to card
« Reply #6 on: 18 / June / 2010, 17:18:23 »
Hi Peter.

Good to see that someone has added new functions to the SDM code.

As far as I know, this is the first known instance !


David

 

Related Topics


SimplePortal © 2008-2014, SimplePortal