// (adc-setclock 'num 'num 'num) -> num any plisp_adc_setclock(any ex) { s32 sfreq; // signed version for negative checking. u32 freq; unsigned id, timer_id = 0; any x, y; x = cdr(ex); NeedNum(ex, y = EVAL(car(x))); id = unBox(y); // get adc id. MOD_CHECK_ID(ex, adc, id); x = cdr(x); NeedNum(ex, y = EVAL(car(x))); sfreq = unBox(y); // get frequency. if (sfreq < 0) err(ex, y, "frequency must be 0 or positive"); freq = (u32) sfreq; if (freq > 0) { x = cdr(x); NeedNum(ex, y = EVAL(car(x))); timer_id = unBox(y); // get timer id. MOD_CHECK_ID(ex, timer, timer_id); MOD_CHECK_RES_ID(ex, adc, id, timer, timer_id); } platform_adc_set_timer(id, timer_id); freq = platform_adc_set_clock(id, freq); return box(freq); }
// Open Connection / Client int transport_open_connection(lua_State *L, Handle *handle) { // Get args & Set up connection unsigned uart_id, tmr_id; check_num_args( L,3 ); // 1st arg is uart num, 2nd arg is tmr_id, 3nd is handle if ( !lua_isnumber( L, 1 ) ) return luaL_error( L, "1st arg must be uart num" ); if ( !lua_isnumber( L, 2 ) ) return luaL_error( L, "2nd arg must be timer num" ); uart_id = lua_tonumber( L, 1 ); MOD_CHECK_ID( uart, uart_id ); tmr_id = lua_tonumber( L, 1 ); MOD_CHECK_ID( timer, tmr_id ); adispatch_buff = -1; handle->tpt.fd = ( int )uart_id; handle->tpt.tmr_id = tmr_id; // Setup uart platform_uart_setup( (unsigned int) uart_id, 115200, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 ); return 1; }
// Lua: apa102.write(data_pin, clock_pin, "string") // Byte quads in the string are interpreted as (brightness, B, G, R) values. // Only the first 5 bits of the brightness value is actually used (0-31). // This function does not corrupt your buffer. // // apa102.write(1, 3, string.char(31, 0, 255, 0)) uses GPIO5 for DATA and GPIO0 for CLOCK and sets the first LED green, with the brightness 31 (out of 0-32) // apa102.write(5, 6, string.char(255, 0, 0, 255):rep(10)) uses GPIO14 for DATA and GPIO12 for CLOCK and sets ten LED to red, with the brightness 31 (out of 0-32). // Brightness values are clamped to 0-31. static int apa102_write(lua_State* L) { uint8_t data_pin = luaL_checkinteger(L, 1); MOD_CHECK_ID(gpio, data_pin); uint32_t alt_data_pin = pin_num[data_pin]; uint8_t clock_pin = luaL_checkinteger(L, 2); MOD_CHECK_ID(gpio, clock_pin); uint32_t alt_clock_pin = pin_num[clock_pin]; size_t buf_len; const char *buf = luaL_checklstring(L, 3, &buf_len); uint32_t nbr_frames = buf_len / 4; if (nbr_frames > 100000) { return luaL_error(L, "The supplied buffer is too long, and might cause the callback watchdog to bark."); } // Initialize the output pins platform_gpio_mode(data_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); GPIO_OUTPUT_SET(alt_data_pin, PLATFORM_GPIO_HIGH); // Set pin high platform_gpio_mode(clock_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); GPIO_OUTPUT_SET(alt_clock_pin, PLATFORM_GPIO_LOW); // Set pin low // Send the buffers apa102_send_buffer(alt_data_pin, alt_clock_pin, (uint32_t *) buf, (uint32_t) nbr_frames); return 0; }
/*Lua: hx711.init(clk_pin,data_pin)*/ static int hx711_init(lua_State* L) { clk_pin = luaL_checkinteger(L,1); data_pin = luaL_checkinteger(L,2); MOD_CHECK_ID( gpio, clk_pin ); MOD_CHECK_ID( gpio, data_pin ); platform_gpio_mode(clk_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_mode(data_pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(clk_pin,1);//put chip to sleep. 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; } }
// Lua: write( id, string1, [string2], ..., [stringn] ) static int uart_write( lua_State* L ) { int id; const char* buf; size_t len, i; int total = lua_gettop( L ), s; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); for( s = 2; s <= total; s ++ ) { if( lua_type( L, s ) == LUA_TNUMBER ) { len = lua_tointeger( L, s ); if( ( len < 0 ) || ( len > 255 ) ) return luaL_error( L, "invalid number" ); platform_uart_send( id, ( u8 )len ); } else { luaL_checktype( L, s, LUA_TSTRING ); buf = lua_tolstring( L, s, &len ); for( i = 0; i < len; i ++ ) platform_uart_send( id, buf[ i ] ); } } return 0; }
static int checkid(lua_State* L,unsigned *ret,char var){ *ret = luaL_checkinteger( L, var ); if(*ret==0) return luaL_error( L, "no pwm for D0" ); MOD_CHECK_ID( pwm, *ret ); 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; }
// Lua: r = ow.reset( id ) static int ow_reset( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); lua_pushinteger( L, onewire_reset(id) ); return 1; }
//tmr.stop(id) //id:1~16 static int ltmr_stop( lua_State* L ) { unsigned id = luaL_checkinteger( L, 1 ) - 1; MOD_CHECK_ID( tmr, id+1 ); mico_stop_timer(&_timer[id]); return 0; }
// Lua: alarm( id, interval, repeat, function ) static int tmr_alarm( lua_State* L ) { s32 interval; unsigned repeat = 0; int stack = 1; unsigned id = luaL_checkinteger( L, stack ); stack++; MOD_CHECK_ID( tmr, id ); interval = luaL_checkinteger( L, stack ); stack++; if ( interval <= 0 ) return luaL_error( L, "wrong arg range" ); if ( lua_isnumber(L, stack) ){ repeat = lua_tointeger(L, stack); stack++; if ( repeat != 1 && repeat != 0 ) return luaL_error( L, "wrong arg type" ); } // luaL_checkanyfunction(L, stack); if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){ lua_pushvalue(L, stack); // copy argument (func) to the top of stack if(alarm_timer_cb_ref[id] != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, alarm_timer_cb_ref[id]); alarm_timer_cb_ref[id] = luaL_ref(L, LUA_REGISTRYINDEX); } os_timer_disarm(&(alarm_timer[id])); os_timer_setfn(&(alarm_timer[id]), (os_timer_func_t *)(alarm_timer_cb[id]), L); os_timer_arm(&(alarm_timer[id]), interval, repeat); return 0; }
//uart.on(id,function(t)) static int uart_on( lua_State* L ) { uint16_t id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); size_t sl; const char *method = luaL_checklstring( L, 2, &sl ); if (method == NULL) return luaL_error( L, "wrong arg type" ); if(sl == 4 && strcmp(method, "data") == 0) { if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 3); if(usr_uart_cb_ref != LUA_NOREF) { luaL_unref(L, LUA_REGISTRYINDEX, usr_uart_cb_ref); } usr_uart_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); } } else { return luaL_error( L, "wrong arg type" ); } return 0; }
// 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: ow.skip( id ) static int ow_skip( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); onewire_skip(id); return 0; }
//uart.setup(1,9600,'n','8','1') //baud:all supported //parity: // n,NO_PARITY; // o,ODD_PARITY; // e,EVEN_PARITY //databits: // 5:DATA_WIDTH_5BIT, // 6:DATA_WIDTH_6BIT, // 7:DATA_WIDTH_7BIT, // 8:DATA_WIDTH_8BIT, // 9:DATA_WIDTH_9BIT //stopbits: // 1,STOP_BITS_1 // 2,STOP_BITS_2 static int uart_setup( lua_State* L ) { uint16_t id, databits=DATA_WIDTH_8BIT, parity=NO_PARITY, stopbits=STOP_BITS_1; uint32_t baud=9600; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); baud = luaL_checkinteger( L, 2 ); size_t sl=0; char const *str = luaL_checklstring( L, 3, &sl ); if(sl == 1 && strcmp(str, "n") == 0) parity = NO_PARITY; else if(sl == 1 && strcmp(str, "o") == 0) parity = ODD_PARITY; else if(sl == 1 && strcmp(str, "e") == 0) parity = EVEN_PARITY; else return luaL_error( L, "arg parity should be 'n' or 'o' or 'e' " ); str = luaL_checklstring( L, 4, &sl ); if(sl == 1 && strcmp(str, "5") == 0) databits = DATA_WIDTH_5BIT; else if(sl == 1 && strcmp(str, "6") == 0) databits = DATA_WIDTH_6BIT; else if(sl == 1 && strcmp(str, "7") == 0) databits = DATA_WIDTH_7BIT; else if(sl == 1 && strcmp(str, "8") == 0) databits = DATA_WIDTH_8BIT; else if(sl == 1 && strcmp(str, "9") == 0) databits = DATA_WIDTH_9BIT; else return luaL_error( L, "arg databits should be '5'~'9' " ); str = luaL_checklstring( L, 5, &sl ); if(sl == 1 && strcmp(str, "1") == 0) stopbits = STOP_BITS_1; else if(sl == 1 && strcmp(str, "2") == 0) stopbits = STOP_BITS_2; else return luaL_error( L, "arg stopbits should be '1' or '2' " ); lua_usr_uart_config.baud_rate = baud; lua_usr_uart_config.parity=(platform_uart_parity_t)parity; lua_usr_uart_config.data_width =(platform_uart_data_width_t)databits; lua_usr_uart_config.stop_bits =(platform_uart_stop_bits_t)stopbits; if(lua_usr_rx_data !=NULL) free(lua_usr_rx_data); lua_usr_rx_data = (uint8_t*)malloc(USR_INBUF_SIZE); if(pinbuf !=NULL) free(pinbuf); pinbuf = (uint8_t*)malloc(USR_UART_LENGTH); ring_buffer_init( (ring_buffer_t*)&lua_usr_rx_buffer, (uint8_t*)lua_usr_rx_data, USR_INBUF_SIZE ); //MicoUartFinalize(LUA_USR_UART); MicoUartInitialize( LUA_USR_UART, &lua_usr_uart_config, (ring_buffer_t*)&lua_usr_rx_buffer ); gL = L; usr_uart_cb_ref = LUA_NOREF; if(plua_usr_usart_thread !=NULL) mico_rtos_delete_thread(plua_usr_usart_thread); mico_rtos_create_thread(plua_usr_usart_thread, MICO_DEFAULT_WORKER_PRIORITY, "lua_usr_usart_thread", lua_usr_usart_thread, 0x300, 0); return 0; }
//uart.send(1,string1,number,...[stringn]) static int uart_send( lua_State* L ) { uint16_t id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); const char* buf; size_t len; int total = lua_gettop( L ), s; for( s = 2; s <= total; s ++ ) { if( lua_type( L, s ) == LUA_TNUMBER ) { len = lua_tointeger( L, s ); if( len > 255 ) return luaL_error( L, "invalid number" ); MicoUartSend( LUA_USR_UART,(char*)len,1); } else { luaL_checktype( L, s, LUA_TSTRING ); buf = lua_tolstring( L, s, &len ); MicoUartSend( LUA_USR_UART, buf,len); } } return 0; }
// Clear the search state so that if will start from the beginning again. // Lua: ow.reset_search( id ) static int ow_reset_search( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); onewire_reset_search(id); return 0; }
//tmr.start(id,interval,function) //id:0~15 static int ltmr_start( lua_State* L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( tmr, id); unsigned interval = luaL_checkinteger( L, 2 ); if ( interval <= 0 ) return luaL_error( L, "wrong arg range" ); if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 3); // copy argument (func) to the top of stack if(tmr_cb_ref[id] != LUA_NOREF) { luaL_unref(L, LUA_REGISTRYINDEX, tmr_cb_ref[id]); } gL = L; tmr_cb_ref[id] = luaL_ref(L, LUA_REGISTRYINDEX); mico_stop_timer(&_timer[id]); mico_deinit_timer( &_timer[id] ); mico_init_timer(&_timer[id], interval, _tmr_handler, (void*)id); mico_start_timer(&_timer[id]); tmr_is_started[id] = true; } else return luaL_error( L, "callback function needed" ); return 0; }
// Lua: data = getchar( id, [ timeout ], [ timer_id ] ) static int uart_getchar( lua_State* L ) { int id, res; char cres; unsigned timer_id = 0; s32 timeout = PLATFORM_UART_INFINITE_TIMEOUT; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); // Check timeout and timer id if( lua_gettop( L ) >= 2 ) { timeout = luaL_checkinteger( L, 2 ); if( ( timeout < 0 ) && ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) ) return luaL_error( L, "invalid timeout value" ); if( ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) && ( timeout != 0 ) ) timer_id = luaL_checkinteger( L, 3 ); } res = platform_uart_recv( id, timer_id, timeout ); if( res == -1 ) lua_pushstring( L, "" ); else { cres = ( char )res; lua_pushlstring( L, &cres, 1 ); } return 1; }
// Lua: stop( id ) static int tmr_stop( lua_State* L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( tmr, id ); os_timer_disarm(&(alarm_timer[id])); return 0; }
// PicoC: data = adc_isdone(id); static void adc_isdone(pstate *p, val *r, val **param, int n) { unsigned id; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); r->Val->UnsignedInteger = platform_adc_is_done(id); }
// Lua: read(id) , return system adc static int adc_sample( lua_State* L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); unsigned val = 0xFFFF & system_adc_read(); lua_pushinteger( L, val ); return 1; }
// Lua: start( id ) static int ICACHE_FLASH_ATTR lpwm_start( lua_State* L ) { unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( pwm, id ); platform_pwm_start( id ); return 0; }
// Lua: i2c.stop( id ) static int i2c_stop( lua_State *L ) { unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( i2c, id ); platform_i2c_send_stop( id ); 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; }
// 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; }
// Lua: stop( id ) static int lpwm_stop( lua_State* L ) { unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( pwm, id ); platform_pwm_stop( id ); return 0; }
//Lua: init(id) static int enc_init( lua_State *L ) { unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( timer, id ); stm32_enc_init( id ); return 0; }
// Lua: data = isdone( id ) static int adc_isdone( lua_State* L ) { unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); lua_pushinteger( L, platform_adc_is_done( id ) ); return 1; }
// PicoC: adc_setblocking(id, mode); static void adc_setblocking(pstate *p, val *r, val **param, int n) { unsigned id, mode; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); mode = param[1]->Val->UnsignedInteger; platform_adc_set_blocking(id, mode); }