Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Archived scripts => Topic started by: a1ex on January 26, 2013, 01:24:01 AM

Title: PicoC scripting API
Post by: a1ex on January 26, 2013, 01:24:01 AM
Current nightly builds (http://nanomad.magiclantern.fm/nightly/) can now run PicoC (http://code.google.com/p/picoc/) scripts :)

(http://a1ex.magiclantern.fm/bleeding-edge/picoc1.png) (http://a1ex.magiclantern.fm/bleeding-edge/picoc2.png) (http://a1ex.magiclantern.fm/bleeding-edge/sokoban.png)



General-purpose functions
[spoiler]

void sleep(float seconds);                            // sleep X seconds
void beep();                                          // short beep sound
void beeps(int num);                                  // short consecutive beeps
void console_show();                                  // show the script console
void console_hide();                                  // hide the script console
void cls();                                           // clear the script console
void screenshot();                                    // take a screenshot (BMP+422)
unsigned int rand();                                  // random numbers


[/spoiler]

Date/time
[spoiler]


struct tm
{
     int hour;
     int minute;
     int second;
     int year;
     int month;
     int day;
}

struct tm * get_time();                               // get current date/time
float get_uptime();                                   // get uptime, in seconds, 1ms resolution (from DIGIC clock)


[/spoiler]

Picture taking
[spoiler]

void takepic();                                       // take a picture
void bulbpic(float seconds);                          // take a picture in bulb mode
void take_fast_pics(int number);                      // take N pictures in burst mode

void wait_pic();                                      // waits until you take a picture (e.g. for starting a custom bracket sequence)


[/spoiler]

Video recording
[spoiler]

void movie_start();                                   // start recording
void movie_end();                                     // stop recording


[/spoiler]

Key press emulation
[spoiler]


Available button codes:
     LEFT, RIGHT, UP, DOWN, SET, MENU, PLAY, ERASE, LV, INFO, ZOOM_IN
     SHOOT_FULL, SHOOT_HALF, AF_ON
     Q, ZOOM_OUT (if present on your camera)
     UNPRESS (for key input only)

void press(int button);                               // "press" a button
void unpress(int button);                             // "unpress" a button
void click(int button);                               // "press" and then "unpress" a button


[/spoiler]

Key input
[spoiler]


Two methods:
- blocking:      int key = wait_key();      // waits for key to be pressed, then returns the key code
- non-blocking:  int key = last_key();     // returns the last key code without waiting (or -1)

Keys are trapped when you call one of those, and also 1 second after. This lets you write loops like:

while(1)
{
    int key = wait_key();

    // process the key
    if (key == SET) { ... }
}

or

while(1)
{
    int key = last_key();

    // process the key
    if (key == SET) { ... }

    sleep(0.1);
}

int wait_key();                                       // waits until you press some key, then returns key code
int last_key();                                       // returns last key pressed, without waiting


[/spoiler]

Exposure settings
[spoiler]


// APEX units
float get_tv();                                       
float get_av();                                       
float get_sv();                                       
void set_tv(float tv);                               
void set_av(float av);                               
void set_sv(float sv);                               

// Conventional units ( ISO 100, 1.0/4000, 2.8 )
int get_iso();                                       
float get_shutter();                                 
float get_aperture();                                 
void set_iso(int iso);                               
void set_shutter(float s);                           
void set_aperture(float s);                           

// Raw units (1/8 EV steps)
int get_rawiso();                                     
int get_rawshutter();                                 
int get_rawaperture();                               
void set_rawiso(int raw);                             
void set_rawshutter(int raw);                         
void set_rawaperture(int raw);                       


[/spoiler]

Exposure compensation (in EV)
[spoiler]

float get_ae();                                       
void set_ae(float ae);                               


[/spoiler]

Flash functions
[spoiler]

int get_flash();                                      // 1=enabled, 0=disabled, 2=auto
int set_flash(int enabled);                           
int pop_flash();                                      // pop-up built-in flash
float get_flash_ae();                                 
void set_flash_ae(float ae);                          // flash exposure compensation


[/spoiler]

White balance
[spoiler]

int get_kelvin();                                     
int get_green();                                     
void set_kelvin(int k);                               // from 1500 to 15000
void set_green(int gm);                               // green-magenta shift, from -9 to 9


[/spoiler]

Focus
[spoiler]

void focus(int steps);                                // move the focus ring by X steps
void focus_setup(int stepsize, int delay, int wait);  // see Focus -> Focus Settings menu
int get_focus_confirm();                              // return AF confirmation state (outside LiveView, with shutter halfway pressed)

void set_af(int af);                                  // enable or disable AF for half-shutter press
void reset_af(int af);                                // restore the original setting from Canon menu (CFn) (auto-called when script finishes)

int get_afma(int mode);                               // get AF microadjust value
void set_afma(int value, int mode);                   // set AF microadjust value

struct dof
{
     char* lens_name;
     int focal_len;
     int focus_dist;
     int dof; int far;
     int near;
     int hyperfocal;
}

struct dof * get_dof();                               


[/spoiler]

Low-level I/O
[spoiler]

void mic_out(int value);                              // digital output via microphone jack, by toggling mic power
void set_led(int led, int value);                     // set LED state; 1 = card LED, 2 = blue LED



[/spoiler]

Graphics
[spoiler]

Graphics constants:

Colors:
    COLOR_EMPTY, COLOR_BLACK, COLOR_WHITE, COLOR_BG,
    COLOR_RED, COLOR_DARK_RED, COLOR_GREEN1, COLOR_GREEN2, COLOR_BLUE, COLOR_LIGHT_BLUE,
    COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_ORANGE,
    COLOR_ALMOST_BLACK, COLOR_ALMOST_WHITE,
    COLOR_GRAY(percent)

Fonts:
    FONT_LARGE, FONT_MED, FONT_SMALL
    FONT(fnt, fg, bg)
    SHADOW_FONT(fnt)
    e.g. FONT(FONT_LARGE, COLOR_YELLOW, COLOR_BLACK) or SHADOW_FONT(FONT_MED)

void clrscr();                                        // clear screen
int get_pixel(int x, int y);                         
void put_pixel(int x, int y, int color);             
void draw_line(int x1, int y1, int x2, int y2, int color);
void draw_line_polar(int x, int y, int radius, float angle, int color);
void draw_circle(int x, int y, int radius, int color);
void fill_circle(int x, int y, int radius, int color);
void draw_rect(int x, int y, int w, int h, int color);
void fill_rect(int x, int y, int w, int h, int color);


[/spoiler]

Text output
[spoiler]

void bmp_printf(int fnt, int x, int y, char* fmt, ...);
void notify_box(float duration, char* fmt, ...);     


[/spoiler]

Interaction with Canon GUI
[spoiler]

void set_canon_gui(int enabled);                      // allow disabling Canon graphics
void set_gui_mode(int mode);                          // set Canon GUI mode (current dialog, 0=idle, 1=play, 2=menu, others camera-specific)
int get_gui_mode();                                   


[/spoiler]

Interaction with ML menus
[spoiler]

// Tip: to get a list with menus and possible values, go to Prefs -> Config File -> Export as PicoC script
// You can also use these functions to create custom presets.
void menu_open();                                     // open ML menu
void menu_close();                                    // close ML menu
void menu_select(char* tab, char* entry);             // select a menu tab and entry (e.g. Overlay, Focus Peak)
int menu_get(char* tab, char* entry);                 // return the raw (integer) value from a menu entry
int menu_set(char* tab, char* entry, int value);      // set a menu entry to some arbitrary value; 1 = success, 0 = failure
char* menu_get_str(char* tab, char* entry);           // return the displayed (string) value from a menu entry
int menu_set_str(char* tab, char* entry, char* value); // set a menu entry to some arbitrary string value (cycles until it gets it); 1 = success, 0 = failure


[/spoiler]

Image analysis
[spoiler]



Don't expect too much; it's quite slow to operate at pixel level due to interpreter overhead.
Consider using plain C for this task.

Functions that operate on larger data sets (e.g. spotmeter, histogram analysis) should be fast enough.

struct yuv
{
     int Y; // 0...255
     int U; // -128...127
     int V; // -128...127
}

struct rgb
{
     int R; // 0...255
     int G; // 0...255
     int B; // 0...255
}

// The following functions operate on normalized coordinates (720x480)
// This means they should work on external monitors without extra care, for example.
// Details: http://magiclantern.wikia.com/wiki/VRAM/Geometry
struct yuv * get_pixel_yuv(int x, int y);             // get the YUV components of a pixel from LiveView buffer
struct rgb * get_pixel_rgb(int x, int y);             // similar for RGB
struct yuv * get_spot_yuv(int x, int y, int size);    // spotmeter: average pixels from a (small) box and return average YUV.
struct rgb * get_spot_rgb(int x, int y, int size);    // similar for RGB

For low-level image processing

struct vram
{
     void* buffer;
     int width;
     int pitch;
     int height;
}
struct vram * get_lv_vram();                          // get LiveView image buffer
struct vram * get_hd_vram();                          // get LiveView recording buffer



[/spoiler]

Powersaving
[spoiler]

void display_on();                                   
void display_off();                                   
int display_is_on();                                 

void lv_pause();                                      // pause LiveView without dropping the mirror
void lv_resume();                                     


[/spoiler]

Cache hacking
[spoiler]

unsigned int cache_locked();                         
void cache_lock();                                   
void cache_unlock();                                 
void cache_fake(unsigned int address, unsigned int data, unsigned int type);


[/spoiler]

Camera info
[spoiler]

char* get_model();                                   
char* get_firmware();                                 

[/spoiler]


Sample scripts
hello.c (https://bitbucket.org/hudson/magic-lantern/raw/tip/scripts/hello.c) - first steps
brack.c (https://bitbucket.org/hudson/magic-lantern/raw/tip/scripts/brack.c) - custom bracketing sequence
keys.c (https://bitbucket.org/hudson/magic-lantern/raw/tip/scripts/keys.c) - key press demo
test.c (https://bitbucket.org/hudson/magic-lantern/raw/tip/scripts/test.c) - API tests, and usage example for each function
sokoban.c (https://bitbucket.org/hudson/magic-lantern/raw/tip/scripts/sokoban.c) - a simple game

To create a script, just place it in the ML/SCRIPTS directory, with the C extension, and they will be autodetected. Use short 8.3 names. You can place max 15 scripts there.

Startup script
If one of the scripts is named AUTORUN.C, it will be executed at startup, automatically.

Looking forward to see what you can do with these scripts. You can use this forum section to share them.

TODO:
- Extend the API (and keep it clean). Here I'd like to see your suggestions. Feel free to implement the todo list (commented APIs) from picoc/library_ml.c (https://bitbucket.org/hudson/magic-lantern/src/tip/picoc/library_ml.c#cl-555).
- [DONE] Edit script parameters (like CHDK).
- In-camera script editing (so you can change small things in the field).
- Compile a desktop PicoC interpreter (so you can try the scripts offline). ML will run in QEMU
- etc
Title: Re: PicoC scripting API
Post by: SDX on January 26, 2013, 01:40:42 AM
No clue on how easy it would be to implement this and if PicoC would allow something like it. But how about parameters? When executing a script, a few parameters can be defined by the user. Would be very useful and make the In-Camera editor (which would be allot of work to implement as well) sort of needless.

But otherwise I really welcome this addition. PicoC is very easy to learn and many might benefit from this.

Suggestions:
GUI stuff: print(); or even set_pixel(); get_pixel();?
Focus stuff: get_af(); set_focus_distance(); get_focus_distance();

EDIT: I'm blind, you even mention parameters in the ToDo list..

Title: Re: PicoC scripting API
Post by: 1% on January 26, 2013, 05:03:25 AM
still pushes my bins over 500K, couldn't we just increase bin size and  allocate more memory? 500k not enough if you use o2 or o3
Title: Re: PicoC scripting API
Post by: a1ex on January 26, 2013, 09:19:57 AM
On 60D there is ~480K in the small buffer (currently 430K for ML and 50K free). There's no space to load ML on the large one (1MB left, but Canon code requires it). 600D/1100D aren't much better. I wouldn't increase binary size in the main tree, but if you have a lot of free RAM you can experiment with it.

If you compile with O2/O3, you can optimize only the modules you need.
Title: Re: PicoC scripting API
Post by: scrax on January 26, 2013, 10:14:55 AM
How about close consolle after script execution? On 600D after the script has ended only way to go back to camera is halfshutter, other buttons are working normally buy dialog screens can't be seen since console is over all.
When console is opened to show the script (so we stay in ML menu screen) console goes away pressing any button and is better

EDIT: Also focus peak is broke now (like wrong image size seems), only for me? Fix worked
Title: Re: PicoC scripting API
Post by: wolf on January 26, 2013, 12:55:54 PM
Is it possible to extend the API to add a new menu entry with

menu_add( "NewMenu", New_menus, COUNT(New_menus) );

The menu is already made for changing values and does it very good. This routine sets even the icons automatically.
The menu panel could also serve as a console.
I can imagine that it's not that easy and there is no menu_go_away() function.
Title: Re: PicoC scripting API
Post by: nanomad on January 26, 2013, 12:58:06 PM
AFAIK menus are statically compiled into ML (the MENU_ADD is actually a pre-processor macro)
Title: Re: PicoC scripting API
Post by: wolf on January 26, 2013, 01:06:25 PM
I think it's dynamically. I changed the testplug.c and it's possible to add a menu after ML has started.

(http://i47.tinypic.com/qsa6nt.jpg)
Title: Re: PicoC scripting API
Post by: a1ex on January 26, 2013, 01:47:39 PM
Check the latest changeset: now each script has its own menu entry and its own submenu, and script parameters could be added there.

Creating menus on the fly it's a bit tricky, I prefer to avoid it.
Title: Re: PicoC scripting API
Post by: scrax on January 26, 2013, 02:38:55 PM
I've tried hello script and was only 1 pic left of space on card so the script got hang and no way to stop it, can we add a stop script option in the script submenu? I can see it showing script going on right now.
Title: Re: PicoC scripting API
Post by: garry23 on January 26, 2013, 03:15:27 PM
Regarding extending scripting functions/calls, as a CHDK user as well as a ML user, would it be a good start to emulate some of the CHDK scripting functionality. For instance, I have an autobracketing script that runs in CHDK where i access a CHDK histogram call to get the histogram at a 12bit level, ie the level my S95 and G11 operates at. This page gives an example of the functions http://chdk.wikia.com/wiki/CHDK_Scripting_Cross_Reference_Page

Just a thought, as the CHDK community have done a lot for scripting of P&S cameras, so why reinvent the wheel, lets bootstrap!
Title: Re: PicoC scripting API
Post by: a1ex on January 26, 2013, 03:41:55 PM
Yes, CHDK is a good starting point. But achieving 100% compatibility is not very easy (histogram here is 8 bit, EV values are not in 1/96 EV steps, but in 1/8 and so on.

Some things are easy to port (draw_line, draw_pixel, reboot,  set_backlight,  set_led).
Title: Re: PicoC scripting API
Post by: garry23 on January 26, 2013, 03:55:36 PM
Alex

Some of the CHDK community have sent time pushing the limits. For instance, as I say, I use a histo call that covers 12bits, and it could be extended to 14bit. It is achieved by sampling  the sensor RAW. Also all the exposure calls work in APEX 96 space.

What is was suggesting is that at least we look at where CHDK is as we construct an ML requirements set of functions. For instance, I know one of the CHDK community has added in functions in CHDK few sensor level exposure reading such that in camera  holly grail timelapse can be smoothly achieved.

Obviously some of the ML functions will be unique to ML, eg mirror up functions, if possible, however, any will and I suggest should be common.

Bottom line: whatever the ML. Developers, such as yourself, can do will be great; IMHO there could be beneficial if the ML community took stock of the CHDK scripting status, especially in the 1.2 developers trunk.
Title: Re: PicoC scripting API
Post by: 1% on January 26, 2013, 06:17:44 PM
QuoteIf you compile with O2/O3, you can optimize only the modules you need.

Still a bit too big. on 6D there seems to be more ram. wish i could have both scripts and optimization. camera i much snappier with the opts
Title: Re: PicoC scripting API
Post by: a1ex on January 26, 2013, 10:28:27 PM
Script parameters seem to work :)

They use CHDK syntax: @title, @param, @default and @range. They are not yet saved to config file (you can only change them in RAM).

@wolf, can you try to port your timelapse script, and see what's missing?
Title: Re: PicoC scripting API
Post by: wolf on January 26, 2013, 11:20:57 PM
Is there a possibilty to display a string which can be updated?

I try to change it to picoc runable without displaying the lasting time human readable.


....
        .name = "Duration",
        .priv = &v,
        .max = 0,
        .choices = (const char *[]) {time_str},
        .help = "",
....
....
char* sec_to_time(int seconds)
{
    int minutes, hours;
    hours=seconds % 86400 / 3600;
    minutes=seconds % 3600 / 60;
    seconds=seconds % 60;
    const char colon[] = ":";
    snprintf(time_str,sizeof(time_str), "%02d%s%02d%s%02d",hours,colon,minutes,colon,seconds );
    return time_str;
}
.....
Title: Re: PicoC scripting API
Post by: a1ex on January 26, 2013, 11:26:03 PM
You can't interact with the menu, but you can print it at the console, with plain printf.
Title: Re: PicoC scripting API
Post by: wolf on January 26, 2013, 11:37:50 PM
Only for design reasons, is it very unlikely that the menu could be interactive one day? I am very happy with ML the way it is right now.

And do you think "bmp_printf" should be added to the API, I guess it's not that hard.
In my plugin I print some info with small letters in the corner.
Title: Re: PicoC scripting API
Post by: wolf on January 27, 2013, 01:03:20 PM
I just thought while trying picoc, if it would be better to change the menu items and add an exit option when selecting a script instead of open a submenu.

QuoteCreating menus on the fly it's a bit tricky, I prefer to avoid it.
also changing?

The benefit of it would  be that the selected script would be already present when switching back to ML after LV for example.

The main reason for my timelaps script was , that I wanted to know the duration of the timelaps and to play with ML.  ;)
But I am not sure if interactivity is that important,  I guess in most scripts it's not needed. If it is needed, it can be already done!

My first script: I always wanted to take a picture at a specific time!
/*
@title Shoot at a time
@param h Hour
@range h 0 23
@param m Minute
@range m 0 59
@param s Second
@range s 0 59
*/
struct tm * t = get_time();
int hour,minute,second;
while ((hour != h) ||  (minute != m) ||  (second != s))
{
    t = get_time();
    hour = t->hour;
    minute = t-> minute;
    second = t->second;
    sleep(0.9);
}
takepic();
printf("Done :)\n");



Title: Re: PicoC scripting API
Post by: scrax on January 27, 2013, 02:48:11 PM
On 600D I can't see the options pictured in the OP to enable the test.c script
Title: Re: PicoC scripting API
Post by: wolf on January 27, 2013, 09:35:20 PM
Does anybody know if the letters of @param have to be in alphabetical order? I get incorrect Parameter Text when letters aren't in order.
Title: Re: PicoC scripting API
Post by: a1ex on January 27, 2013, 10:02:53 PM
Fixed. I wonder why CHDK guys didn't notice it, since it was from their code.
Title: Re: PicoC scripting API
Post by: scrax on February 05, 2013, 06:26:31 PM
what about this program http://www.zenoshrdlu.com/kapstuff/zubdb.html will it work with ML scripts? only ubasic
Title: Re: PicoC scripting API
Post by: wolf on February 06, 2013, 12:21:43 PM
Wrote a very basic and rough picoc-ml parser bash script to try and test a script outside the camera. I just implemented the functions I needed lately.
Usage: ml_picoc_parser.sh "script-file"

parser:

#!/bin/bash
# ml_picoc_parser.sh
echo "//temporary file just delete me" > foo.c
echo "#include<ml_functions.h>" >> foo.c
FILE=$1
while read line;do
        a=( $line )
        if [ "${a[0]}" = "@param" ];then
                echo "int ${a[1]};" >> foo.c
        fi
        if [ "${a[0]}" = "@default" ];then
                echo "${a[1]}=${a[2]};" >> foo.c
        fi
done < "$FILE"
cat $1>>foo.c

picoc -s foo.c



ml_functions.h can be easily extended

// ml_functions.h

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define SHOOT_HALF 0
#define SHOOT_FULL 1

void beep()
{
    printf("beep\n");
}

void sleep(float n)
{
usleep((int)(n*1000000));
}

void click(int x)
{
if (x==0)
    printf("click(SHOOT_HALF)\n");
if (x==1)
    printf("click(SHOOT_FULL)\n");
}

void console_show()
{
    printf("console_show\n");
}

void console_hide()
{
    printf("console_hide\n");
}


void display_on()
{
    printf("display_on\n");
}


void display_off()
{
    printf("display_off\n");
}


void lv_pause()
{
    printf("lv_pause\n");
}


void lv_resume()
{
    printf("lv_resume\n");
}


Title: Re: PicoC scripting API
Post by: Marsu42 on February 22, 2013, 12:17:59 PM
Imho this is an important feature, so it'd be a pitty if the development should stall - I suggest a script option for triggering ml features: void start_ml_shooting(int feature) for things like focus stack, bracketing or intervalometer to enable this http://www.magiclantern.fm/forum/index.php?topic=4622.0 ... or are these ml features supposed to be started with keypress emulation?

Title: Re: PicoC scripting API
Post by: a1ex on February 24, 2013, 12:19:24 AM
Just added some more APIs:
- key interaction (get_key)
- menu interaction (select a menu item, get/set value)
- DOF and focus info (get_dof: hyperfocal, focus distance, DOF far/near etc)
- set_led
- mic_out (digital output via mic jack)
- focus, focus_setup (in LiveView, like follow focus)
- get/set AFMA

There are some more commented out in picoc/library_ml.c (https://bitbucket.org/hudson/magic-lantern/src/tip/picoc/library_ml.c#cl-555); feel free to implement them.
Title: Re: PicoC scripting API
Post by: scrax on February 24, 2013, 12:54:54 PM
Is already possible to add a label to @param?
For example I want to make set param in the script that make the scrip load or save some settings, so t should show Loan and Save instead of On Off, also another idea on something form complex labels with also the value shown.

/*
@title PicoC script's title

@param a First variable name
@range a 0 1
@labels a Load Save

@param b Second variable name
@range b 0 5
@labels b Off 'C-Mode:%d, @range'
*/
Title: Re: PicoC scripting API
Post by: a1ex on February 24, 2013, 01:01:19 PM
CHDK has @values for this, but it's not implemented yet.

http://chdk.wikia.com/wiki/CHDK_scripting#The_Script_Header
Title: Re: PicoC scripting API
Post by: a1ex on February 24, 2013, 07:59:28 PM
Graphics functions (draw_line, draw_circle, bmp_printf etc) are working.

Anyone would like to write a nice demo script?
Title: Re: PicoC scripting API
Post by: photo360 on February 25, 2013, 11:53:41 AM
Is it possible with PicoC scripting API to do a special bracketing sequence with/without flash?
I need to take some pictures with flash at some shutter speed and to take the same pictures without flash...
this should work on 5DII with canon speedlite 580EX.

Thanks for your help

Exemple of script:

set_aperture(11);
set_iso(100);
set_shutter(1./160);
"flash off"
takepic();

"flash on"
takepic();
Title: Re: PicoC scripting API
Post by: a1ex on February 25, 2013, 12:11:19 PM
I've just added set_flash(1) / set_flash(0), but I have no idea if it works on external flashes.

Also pop_flash() should work for built-in flash.
Title: Re: PicoC scripting API
Post by: photo360 on February 26, 2013, 02:55:36 PM
Thanks !

Some tests:

set_flash(0)/set_flash(1) works great on canon 50D with external flash (canon speedlite 580 EX) or with built-in flash.
pop_flash() does not work on my 50D. The flash shoud be pop up manually.

set_flash(0)/set_flash(1)  does not work en 5DMKII with canon speedlite 580 EX. the flash fire each time.
It seems that this function works only for canon with build-in flash.

Is there another solution for 5DMKII ?
Title: Re: PicoC scripting API
Post by: gerk.raisen on February 27, 2013, 11:57:59 AM
Hello,

please add an API to read the sound dB level so that we can trigger when sound level go up (or also go down) a threshold.

Thank you.
Title: Re: PicoC scripting API
Post by: SDX on February 27, 2013, 01:08:27 PM
Audio Remote Shoot does exactly that - it's in the shoot menu.
Title: Re: PicoC scripting API
Post by: gerk.raisen on February 27, 2013, 02:48:28 PM
Yes, I know that, but the idea is to have an API to use it within a script.
Title: Re: PicoC scripting API
Post by: wolf on February 27, 2013, 07:13:17 PM
You could switch the audio trigger on and off with picoc:

void menu_open();                                     // open ML menu
void menu_close();                                    // close ML menu
void menu_select(char* tab, char* entry);             // select a menu tab and entry (e.g. Overlay, Focus Peak)
int menu_get(char* tab, char* entry);                 // return the raw (integer) value from a menu entry
int menu_set(char* tab, char* entry, int value);      // set a menu entry to some arbitrary value; 1 = success, 0 = failure
char* menu_get_str(char* tab, char* entry);           // return the displayed (string) value from a menu entry
int menu_set_str(char* tab, char* entry, char* value); // set a menu entry to some arbitrary string value (cycles until it gets it); 1 = success, 0 = failure


Title: Re: PicoC scripting API
Post by: Kent on March 02, 2013, 08:34:53 PM
Is it possible to add functionality to export AF mode and the settings for AF Case 1 , Case 2 etc.
as a PicoC script ?
Title: Re: PicoC scripting API
Post by: a1ex on March 02, 2013, 08:54:47 PM
Ouch, that looks painful...
Title: Re: PicoC scripting API
Post by: Kent on March 02, 2013, 08:56:31 PM
Sorry Alex ! LOL
Title: Re: PicoC scripting API
Post by: Alia5 on March 07, 2013, 02:39:55 PM
Would anyone mind extending beep() to beep(int msecs, int frequency)? ;)
Title: Re: PicoC scripting API
Post by: scrax on March 17, 2013, 10:43:21 PM
I can't find a way to change image quality with scripts, seems not yet implemented. To explain better my idea this is the script I'm working on:

/*
@title Custom Burst Shot
@param r Number of raw burst
@range r 0 9
@default r 3
@param l Number of L Jpg burst
@range l 0 9
@defaull r 6
@param m Number of M Jpg burst
@range m 1 29
@default m 16
*/
console_hide();
int quality = get_quality();
int key = wait_key();
while(key != SET){
    if (key == SHOOT_FULL){
        if(r != 0){
            set_quality(RAW);
            take_fast_pics(r);
            sleep(0.1);
        }
        if(l != 0){
            set_quality(JPGLF);
            take_fast_pics(l);
            sleep(0.1);
        }
    }
    set_quality(JPGMF);
    take_fast_pics(m);
    sleep(0.1);
    set_quality_to(quality);
}
if (key == SET){
    break;
}

Title: Re: PicoC scripting API
Post by: a1ex on March 17, 2013, 10:51:03 PM
Yes, it's not implemented. And since some of these constants are camera-specific, and wrong values cause persistent ERR70 (even if you boot without ML), I'm a bit hesitant to add it.

A common subset (e.g. plain RAW and a few JPEG presets) should be OK though.
Title: Re: PicoC scripting API
Post by: scrax on March 17, 2013, 11:37:00 PM
Quote from: a1ex on March 17, 2013, 10:51:03 PM
Yes, it's not implemented. And since some of these constants are camera-specific, and wrong values cause persistent ERR70 (even if you boot without ML), I'm a bit hesitant to add it.

A common subset (e.g. plain RAW and a few JPEG presets) should be OK though.
Totally agree, adding just a safe subset can be enough.

Title: Re: PicoC scripting API
Post by: engardeknave on March 25, 2013, 09:23:11 AM
How about listing/renaming files or even triggering a directory creation/change ala the canon menu? I plan to do one of these things after a bracket set to organize the sets.
Title: Re: PicoC scripting API
Post by: a1ex on March 25, 2013, 09:50:57 AM
That's difficult, since Canon code also expects a catalog of files (not just what's on the filesystem). If you rename the files from the camera, you'll get ERR70.
Title: Re: PicoC scripting API
Post by: engardeknave on March 25, 2013, 05:29:54 PM
What about changing folders exactly as if it were manually done from the menu?
Title: Re: PicoC scripting API
Post by: engardeknave on March 26, 2013, 05:28:47 AM
A couple more things.

1. Right now when I have a script continuously running, it says "script running" when I view photos. Then the console pops up when I try to delete a photo by pressing the set button. There's got to be a way to keep a script completely in the background during normal use. Well that's mostly fixed now.

2. Still really, really want to be able to perform a select folder/create folder programmatically exactly the way it is in the Canon menus.

3. It would be great to be able to get the current camera mode.

4. An easy way to execute scripts from a script.

edit: 5. Location of the focus box in liveview. And maybe even the value of the spotmeter.

This new ability to script is extremely exciting.
Title: Re: PicoC scripting API
Post by: a1ex on March 26, 2013, 02:55:34 PM
2. This needs reverse engineering, we don't know how to do that.

3. should be easy, I'll add it.

4. sounds interesting, but I'm not sure where it would be useful; can you describe some use case for this?

5. should be easy. You can already use custom spotmeters, see img.c (https://bitbucket.org/hudson/magic-lantern/src/tip/scripts/img.c).

Also, we are considering to upgrade the engine to TCC (it's much faster, roughly 1000x, since TCC is a compiler, not an interpreter). Technical discussion: http://www.magiclantern.fm/forum/index.php?topic=4877 . The existing GUI is already working with TCC (just compile with CONFIG_TCC=y instead of CONFIG_PICOC if you want to try it).

The main issues that need to be solved:
- memory usage is a bit higher now (so 60D won't boot with it), but TCC only needs to be loaded when compiling the scripts, and then unloaded.
- compatibility with existing scripts (TCC is more strict, it requires a main function)
- how to stop the scripts? (we could just kill the task, but it's ugly)

The existing API should be easy to port, I don't foresee any difficulties here.
Title: Re: PicoC scripting API
Post by: engardeknave on March 26, 2013, 05:04:49 PM
My purpose in running scripts from a script would be to execute scripts at startup from autorun.c, possibly contingent on some conditions, such as camera mode.

The things I want to do so far will actually work in PicoC, but TCC sounds fantastic.
Title: Re: PicoC scripting API
Post by: scrax on March 27, 2013, 11:28:36 PM
totally agree for 3 and 4.
I fell the need to call other script too from a main script.
Best of this will be to call a script saved in any folder on the card, so to not fill the 11 limit in the main script folder.
That way we can also build a sort of script library that is used by one of the 11 launcher scripts we can run from ML.
For example Marsu42 request of a beep after bracketing could be a script in /EOS_DIGITAL/ML/script/library/ called from a braketing "launcher" script when finished, maybe triggered by a script option.
Same beep script will then be called also from another "launcher" script.

This is really a simple example that could already be done without this implementation but I think that once added it will be used a lot for more interesting things.

TCC is really fascinating, but will put my questions about it in the other topic.
Title: Re: PicoC scripting API
Post by: jplxpto on April 04, 2013, 12:08:32 AM

sorry :)
Title: Re: PicoC scripting API
Post by: engardeknave on April 07, 2013, 07:43:10 AM
I've been waiting for some sort of update to the API. Has anything been added?

I'm especially interested in getting the location of the focus box in LV.

Would it be possible to make a function that "clicks" specific Canon menus?
Title: Scripting the programming of adapter chips
Post by: sfmurph on May 07, 2013, 11:31:56 PM
So I'd like to script the programming of the Optix focus conformation chip I have. I bet folks with the Dandelion chip would also like to. The main things I'd need to do are to check that the camera is in the right mode (M or A, I believe), activate the DOF Preview button and adjust the aperture setting (for Optix) plus set exposure time to 11 seconds (or more) and release the shutter (for Dandelion).

From the API listing, I don't see a way to do the DOF Preview, which would be required for the Optix chips (which I have). There's no "key" for that. My camera doesn't have that key, so I'd need a direct api to activate the DPF preview. The rest do seem available.

Would this be possible?

Here are the Optix programming instructions (http://www.optixpcb.com/pdf/optixv5plusenqk.pdf).
And the Dandelion programming instructions (http://web.archive.org/web/20120605020006/http://filmprocess.ru/oduvanchik/instructions/programming_en.html).
Title: Re: PicoC scripting API
Post by: Francis on May 07, 2013, 11:43:30 PM
There is work in progress to change the scripting language to TCC for a number of performance issues. It is unlikely that the PicoC API will be updated. Unsure if the DOF button press can be emulated but probably. Sticky DOF seems like it would work like that.
Title: Re: PicoC scripting API
Post by: a1ex on May 07, 2013, 11:45:31 PM
You can emulate a DOF press with dofp_set, look in tweaks.c.

To add it to PicoC, look in library_ml.c. Half-shutter and AF_ON are implemented in a similar way.
Title: Re: PicoC scripting API
Post by: arossbach on May 23, 2013, 02:20:04 PM
When will PicoC be available to use?

I'd like to get the value from a custom WB with get_kelvin(). Is there another way to do this without PicoC?
Title: Re: PicoC scripting API
Post by: a1ex on May 23, 2013, 02:24:28 PM
Grab the ufraw source code and port the RGB_to_Temperature. Don't be afraid of matrix math.
Title: Re: PicoC scripting API
Post by: arossbach on May 24, 2013, 01:34:39 AM
I'm not a programmer but can try it :)

I had a first look and imported the code from ufraw to ML. It compiled successfully.
The parameters from WBGain_* have to be converted, but I cannot see any porting issues at the moment.

I have some problems to link the binary on my system (cygwin), so I can't test it and measure the performance.
Title: Re: PicoC scripting API
Post by: palark93 on May 25, 2013, 03:26:07 AM
cant seem to get this to work right, every time i try it it just takes pics at 1/125
uuuggghhhh >:(

// Custom bracket sequence

console_hide();set_aperture(22);
set_iso(100);
set_shutter(1./2000);
takepic();
set_shutter(1./1000);
takepic();
set_shutter(1./500);
takepic();
set_shutter(1./250);
takepic();
set_shutter(1./125);
takepic();
etc.....
Title: Re: PicoC scripting API
Post by: RenatoPhoto on May 25, 2013, 03:38:49 PM
Quote from: palark93 on May 25, 2013, 03:26:07 AM
cant seem to get this to work right, every time i try it it just takes pics at 1/125
uuuggghhhh >:(

// Custom bracket sequence

console_hide();set_aperture(22);
set_iso(100);
set_shutter(1./2000);
takepic();
set_shutter(1./1000);
takepic();
set_shutter(1./500);
takepic();
set_shutter(1./250);
takepic();
set_shutter(1./125);
takepic();
etc.....


Make sure you are not using a compile where the PicoC has been disabled.  All RAW video compiles do not have PicoC enable.  But.. I know nothing else about PicoC
Title: Re: PicoC scripting API
Post by: sfmurph on May 28, 2013, 06:06:42 AM
So I tried adding PicoC functions so I could "press" the DOF preview on my T3 (that doesn't have a hardware DOF preview). This compiled fine previously, but I've been having other issues with my compiled binaries. I'm on a Mac with the yagarto compiler. It doesn't seem like it's my problem, but it makes it hard to figure out what's going on.

Here's my diff to

$ hg diff library_ml.c
diff -r 7fdfc08ac9ad picoc/library_ml.c
--- a/picoc/library_ml.c   Mon May 27 23:01:29 2013 +0300
+++ b/picoc/library_ml.c   Mon May 27 20:59:19 2013 -0700
@@ -6,6 +6,7 @@
#define UNPRESS -10000 // generic unpress code
#define NO_KEY -1
#define AF_ON 12345
+#define DOF_PREVIEW 23456  // making this up

static char camera_model[32];
static char firmware_version[32];
@@ -139,6 +140,13 @@
         case BGMT_PRESS_FULLSHUTTER:
             SW2(1,50);
             return;
+        case DOF_PREVIEW:
+        {
+            int x = 1;
+            prop_request_change(PROP_DOF_PREVIEW_MAYBE, &x, 2);
+            msleep(50);
+            return;
+        }
         default:
             if (btn >= 0) fake_simple_button(btn);
     }
@@ -200,6 +208,13 @@
             unpress = BGMT_UNPRESS_DOWN;
             break;
         #endif
+        case DOF_PREVIEW:
+        {
+            int x = 0;
+            prop_request_change(PROP_DOF_PREVIEW_MAYBE, &x, 2);
+            msleep(50);
+            return;
+        }
         
         default:
             // this button does not have an unpress code
@@ -1347,6 +1362,7 @@
     CONST0(UNPRESS)
     CONST0(NO_KEY)
     CONST0(AF_ON)
+    CONST0(DOF_PREVIEW)
     CONST0(HALFSHUTTER_PRESSED)

     /** Color codes */
Title: Re: PicoC scripting API
Post by: trsaunders on June 06, 2013, 05:05:56 PM
Is there a way to set AF focus at infinity / nearest?

I'm trying to write a script that takes several photos at different AFMA values, to load into Reikan. Before it takes each photo I want to set focus to infinity.
Title: Re: PicoC scripting API
Post by: marci on June 09, 2013, 04:47:43 PM
Quote from: photo360 on February 26, 2013, 02:55:36 PM
set_flash(0)/set_flash(1)  does not work en 5DMKII with canon speedlite 580 EX. the flash fire each time.
It seems that this function works only for canon with build-in flash.

Is there another solution for 5DMKII ?

Rotate the mode dial to no flash mode! :)
Title: Re: PicoC scripting API
Post by: marci on June 09, 2013, 04:57:48 PM
Quote from: Alia5 on March 07, 2013, 02:39:55 PM
Would anyone mind extending beep() to beep(int msecs, int frequency)? ;)

I have added the beep_custom(int duration, int frequency, int wait) function! If wait is 1, it will wait until the beep has ended. If not, the script continues.
It will be in the jun10 nightly :)
Title: Re: PicoC scripting API
Post by: pravdomil on September 30, 2013, 12:18:04 AM
A1ex could you please send your script, that generates this API from comments? I would like to make ML modules API.
Title: Re: PicoC scripting API
Post by: nitrosito on December 15, 2014, 06:36:55 PM
I have a question... (Sorry for my english, i'm spanish)

¿Any guide or tutorial to write modules/script for ML?

¿How we test the script? Only in the camera?

Thanks and regards