// Lua: sample( id, count ) static int adc_sample( lua_State* L ) { unsigned id, count, nchans = 1; int res, i; count = luaL_checkinteger( L, 2 ); if ( ( count == 0 ) || count & ( count - 1 ) ) return luaL_error( L, "count must be power of 2 and > 0" ); // If first parameter is a table, extract channel list if ( lua_istable( L, 1 ) == 1 ) { nchans = lua_objlen(L, 1); // Get/check list of channels and setup for( i = 0; i < nchans; i++ ) { lua_rawgeti( L, 1, i+1 ); id = luaL_checkinteger( L, -1 ); MOD_CHECK_ID( adc, id ); res = adc_setup_channel( id, intlog2( count ) ); if ( res != PLATFORM_OK ) return luaL_error( L, "sampling setup failed" ); } // Initiate sampling platform_adc_start_sequence(); } else if ( lua_isnumber( L, 1 ) == 1 ) { id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); res = adc_setup_channel( id, intlog2( count ) ); if ( res != PLATFORM_OK ) return luaL_error( L, "sampling setup failed" ); platform_adc_start_sequence(); } else { return luaL_error( L, "invalid channel selection" ); } return 0; }
// (adc-sample 'num 'num) -> Nil any plisp_adc_sample(any ex) { unsigned id, count = 0, nchans = 1; int res, i; any x, y, s; // get count value, the second parameter // in the picoLisp function call. s = cdr(ex), y = EVAL(car(s)); s = cdr(s); NeedNum(ex, y = EVAL(car(s))); count = unBox(y); // validate count. if ((count == 0) || count & (count - 1)) err(ex, y, "count must be power of 2 and > 0"); // get first parameter in the function // call. x = cdr(ex), y = EVAL(car(x)); // If first parameter is a table, // extract channel list. if (isCell(y)) { nchans = length(y); for (i = 0; i < nchans; i++) { NeedNum(y, car(y)); id = unBox(car(y)); MOD_CHECK_ID(ex, adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) err(ex, y, "sampling setup failed"); } // initiate sampling. platform_adc_start_sequence(); } else if (isNum(y)) { NeedNum(ex, y); id = unBox(y); MOD_CHECK_ID(ex, adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) err(ex, y, "sampling setup failed"); platform_adc_start_sequence(); } else { err(ex, y, "invalid channel selection"); } return Nil; }
// PicoC: adc_sample(id, count); // In PicoC, you could write an array and // use an iteration to init and setup a // list of channels. static void adc_sample(pstate *p, val *r, val **param, int n) { unsigned id, count; int res; count = param[1]->Val->UnsignedInteger; if ((count == 0) || count & (count - 1)) return pmod_error("count must be power of 2 and > 0"); id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) return pmod_error("sampling setup failed"); platform_adc_start_sequence(); r->Val->Integer = res; }