// Lua: table_of_vals = getsamples( id, [count] ) static int adc_getsamples( lua_State* L ) { unsigned id, i; u16 bcnt, count = 0; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); if ( lua_isnumber(L, 2) == 1 ) count = ( u16 )lua_tointeger(L, 2); bcnt = adc_wait_samples( id, count ); // If count is zero, grab all samples if ( count == 0 ) count = bcnt; // Don't pull more samples than are available if ( count > bcnt ) count = bcnt; lua_createtable( L, count, 0 ); for( i = 1; i <= count; i ++ ) { lua_pushinteger( L, adc_get_processed_sample( id ) ); lua_rawseti( L, -2, i ); } return 1; }
// Lua: insertsamples(id, table, idx, count) static int adc_insertsamples( lua_State* L ) { unsigned id, i, startidx; u16 bcnt, count; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); luaL_checktype(L, 2, LUA_TTABLE); startidx = luaL_checkinteger( L, 3 ); if ( startidx <= 0 ) return luaL_error( L, "idx must be > 0" ); count = luaL_checkinteger(L, 4 ); if ( count == 0 ) return luaL_error( L, "count must be > 0" ); bcnt = adc_wait_samples( id, count ); for( i = startidx; i < ( count + startidx ); i ++ ) { if ( i < bcnt + startidx ) lua_pushinteger( L, adc_get_processed_sample( id ) ); else lua_pushnil( L ); // nil-out values where we don't have enough samples lua_rawseti( L, 2, i ); } return 0; }
// PicoC: adc_insertsamples(id, arr, idx, count); // Function parameters: // 1. id - ADC channel ID. // 2. arr - array to write samples to. Values at arr[idx] // to arr[idx + count -1] will be overwritten with samples // (or 0 if not enough samples are available). // 3. idx - first index to use in the array for writing // samples. // 4. count - number of samples to return. If not enough // samples are available (after blocking, if enabled) // remaining values will be 0; static void adc_insertsamples(pstate *p, val *r, val **param, int n) { unsigned id, i, startidx; int *arr; u16 bcnt, count; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); arr = param[1]->Val->Pointer; startidx = param[2]->Val->UnsignedInteger; if (startidx < 0) return pmod_error("idx must be >= 0"); count = param[3]->Val->UnsignedInteger; if (count == 0) return pmod_error("count must be > 0"); bcnt = adc_wait_samples(id, count); for (i = startidx; i < (count + startidx); i++) { if (i < bcnt + startidx) arr[i] = adc_get_processed_sample(id); else // zero-out values where we don't have enough samples arr[i] = 0; } }
// PicoC: val = adc_getsample(id); static void adc_getsample(pstate *p, val *r, val **param, int n) { unsigned id; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); // If we have at least one sample, return it if (adc_wait_samples(id, 1) >= 1) r->Val->Integer = adc_get_processed_sample(id); }
// Lua: val = getsample( id ) static int adc_getsample( lua_State *L ) { unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); // If we have at least one sample, return it if ( adc_wait_samples( id, 1 ) >= 1 ) { lua_pushinteger( L, adc_get_processed_sample( id ) ); return 1; } return 0; }
// (adc-getsample 'num) -> num any plisp_adc_getsample(any ex) { unsigned id; any x, y; x = cdr(ex); NeedNum(ex, y = EVAL(car(x))); id = unBox(y); // get id. MOD_CHECK_ID(ex, adc, id); // If we have at least one sample, return it. if (adc_wait_samples(id, 1) >= 1) return box(adc_get_processed_sample(id)); return Nil; }
// (adc-insertsamples 'num 'lst 'num 'num) -> lst any plisp_adc_insertsamples(any ex) { #ifdef BUF_ENABLE_ADC unsigned id, i, startidx; u16 bcnt, count; any x, y, tab; x = cdr(ex); NeedNum(ex, y = EVAL(car(x))); id = unBox(y); // get id MOD_CHECK_ID(ex, adc, id); // get the list of samples x = cdr(x); NeedLst(ex, y = EVAL(car(x))); tab = y; x = cdr(x); NeedNum(ex, y = EVAL(car(x))); startidx = unBox(y); // get startidx if (startidx <= 0) err(ex, y, "idx must be > 0"); x = cdr(x); NeedNum(ex, y = EVAL(car(x))); count = unBox(y); // get count if (count == 0) err(ex, y, "count must be > 0"); bcnt = adc_wait_samples(id, count); for (i = startidx; i < (count + startidx); i++) tab = ins_element(tab, i, adc_get_processed_sample(id)); return tab; #else err(NULL, NULL, "BUF_ENABLE_ADC not defined"); #endif }
// (adc-getsamples 'num ['num]) -> lst any plisp_adc_getsamples(any ex) { #ifdef BUF_ENABLE_ADC unsigned id, i; u16 bcnt, count = 0; any x, y; cell c1; x = cdr(ex); NeedNum(ex, y = EVAL(car(x))); id = unBox(y); // get id MOD_CHECK_ID(ex, adc, id); if (plen(ex) >= 2) { x = cdr(x); NeedNum(ex, y = EVAL(car(x))); count = (u16)unBox(y); // get count } bcnt = adc_wait_samples(id, count); // If count is zero, grab all samples if (count == 0) count = bcnt; // Don't pull more samples than are available if (count > bcnt) count = bcnt; // Make the list of adc samples Push(c1, y = cons(box(adc_get_processed_sample(id)), Nil)); for (i = 1; i < count - 1; i++) Push(c1, y = cons(box(adc_get_processed_sample(id)), y)); return Pop(c1); #else err(NULL, NULL, "BUF_ENABLE_ADC not defined"); #endif }
// PicoC: adc_getsamples(id, count, arr); // In PicoC, setup an array of integers of // size 'count'. static void adc_getsamples(pstate *p, val *r, val **param, int n) { int id, i, *arr; u16 bcnt, count = 0; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); count = (u16)param[1]->Val->UnsignedInteger; arr = (int *)param[2]->Val->Pointer; bcnt = adc_wait_samples(id, count); /* If count is zero, grab all samples */ if (count == 0) count = bcnt; /* Don't pull more samples than are available */ if (count > bcnt) count = bcnt; for (i = 0; i < count; i++) arr[i] = adc_get_processed_sample(id); }