├── main-ui.png ├── bs-machines.jpg ├── current-drawing.png ├── machine-settings.png ├── sketchy_driver ├── bool.h ├── sketchy.h ├── lua-5.2.3 │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lfunc.h │ ├── ldebug.h │ ├── lualib.h │ ├── ltm.h │ ├── lstring.h │ ├── ltable.h │ ├── lvm.h │ ├── ldo.h │ ├── lzio.h │ ├── lzio.c │ ├── linit.c │ ├── lmem.h │ ├── ltm.c │ ├── lctype.h │ ├── llex.h │ ├── lctype.c │ ├── lmem.c │ ├── lcode.h │ ├── lopcodes.c │ ├── lparser.h │ ├── ldump.c │ ├── lcorolib.c │ ├── lfunc.c │ ├── lbitlib.c │ ├── lstring.c │ ├── lgc.h │ ├── lundump.c │ ├── Makefile │ ├── Makefile_cross │ ├── lmathlib.c │ ├── lauxlib.h │ ├── lstate.h │ └── ltablib.c ├── FSObject.h ├── FSObject.c ├── FSNumber.h ├── FSArray.h ├── Preview.h ├── Point.h ├── Step.h ├── tests │ ├── PreviewTest.h │ ├── PointTest.h │ └── SpeedManagerTest.h ├── FSNumber.c ├── Step.c ├── test.c ├── FSArray.c ├── Model.h ├── SpeedManager.h ├── Config.h ├── machine-settings.h ├── Preview.c ├── inih │ ├── ini.h │ └── ini.c ├── makefile ├── main.c ├── SpeedManager.c ├── sketchy.c └── Model.c ├── sketchy_server ├── manifest.ini ├── Makefile └── mongoose │ └── mongoose.h ├── .gitignore ├── sketchy_shared ├── test_driver.c ├── test_server.c ├── sketchy-ipc.h └── sketchy-ipc.c ├── LICENSE └── README.md /main-ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullscreennl/blackstripes-drawbot-driver/HEAD/main-ui.png -------------------------------------------------------------------------------- /bs-machines.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullscreennl/blackstripes-drawbot-driver/HEAD/bs-machines.jpg -------------------------------------------------------------------------------- /current-drawing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullscreennl/blackstripes-drawbot-driver/HEAD/current-drawing.png -------------------------------------------------------------------------------- /machine-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fullscreennl/blackstripes-drawbot-driver/HEAD/machine-settings.png -------------------------------------------------------------------------------- /sketchy_driver/bool.h: -------------------------------------------------------------------------------- 1 | #ifndef BOOL_H 2 | #define BOOL_H 3 | 4 | typedef enum { false, true } bool; 5 | 6 | #endif 7 | 8 | 9 | -------------------------------------------------------------------------------- /sketchy_driver/sketchy.h: -------------------------------------------------------------------------------- 1 | #ifndef SKETCHY_H 2 | #define SKETCHY_H 3 | 4 | #include "Point.h" 5 | 6 | int run(void (*executeMotion)()); 7 | void sketchy_suspend(); 8 | void sketchy_resume(); 9 | 10 | #endif -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /sketchy_driver/FSObject.h: -------------------------------------------------------------------------------- 1 | //class FSObject// 2 | 3 | #ifndef FSOBJECT_H 4 | #define FSOBJECT_H 5 | 6 | typedef struct FSObject{ 7 | int retainCount; 8 | char *type; 9 | }FSObject; 10 | 11 | void FSObject_release(void *o); 12 | void FSObject_retain(void *o); 13 | 14 | #endif 15 | 16 | 17 | -------------------------------------------------------------------------------- /sketchy_server/manifest.ini: -------------------------------------------------------------------------------- 1 | [machine_settings] 2 | version = 1.0.0 3 | canvas_width = 1000 4 | canvas_height = 1000 5 | marker_nib = 2.5 6 | max_delay = 900000 7 | min_delay = 500000 8 | min_move_delay = 50000 9 | pen_lookahead = 1 10 | lookahead_mm = 100 11 | 12 | [user] 13 | name = 14 | email = 15 | 16 | [jobticket] 17 | motion_svg = job.svg 18 | -------------------------------------------------------------------------------- /sketchy_driver/FSObject.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "FSObject.h" 4 | 5 | void FSObject_release(void *o){ 6 | FSObject *obj = (FSObject*)o; 7 | obj->retainCount --; 8 | if(obj->retainCount == 0){ 9 | free(obj); 10 | } 11 | } 12 | 13 | void FSObject_retain(void *o){ 14 | FSObject *obj = (FSObject*)o; 15 | obj->retainCount ++; 16 | } 17 | -------------------------------------------------------------------------------- /sketchy_driver/FSNumber.h: -------------------------------------------------------------------------------- 1 | //class FSNumber// 2 | 3 | #ifndef FSNUMBER_H 4 | #define FSNUMBER_H 5 | 6 | typedef struct FSNumber{ 7 | int retainCount; 8 | char *type; 9 | float floatValue; 10 | float intValue; 11 | }FSNumber; 12 | 13 | FSNumber *FSNumber_allocWithInt(int intval); 14 | FSNumber *FSNumber_allocWithFloat(float floatval); 15 | void FSNumber_release(FSNumber *p); 16 | void FSNumber_retain(FSNumber *p); 17 | 18 | #endif 19 | 20 | 21 | -------------------------------------------------------------------------------- /sketchy_driver/FSArray.h: -------------------------------------------------------------------------------- 1 | //class FSArray// 2 | 3 | #ifndef FSARRAY_H 4 | #define FSARRAY_H 5 | 6 | typedef struct FSArray{ 7 | int retainCount; 8 | char *type; 9 | int cursor; 10 | int length; 11 | void** array; 12 | }FSArray; 13 | 14 | FSArray *FSArray_alloc(int length); 15 | void FSArray_append(FSArray *p, void *elem); 16 | void FSArray_release(FSArray *p); 17 | void FSArray_retain(FSArray *p); 18 | int FSArray_count(FSArray *p); 19 | void *FSArray_objectAtIndex(FSArray *p,int index); 20 | 21 | #endif 22 | 23 | 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # mac stuff 2 | *DS_Store* 3 | 4 | sketchy_driver/sketchy-driver 5 | sketchy_driver/sketchy-preview 6 | sketchy_server/sketchy-server 7 | 8 | # Object files 9 | *.o 10 | *.ko 11 | *.obj 12 | *.elf 13 | 14 | # Precompiled Headers 15 | *.gch 16 | *.pch 17 | 18 | # Libraries 19 | *.lib 20 | *.a 21 | *.la 22 | *.lo 23 | 24 | # Shared objects (inc. Windows DLLs) 25 | *.dll 26 | *.so 27 | *.so.* 28 | *.dylib 29 | 30 | # Executables 31 | *.exe 32 | *.out 33 | *.app 34 | *.i*86 35 | *.x86_64 36 | *.hex 37 | 38 | # Debug files 39 | *.dSYM/ 40 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /sketchy_driver/Preview.h: -------------------------------------------------------------------------------- 1 | //class Preview// 2 | #include "bool.h" 3 | 4 | #ifndef PREVIEW_H 5 | #define PREVIEW_H 6 | 7 | typedef struct Preview{ 8 | int retainCount; 9 | char *type; 10 | char *imageName; 11 | unsigned char* imageData; 12 | int width; 13 | int height; 14 | int maxDelay; 15 | int minDelay; 16 | }Preview; 17 | 18 | Preview *Preview_alloc(int width, int height, char *imagename,int maxDelay, int minDelay); 19 | void Preview_setPixel(Preview *self,int x, int y,int delay, bool shouldDraw); 20 | void Preview_save(Preview *self); 21 | void Preview_release(Preview *p); 22 | void Preview_retain(Preview *p); 23 | void Preview_updateSpeed(Preview *self, int maxDelay, int minDelay); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /sketchy_shared/test_driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sketchy-ipc.h" 8 | 9 | 10 | static int lastReadMessageID; 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | 15 | create(); 16 | while(1){ 17 | DriverCommand *cmd = getCommand(); 18 | if(cmd->messageID != lastReadMessageID){ 19 | printf("Readed from SHM cmd ID: %i\n", cmd->messageID); 20 | printf("Readed from SHM cmd code: %i\n", cmd->commandCode); 21 | printf("Readed from SHM cmd msg: %s\n", cmd->msg); 22 | lastReadMessageID = cmd->messageID; 23 | } 24 | updateDriverState(argv[1]); 25 | sleep(2); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sketchy_driver/Point.h: -------------------------------------------------------------------------------- 1 | //class Point// 2 | 3 | #ifndef POINT_H 4 | #define POINT_H 5 | 6 | typedef struct FSPoint{ 7 | int retainCount; 8 | char *type; 9 | float x; 10 | float y; 11 | float left_angle; 12 | float right_angle; 13 | int left_steps; 14 | int right_steps; 15 | }Point; 16 | 17 | Point *Point_alloc(float x, float y); 18 | 19 | Point *Point_allocWithXY(float x, float y); 20 | void Point_updateWithXY(Point *p,float x, float y); 21 | 22 | Point *Point_allocWithSteps(int left_steps, int right_steps); 23 | void Point_updateWithSteps(Point *p,int left_steps, int right_steps); 24 | 25 | void Point_copy(Point *src,Point *dest); 26 | void Point_release(Point *p); 27 | void Point_retain(Point *p); 28 | void Point_setNull(Point *p); 29 | void Point_log(Point *p); 30 | 31 | #endif 32 | 33 | 34 | -------------------------------------------------------------------------------- /sketchy_driver/Step.h: -------------------------------------------------------------------------------- 1 | // 2 | // Step.h 3 | // sketchy 4 | // 5 | // Created by Johan Ten Broeke on 16/11/14. 6 | // Copyright (c) 2014 Johan Ten Broeke. All rights reserved. 7 | // 8 | 9 | #ifndef STEP_H 10 | #define STEP_H 11 | 12 | typedef enum stepperMotorDir{ 13 | 14 | stepperMotorDirUp = 0, 15 | stepperMotorDirNone = 1, 16 | stepperMotorDirDown = 2 17 | 18 | }StepperMotorDir; 19 | 20 | typedef struct Step{ 21 | int retainCount; 22 | char *type; 23 | StepperMotorDir leftengine; 24 | StepperMotorDir rightengine; 25 | }Step; 26 | 27 | Step *Step_alloc(StepperMotorDir leftengine, StepperMotorDir rightengine); 28 | Step *Step_update(Step *obj,StepperMotorDir leftengine, StepperMotorDir rightengine); 29 | void Step_release(Step *obj); 30 | void Step_retain(Step *obj); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /sketchy_driver/tests/PreviewTest.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../Preview.h" 3 | 4 | #ifndef PREVIEW_TEST_H 5 | #define PREVIEW_TEST_H 6 | 7 | void Preview_test(){ 8 | 9 | Preview *pr = Preview_alloc(1000,1000,"tests/test_image.png",Config_maxDelay(),Config_minDelay()); 10 | Preview_setPixel(pr,100,100,Config_maxDelay()); 11 | Preview_setPixel(pr,110,110,Config_minDelay()); 12 | int bandWidth = Config_maxDelay() - Config_minDelay(); 13 | Preview_setPixel(pr,120,120,Config_minDelay()+bandWidth/2.0); 14 | Preview_save(pr); 15 | Preview_release(pr); 16 | 17 | //Maybe compare the generated file with a fixed test image and assert that. 18 | //For now inspect manually, we just need to assure that fast pixels 19 | //are red and slow pixles are green 20 | assert(1); 21 | 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /sketchy_shared/test_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sketchy-ipc.h" 8 | 9 | static int lastReadMessageID; 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | create(); 14 | while(1){ 15 | DriverState *state = driverState(); 16 | if(state->messageID != lastReadMessageID){ 17 | printf("Readed from SHM message ID: %i\n", state->messageID); 18 | printf("Readed from SHM status code: %i\n", state->statusCode); 19 | printf("Readed from SHM name: %s\n", state->name); 20 | printf("Readed from SHM joburl: %s\n", state->joburl); 21 | lastReadMessageID = state->messageID; 22 | } 23 | setCommand(argv[1]); 24 | sleep(2); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /sketchy_driver/FSNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "FSObject.h" 5 | #include "FSNumber.h" 6 | 7 | FSNumber *FSNumber_allocWithInt(int intval){ 8 | FSNumber *n = (FSNumber *) malloc(sizeof(FSNumber)); 9 | n->intValue = intval; 10 | n->floatValue = (float)intval; 11 | n->retainCount = 1; 12 | n->type = "Number"; 13 | return n; 14 | } 15 | 16 | FSNumber *FSNumber_allocWithFloat(float floatval){ 17 | FSNumber *n = (FSNumber *) malloc(sizeof(FSNumber)); 18 | n->intValue = (int)round(floatval); 19 | n->floatValue = floatval; 20 | n->retainCount = 1; 21 | n->type = "Number"; 22 | return n; 23 | } 24 | 25 | void FSNumber_release(FSNumber *n){ 26 | FSObject_release(n); 27 | } 28 | 29 | void FSNumber_retain(FSNumber *n){ 30 | FSObject_retain(n); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /sketchy_driver/Step.c: -------------------------------------------------------------------------------- 1 | // 2 | // Step.c 3 | // sketchy 4 | // 5 | // Created by Johan Ten Broeke on 16/11/14. 6 | // Copyright (c) 2014 Johan Ten Broeke. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "Step.h" 12 | #include "FSObject.h" 13 | 14 | Step *Step_alloc(StepperMotorDir leftengine, StepperMotorDir rightengine){ 15 | Step *obj = (Step *) malloc(sizeof(Step)); 16 | obj->leftengine = leftengine; 17 | obj->rightengine = rightengine; 18 | obj->retainCount = 1; 19 | obj->type = "Step"; 20 | return obj; 21 | } 22 | 23 | Step *Step_update(Step *obj,StepperMotorDir leftengine, StepperMotorDir rightengine){ 24 | obj->leftengine = leftengine; 25 | obj->rightengine = rightengine; 26 | return obj; 27 | } 28 | 29 | 30 | void Step_release(Step *obj){ 31 | FSObject_release(obj); 32 | } 33 | 34 | void Step_retain(Step *obj){ 35 | FSObject_retain(obj); 36 | } 37 | -------------------------------------------------------------------------------- /sketchy_driver/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Config.h" 6 | 7 | #include "tests/InputImageTest.h" 8 | #include "tests/SpeedManagerTest.h" 9 | #include "tests/PreviewTest.h" 10 | #include "tests/PointTest.h" 11 | 12 | #include "sketchy-ipc.h" 13 | 14 | int main(int argc, char *argv[]){ 15 | 16 | Config_setBasePath("../test_assets/"); 17 | Config_load("../test_assets/test.ini"); //test code assumes 1000px x 1000px 18 | 19 | shmCreate(); 20 | 21 | InputImage_testSaveAsPNG(); 22 | InputImage_testBestPointOfNDestinationsFromXY(); 23 | InputImage_testClearDarkness(); 24 | InputImage_testLineDarkness(); 25 | InputImage_test(); 26 | InputImage_testGetBrightness(); 27 | printf("\n\nInputImage test PASSED\n"); 28 | 29 | SpeedManager_test(); 30 | printf("SpeedManager test PASSED\n"); 31 | 32 | Preview_test(); 33 | printf("Preview test PASSED\n"); 34 | 35 | Point_test(); 36 | printf("Point test PASSED\n"); 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Fullscreen.nl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /sketchy_driver/FSArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "FSObject.h" 4 | #include "FSArray.h" 5 | 6 | FSArray *FSArray_alloc(int length) 7 | { 8 | FSArray *p = (FSArray *) malloc(sizeof(FSArray)); 9 | p->array = (void**) malloc(length*sizeof(void*)); 10 | p->cursor = 0; 11 | p->length = length; 12 | p->retainCount = 1; 13 | p->type = "Array"; 14 | return p; 15 | } 16 | 17 | int FSArray_count(FSArray *p){ 18 | return p->length; 19 | } 20 | 21 | void *FSArray_objectAtIndex(FSArray *p,int index){ 22 | return p->array[index]; 23 | } 24 | 25 | void FSArray_append(FSArray *p,void *elem){ 26 | FSObject_retain(elem); 27 | p->array[p->cursor] = elem; 28 | p->cursor ++; 29 | } 30 | 31 | void FSArray_release(FSArray *p){ 32 | int i = 0; 33 | for(i=0; i < p->length; i++){ 34 | void *elem = (void*)p->array[i]; 35 | FSObject_release(elem); 36 | } 37 | p->retainCount --; 38 | if(p->retainCount == 0){ 39 | free(p->array); 40 | free(p); 41 | } 42 | } 43 | 44 | void FSArray_retain(FSArray *p){ 45 | FSObject_retain(p); 46 | } 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /sketchy_shared/sketchy-ipc.h: -------------------------------------------------------------------------------- 1 | #ifndef SKETCHY_IPC_H 2 | #define SKETCHY_IPC_H 3 | 4 | typedef enum { 5 | driverSatusCodeBusy = 1, 6 | driverSatusCodeIdle = 2, 7 | driverStatusCodePaused = 3, 8 | driverStateOutOfBoundsError = 4, 9 | driverStateNoDataFoundInSVGError = 5, 10 | }DriverSatusCode; 11 | 12 | typedef struct DriverState{ 13 | int messageID; 14 | DriverSatusCode statusCode; 15 | char name[100]; 16 | char joburl[100]; 17 | }DriverState; 18 | 19 | typedef enum { 20 | commandCodeNone = 0, 21 | commandCodeStop = 1, 22 | commandCodeSpeed = 2, 23 | commandCodePenMode = 3, 24 | commandCodePause = 4, 25 | commandCodeResume = 5, 26 | commandCodePreviewAbort = 6, 27 | }CommandCode; 28 | 29 | typedef struct DriverCommand{ 30 | int messageID; 31 | CommandCode commandCode; 32 | char msg[100]; 33 | float fvalue; 34 | int ivalue; 35 | }DriverCommand; 36 | 37 | void shmCreate(); 38 | void shmDestroy(); 39 | 40 | void updateDriverState(DriverSatusCode statusCode,const char *joburl,const char *name); 41 | DriverState *driverState(); 42 | 43 | void setCommand(char *msg, CommandCode command, float floatValue, int intValue); 44 | DriverCommand *getCommand(); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /sketchy_driver/Model.h: -------------------------------------------------------------------------------- 1 | //class Model// 2 | #include "Point.h" 3 | #include "Step.h" 4 | #include "SpeedManager.h" 5 | 6 | #ifndef MODEL_H 7 | #define MODEL_H 8 | 9 | SpeedManager *sm; 10 | Point *Model_toPoint; 11 | 12 | typedef enum solenoidState{ 13 | solenoidStateUp = 0, 14 | solenoidStateDown = 1 15 | }SolenoidState; 16 | 17 | typedef enum penMode{ 18 | penModeImage = 1, 19 | penModeManualUp = 2, 20 | penModeManualDown = 3 21 | }PenMode; 22 | 23 | typedef struct FSBotState{ 24 | int retainCount; 25 | char *type; 26 | Point *home; 27 | Point *currentLocation; 28 | SpeedManager *speedManager; 29 | int leftsteps; 30 | int rightsteps; 31 | int delay; 32 | void (*executeStepCallback)(Step *step); 33 | PenMode penMode; 34 | PenMode scheduledPenMode; 35 | }BotState; 36 | 37 | BotState *BOT; 38 | 39 | void SpeedManager_callback(float x, float y, int delay, int cursor,int penMode); 40 | void Model_createInstance(); 41 | void Model_addStep(int left, int right); 42 | void Model_logState(); 43 | void Model_release(); 44 | void Model_retain(); 45 | void Model_moveTo(Point *dest); 46 | void Model_moveHome(); 47 | void Model_setExecuteStepCallback(void (*executeStepCallback)(Step *step)); 48 | void Model_setPenMode(PenMode mode); 49 | void Model_finish(); 50 | void Model_resume(); 51 | 52 | void report_memory(int id); 53 | 54 | #endif 55 | 56 | 57 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /sketchy_driver/SpeedManager.h: -------------------------------------------------------------------------------- 1 | //class SpeedManager// 2 | #ifndef SPEEDMANAGER_H 3 | #define SPEEDMANAGER_H 4 | 5 | #define LOOKAHEAD_IN_MM 100 6 | 7 | typedef struct PathSegment 8 | { 9 | float direction; 10 | struct PathSegment *next; 11 | float x; 12 | float y; 13 | int penMode; 14 | int solenoidState; 15 | }PathSegment; 16 | 17 | 18 | typedef struct SpeedManager{ 19 | int retainCount; 20 | char *type; 21 | int queueLength; 22 | int length; 23 | PathSegment* bottom; 24 | PathSegment* top; 25 | float max; 26 | float currentDirection; 27 | float currentX; 28 | float currentY; 29 | void (*executeCallback)(float x, float y, int delay,int cursor,int penMode); 30 | int delay; 31 | int targetDelay; 32 | int delayStep; 33 | int delayStepDraw; 34 | int delayStepMove; 35 | float delayPerDegree; 36 | float delayPerDegreeMove; 37 | int usePenChangeInLookAhead; 38 | }SpeedManager; 39 | 40 | SpeedManager *SpeedManager_alloc(); 41 | void SpeedManager_append(SpeedManager *sm,float x,float y,int penMode,int solenoidState); 42 | void SpeedManager_setCallback(SpeedManager *sm,void (*executeCallback)(float x,float y, int delay,int cursor,int penMode)); 43 | void SpeedManager_resume(SpeedManager *sm); 44 | void SpeedManager_finish(SpeedManager *sm); 45 | void SpeedManager_release(SpeedManager *sm); 46 | void SpeedManager_retain(SpeedManager *sm); 47 | void SpeedManager_reduceQueue(SpeedManager *sm); 48 | 49 | #endif 50 | 51 | 52 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | /* returns the key, given the value of a table entry */ 21 | #define keyfromval(v) \ 22 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 23 | 24 | 25 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 26 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 27 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 28 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 29 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 30 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 31 | LUAI_FUNC Table *luaH_new (lua_State *L); 32 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 33 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 34 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 35 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 36 | LUAI_FUNC int luaH_getn (Table *t); 37 | 38 | 39 | #if defined(LUA_DEBUG) 40 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 41 | LUAI_FUNC int luaH_isdummy (Node *n); 42 | #endif 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) 23 | 24 | 25 | /* not to called directly */ 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 27 | 28 | 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_finishOp (lua_State *L); 38 | LUAI_FUNC void luaV_execute (lua_State *L); 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 40 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 41 | const TValue *rc, TMS op); 42 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /sketchy_driver/tests/PointTest.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../Point.h" 4 | 5 | #ifndef POINT_TEST_H 6 | #define POINT_TEST_H 7 | 8 | static void Point_testForXY(float x, float y){ 9 | 10 | Point *p1 = Point_allocWithXY(x,y); 11 | Point_log(p1); 12 | 13 | Point *p2 = Point_allocWithSteps(p1->left_steps,p1->right_steps); 14 | Point_log(p2); 15 | 16 | //printf("- - - - - - - - - - - - - - - - - - - - - - -\n\n"); 17 | // assert(p1->left_steps == p2->left_steps); 18 | // assert(p1->right_steps == p2->right_steps); 19 | 20 | // //ignore small rounding errors 21 | // assert(fabs(p1->x - p2->x) < 1.0); 22 | // assert(fabs(p1->y - p2->y) < 1.0); 23 | 24 | #ifndef __VPLOTTER__ 25 | assert(fabs(p1->left_angle - p2->left_angle) < 1.0); 26 | assert(fabs(p1->right_angle - p2->right_angle) < 1.0); 27 | #endif 28 | 29 | Point_release(p1); 30 | Point_release(p2); 31 | 32 | } 33 | 34 | void Point_test(){ 35 | 36 | #ifdef __VPLOTTER__ 37 | 38 | Point *home = Point_allocWithSteps(0 ,0); 39 | Point_log(home); 40 | printf("-----\n"); 41 | 42 | Point_testForXY(home->x,home->y); 43 | Point_testForXY(1000,1000); 44 | Point_testForXY(1500,1500); 45 | Point_testForXY(1457.500000,609.994873); 46 | 47 | Point_release(home); 48 | 49 | #else 50 | 51 | Point *home = Point_allocWithSteps(0 ,0); 52 | 53 | Point_testForXY(home->x,home->y); 54 | Point_testForXY(500,500); 55 | Point_testForXY(30,600); 56 | Point_testForXY(30.5,20.556); 57 | 58 | Point_release(home); 59 | 60 | #endif 61 | 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /sketchy_driver/Config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | typedef struct 5 | { 6 | const char* versionString; 7 | const char* name; 8 | const char* email; 9 | int canvasWidth; 10 | int canvasHeight; 11 | float nibSize; 12 | const char *imagename; 13 | const char *motionScript; 14 | const char *motionSVG; 15 | const char *_svg; 16 | const char *_lua; 17 | int maxDelay; 18 | int minDelay; 19 | int minMoveDelay; 20 | int initialThreshold; 21 | int usePenChangeInLookAhead; 22 | int lookaheadMM; 23 | } Config; 24 | 25 | Config config; 26 | 27 | void Config_reload(); 28 | void Config_load(char *inifilename); 29 | void Config_write(char *inifilename); 30 | 31 | int Config_getCanvasWidth(); 32 | int Config_getCanvasHeight(); 33 | float Config_getNibSize(); 34 | 35 | const char* Config_getScriptName(); 36 | const char* Config_getSVGName(); 37 | const char* Config_getEmail(); 38 | 39 | 40 | int Config_maxDelay(); 41 | void Config_setMaxDelay(int value); 42 | 43 | int Config_getLookaheadMM(); 44 | void Config_setLookaheadMM(int value); 45 | 46 | int Config_minDelay(); 47 | void Config_setMinDelay(int value); 48 | 49 | int Config_minMoveDelay(); 50 | void Config_setMinMoveDelay(int value); 51 | 52 | int Config_canvasWidth(); 53 | void Config_setCanvasWidth(int value); 54 | 55 | int Config_canvasHeight(); 56 | void Config_setCanvasHeight(int value); 57 | 58 | void Config_setBasePath(char *bp); 59 | int Config_setIniBasePath(char *inifilePath); 60 | 61 | int Config_usePenChangeInLookAhead(); 62 | void Config_setUsePenChangeInLookAhead(int value); 63 | 64 | void Config_setSVGJob(const char * value); 65 | void Config_setLuaJob(const char * value); 66 | 67 | const char * Config_getJob(); 68 | 69 | const char* Config_getJSON(); 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /sketchy_server/Makefile: -------------------------------------------------------------------------------- 1 | LIBS = -lm -ldl 2 | 3 | ifeq ($(type),vplotter) 4 | MACHINE_TYPE = -D__VPLOTTER__ 5 | else 6 | MACHINE_TYPE = 7 | endif 8 | 9 | # define the C compiler to use 10 | ifeq ($(MAKECMDGOALS),pi_cc) 11 | CC = arm-linux-gnueabihf-gcc 12 | MAIN = sketchy-server 13 | PROG = main 14 | CFLAGS = -W -Wall -Wno-missing-field-initializers -pthread -O0 $(CFLAGS_EXTRA) $(MACHINE_TYPE) 15 | SOURCES = $(PROG).c mongoose/mongoose.c ../sketchy_shared/sketchy-ipc.c ../sketchy_driver/Config.c ../sketchy_driver/inih/ini.c 16 | INCLUDES = -I../sketchy_shared -I../sketchy_driver -I../sketchy_driver/inih -I../sketchy_driver/nanosvg -I/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include 17 | else 18 | MAKECMDGOALS = default 19 | CC = gcc 20 | MAIN = sketchy-server 21 | PROG = main 22 | CFLAGS = -W -Wall -Wno-missing-field-initializers -pthread -O0 $(CFLAGS_EXTRA) $(MACHINE_TYPE) 23 | SOURCES = $(PROG).c mongoose/mongoose.c ../sketchy_shared/sketchy-ipc.c ../sketchy_driver/Config.c ../sketchy_driver/inih/ini.c 24 | INCLUDES = -I../sketchy_shared -I../sketchy_driver -I../sketchy_driver/inih -I../sketchy_driver/nanosvg 25 | endif 26 | OBJS = $(SOURCES:.c=.o) 27 | .PHONY: depend clean test pi_cc 28 | 29 | $(MAKECMDGOALS):$(MAIN) 30 | @echo sketchy server has been compiled 31 | 32 | $(MAIN): $(OBJS) 33 | $(CC) -o $(MAIN) $(OBJS) $(CFLAGS) $(INCLUDES) $(LIBS) 34 | 35 | .c.o: 36 | $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ 37 | 38 | clean: 39 | rm -rf $(MAIN) *.exe *.dSYM *.obj *.exp *.o *.lib mongoose/*.o ../sketchy_shared/*.o ../sketchy_driver/inih/*.o ../sketchy_driver/Config.o 40 | 41 | install: 42 | install sketchy-server ../build 43 | install index.html ../build 44 | install manifest.ini ../build/job 45 | 46 | depend: $(SRCS) 47 | makedepend $(INCLUDES) $^ 48 | -------------------------------------------------------------------------------- /sketchy_driver/tests/SpeedManagerTest.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../SpeedManager.h" 3 | #include "../Config.h" 4 | #include "../machine-settings.h" 5 | 6 | 7 | #ifndef SPEED_MANAGER_TEST_H 8 | #define SPEED_MANAGER_TEST_H 9 | 10 | static int maxDelay; 11 | static int minDelay; 12 | 13 | static int testLineLength = 400; 14 | 15 | void SpeedManager_log(SpeedManager *sm); 16 | 17 | void SpeedManager_testCallback(float x, float y, int delay,int cursor,int penMode){ 18 | int translatedCursor = cursor-LOOKAHEAD_IN_MM+1; 19 | if(translatedCursor == testLineLength){ 20 | assert(delay == maxDelay); 21 | } 22 | if(translatedCursor-1 == LOOKAHEAD_IN_MM){ 23 | assert(delay == minDelay); 24 | } 25 | //printf("callback x %f y %f delay %i - cursor %i\n",x,y,delay,cursor-LOOKAHEAD_IN_MM+1); 26 | } 27 | 28 | 29 | //test 180 degree turn 30 | //should slowdown to speed with maxdelay at the turning point 31 | //should be up to speed with minDelay after LOOKAHEAD_IN_MM 32 | void SpeedManager_test(){ 33 | 34 | maxDelay = Config_maxDelay(); 35 | minDelay = Config_minDelay(); 36 | 37 | int penMode = 1; 38 | int solenoidState = 1; 39 | SpeedManager *sm = SpeedManager_alloc(); 40 | SpeedManager_setCallback(sm,SpeedManager_testCallback); 41 | 42 | //SpeedManager_log(sm); 43 | Point *home = Point_allocWithSteps(0 ,0); 44 | float x = home->x; 45 | float y = home->y; 46 | Point_release(home); 47 | 48 | int i; 49 | for(i=0;i 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /sketchy_driver/machine-settings.h: -------------------------------------------------------------------------------- 1 | #ifndef MACHINE_SETTINGS_H 2 | #define MACHINE_SETTINGS_H 3 | 4 | #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 5 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 6 | 7 | #define LINE_SEGMENT_SIZE_MM 1.0 8 | #define DEG 57.2957795 9 | 10 | #define MAXDELAY 900000 11 | #define MINDELAY 50000 12 | 13 | #define MARKER_NIB_SIZE_MM 3.2 //4.0 14 | #define MARKER_NIB_SIZE MARKER_NIB_SIZE_MM 15 | 16 | //#define machine_size_medium 1 //small/medium/large 17 | 18 | #ifdef __VPLOTTER__ 19 | 20 | #define MAX_CANVAS_SIZE_X 1500.0 21 | #define MAX_CANVAS_SIZE_Y 1550.0 22 | #define CANVAS_Y 875.0 23 | #define LEFT_STEPPER_X 0.0 24 | #define LEFT_STEPPER_Y 0.0 25 | #define RIGHT_STEPPER_X 2915.0 26 | #define RIGHT_STEPPER_Y 0.0 27 | #define STEPS_PER_MM 42.5532 28 | #define HOME_LEFT_MM 1580.0 29 | #define HOME_RIGHT_MM 1580.0 30 | 31 | #else 32 | 33 | #ifdef machine_size_medium 34 | 35 | #define CENTER 180.0 36 | #define UPPER_ARM_LENGTH 252.0 37 | #define LOWER_ARM_LENGTH 324.0 38 | #define SHOULDER_DIST 108.0 39 | #define SHOULDER_HEIGHT 36.0 40 | #define LEFT_SHOULDER_POS_X (CENTER - 50.0) 41 | #define RIGHT_SHOULDER_POS_X (CENTER + 50.0) 42 | #define LEFT_SHOULDER_POS_Y 36.0 43 | #define RIGHT_SHOULDER_POS_Y 36.0 44 | #define EXENSION1 30.0 45 | #define CANVAS_Y 142 46 | #define ANGLE_PER_STEP (360.0/(25600.0)) 47 | #define MAX_CANVAS_SIZE_X 360.0 48 | #define MAX_CANVAS_SIZE_Y 360.0 49 | 50 | #else 51 | 52 | #define CENTER 550.0 53 | #define UPPER_ARM_LENGTH 700.0 54 | #define LOWER_ARM_LENGTH 900.0 55 | #define SHOULDER_DIST 300.0 56 | #define SHOULDER_HEIGHT 100.0 57 | #define LEFT_SHOULDER_POS_X (CENTER - 150.0) 58 | #define RIGHT_SHOULDER_POS_X (CENTER + 150.0) 59 | #define LEFT_SHOULDER_POS_Y 100.0 60 | #define RIGHT_SHOULDER_POS_Y 100.0 61 | #define EXENSION1 72.0 62 | #define CANVAS_Y 330 63 | #define ANGLE_PER_STEP (360.0/(3200.0*50.0)) 64 | #define MAX_CANVAS_SIZE_X 1100.0 65 | #define MAX_CANVAS_SIZE_Y 1200.0 66 | 67 | #endif 68 | 69 | #endif 70 | 71 | #endif 72 | 73 | 74 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is 19 | ** always constant. 20 | ** The macro is somewhat complex to avoid warnings: 21 | ** +1 avoids warnings of "comparison has constant result"; 22 | ** cast to 'void' avoids warnings of "value unused". 23 | */ 24 | #define luaM_reallocv(L,b,on,n,e) \ 25 | (cast(void, \ 26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ 27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 28 | 29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 32 | 33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 35 | #define luaM_newvector(L,n,t) \ 36 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 37 | 38 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 39 | 40 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41 | if ((nelems)+1 > (size)) \ 42 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 43 | 44 | #define luaM_reallocvector(L, v,oldn,n,t) \ 45 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 46 | 47 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 48 | 49 | /* not to be called directly */ 50 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 51 | size_t size); 52 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 53 | size_t size_elem, int limit, 54 | const char *what); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /sketchy_driver/Preview.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "machine-settings.h" 4 | #include "Preview.h" 5 | #include "FSObject.h" 6 | #include "Config.h" 7 | #include "bool.h" 8 | 9 | #include "lodepng/lodepng.h" 10 | 11 | Preview *Preview_alloc(int width, int height, char *imagename,int maxDelay, int minDelay){ 12 | Preview *p = (Preview *) malloc(sizeof(Preview)); 13 | p->width = width; 14 | p->height = height; 15 | p->maxDelay = maxDelay; 16 | p->minDelay = minDelay; 17 | p->imageName = imagename; 18 | p->imageData = malloc(width * height * 4); 19 | p->retainCount = 1; 20 | p->type = "Preview"; 21 | return p; 22 | } 23 | 24 | void Preview_updateSpeed(Preview *self, int maxDelay, int minDelay){ 25 | self->maxDelay = maxDelay; 26 | self->minDelay = minDelay; 27 | } 28 | 29 | void Preview_setPixel(Preview *self,int x, int y,int delay, bool shouldDraw){ 30 | 31 | if(!shouldDraw){ 32 | return; 33 | } 34 | 35 | if(x > self->width-1 || y > self->height-1 || x < 0 || y < 0){ 36 | //printf("out of bounds x %i y %i\n",x,y); 37 | return; 38 | } 39 | 40 | int delayProp = Config_maxDelay() - delay; 41 | int bandWidth = Config_maxDelay() - self->minDelay; 42 | float perc = (float)delayProp/bandWidth; 43 | 44 | int g = 255.0; 45 | int r = MIN(255.0,255.0 * perc * 2.0); 46 | if (perc > 0.5){ 47 | g = (1 - perc) * 2.0 * 255; 48 | }else{ 49 | g = 255.0; 50 | } 51 | 52 | 53 | self->imageData[4 * self->width * y + 4 * x + 0] = r; 54 | self->imageData[4 * self->width * y + 4 * x + 1] = g; 55 | if (! shouldDraw){ 56 | self->imageData[4 * self->width * y + 4 * x + 2] = 255; 57 | }else{ 58 | self->imageData[4 * self->width * y + 4 * x + 2] = 0; 59 | } 60 | self->imageData[4 * self->width * y + 4 * x + 3] = 255; 61 | 62 | } 63 | 64 | void Preview_save(Preview *self){ 65 | unsigned error = lodepng_encode32_file(self->imageName, self->imageData, self->width, self->height); 66 | if(error) printf("error %u: %s\n", error, lodepng_error_text(error)); 67 | } 68 | 69 | void Preview_release(Preview *self){ 70 | self->retainCount --; 71 | if(self->retainCount == 0){ 72 | free(self->imageData); 73 | free(self); 74 | } 75 | } 76 | 77 | void Preview_retain(Preview *self){ 78 | FSObject_retain(self); 79 | } 80 | 81 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /sketchy_driver/inih/ini.h: -------------------------------------------------------------------------------- 1 | /* inih -- simple .INI file parser 2 | 3 | inih is released under the New BSD license (see LICENSE.txt). Go to the project 4 | home page for more info: 5 | 6 | http://code.google.com/p/inih/ 7 | 8 | */ 9 | 10 | #ifndef __INI_H__ 11 | #define __INI_H__ 12 | 13 | /* Make this header file easier to include in C++ code */ 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | #include 19 | 20 | /* Parse given INI-style file. May have [section]s, name=value pairs 21 | (whitespace stripped), and comments starting with ';' (semicolon). Section 22 | is "" if name=value pair parsed before any section heading. name:value 23 | pairs are also supported as a concession to Python's ConfigParser. 24 | 25 | For each name=value pair parsed, call handler function with given user 26 | pointer as well as section, name, and value (data only valid for duration 27 | of handler call). Handler should return nonzero on success, zero on error. 28 | 29 | Returns 0 on success, line number of first error on parse error (doesn't 30 | stop on first error), -1 on file open error, or -2 on memory allocation 31 | error (only when INI_USE_STACK is zero). 32 | */ 33 | int ini_parse(const char* filename, 34 | int (*handler)(void* user, const char* section, 35 | const char* name, const char* value), 36 | void* user); 37 | 38 | /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't 39 | close the file when it's finished -- the caller must do that. */ 40 | int ini_parse_file(FILE* file, 41 | int (*handler)(void* user, const char* section, 42 | const char* name, const char* value), 43 | void* user); 44 | 45 | /* Nonzero to allow multi-line value parsing, in the style of Python's 46 | ConfigParser. If allowed, ini_parse() will call the handler with the same 47 | name for each subsequent line parsed. */ 48 | #ifndef INI_ALLOW_MULTILINE 49 | #define INI_ALLOW_MULTILINE 1 50 | #endif 51 | 52 | /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of 53 | the file. See http://code.google.com/p/inih/issues/detail?id=21 */ 54 | #ifndef INI_ALLOW_BOM 55 | #define INI_ALLOW_BOM 1 56 | #endif 57 | 58 | /* Nonzero to use stack, zero to use heap (malloc/free). */ 59 | #ifndef INI_USE_STACK 60 | #define INI_USE_STACK 1 61 | #endif 62 | 63 | /* Stop parsing on first error (default is to keep parsing). */ 64 | #ifndef INI_STOP_ON_FIRST_ERROR 65 | #define INI_STOP_ON_FIRST_ERROR 0 66 | #endif 67 | 68 | /* Maximum line length for any line in INI file. */ 69 | #ifndef INI_MAX_LINE 70 | #define INI_MAX_LINE 200 71 | #endif 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif /* __INI_H__ */ 78 | -------------------------------------------------------------------------------- /sketchy_shared/sketchy-ipc.c: -------------------------------------------------------------------------------- 1 | //gcc test_server.c sketchy-ipc.c -o server 2 | //gcc test_driver.c sketchy-ipc.c -o driver 3 | //list: ipcs -m 4 | //remove: ipcrm -M 1234 5 | //remove: ipcrm -M 4567 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "sketchy-ipc.h" 14 | 15 | //driver state 16 | static int driverstate_shmsize = sizeof(DriverState); 17 | static DriverState *driverstate_shm_pt; 18 | static int driverstate_shm_id; 19 | 20 | //driver commands 21 | static int command_shmsize = sizeof(DriverCommand); 22 | static DriverCommand *command_shm_pt; 23 | static int command_shm_id; 24 | 25 | void updateDriverState(DriverSatusCode statusCode,const char *joburl,const char *name){ 26 | DriverState *driver_state = driverState(); 27 | driver_state->messageID ++; 28 | driver_state->statusCode = statusCode; 29 | strcpy( driver_state->joburl , joburl ); 30 | strcpy( driver_state->name , name ); 31 | } 32 | 33 | DriverState *driverState(){ 34 | return driverstate_shm_pt; 35 | } 36 | 37 | void setCommand(char *msg, CommandCode command, float floatValue, int intValue){ 38 | DriverCommand *cmd = getCommand(); 39 | cmd->messageID ++; 40 | cmd->commandCode = command; 41 | strcpy(cmd->msg,msg); 42 | cmd->fvalue = floatValue; 43 | cmd->ivalue = intValue; 44 | } 45 | 46 | DriverCommand *getCommand(){ 47 | return command_shm_pt; 48 | } 49 | 50 | void shmCreate() 51 | { 52 | 53 | key_t shm_key = 1234; 54 | key_t shm_key2 = 4567; 55 | 56 | // Create our memory segments 57 | if((driverstate_shm_id = shmget(shm_key, driverstate_shmsize, IPC_CREAT | 0600)) < 0) 58 | { 59 | perror("shmget"); 60 | exit(1); 61 | } 62 | 63 | if((command_shm_id = shmget(shm_key2, command_shmsize, IPC_CREAT | 0600)) < 0) 64 | { 65 | perror("shmget"); 66 | exit(1); 67 | } 68 | 69 | // Attach memory segments 70 | if((driverstate_shm_pt = shmat(driverstate_shm_id, NULL, 0)) == (DriverState *)-1) 71 | { 72 | perror("shmat"); 73 | exit(1); 74 | } 75 | 76 | if((command_shm_pt = shmat(command_shm_id, NULL, 0)) == (DriverCommand *)-1) 77 | { 78 | perror("shmat"); 79 | exit(1); 80 | } 81 | 82 | } 83 | 84 | void shmDestroy(){ 85 | 86 | // Detach the shared memory segments 87 | if (shmdt(driverstate_shm_pt) == -1) 88 | { 89 | perror("shmdt"); 90 | exit(1); 91 | } 92 | 93 | if (shmdt(command_shm_pt) == -1) 94 | { 95 | perror("shmdt"); 96 | exit(1); 97 | } 98 | 99 | // Free and delete the memory segments 100 | if(shmctl(driverstate_shm_id, IPC_RMID, 0) < 0) 101 | { 102 | perror("shmctl"); 103 | exit(1); 104 | } 105 | 106 | if(shmctl(command_shm_id, IPC_RMID, 0) < 0) 107 | { 108 | perror("shmctl"); 109 | exit(1); 110 | } 111 | 112 | } 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *NOTE: This is not a step by step guide for building a drawbot. We are working on this and will publish it when ready on [blackstripes.nl](http://www.blackstripes.nl)* 2 | 3 | Blackstripes drawbot driver 4 | =========================== 5 | 6 | This is the driver we use at [blackstripes.nl](http://www.blackstripes.nl). It controls two stepper motors and a single solenoid for pen lifting. The main features are: 7 | 8 | 1. Direct SVG workflow. Upload svg to the Raspberry-PI through web based interface, no translation to g-code needed. 9 | 2. Speed management through web interface. 10 | 3. Automatic lookahead acceleration management. 11 | 4. Simple Lua scripting interface. `moveTo(x,y) penUp() penDown()` 12 | 5. Can control V-plotters as well as other machines. 13 | 6. Standalone operation with a single Raspberry-PI 14 | 15 | 16 | ![blackstripes drawbots](bs-machines.jpg) 17 | 18 | 19 | [Example video.](https://youtu.be/Wb5XR8IF5E0) 20 | 21 | 22 | Dependencies 23 | ============ 24 | 25 | This will run on a Raspberry-PI model B+. 26 | [Xenomai](http://www.http://xenomai.org) must be Installed. 27 | 28 | This project uses the following excellent opensource software: 29 | 30 | ###LodePNG: 31 | [https://github.com/lvandeve/lodepng](https://github.com/lvandeve/lodepng) 32 | 33 | ###Nano SVG: 34 | [https://github.com/memononen/nanosvg](https://github.com/memononen/nanosvg) 35 | 36 | ###lua-5.2.3: 37 | [https://github.com/lua/lua](https://github.com/lua/lua) 38 | 39 | ###inih: 40 | [https://github.com/benhoyt/inih](https://github.com/benhoyt/inih) 41 | 42 | 43 | 44 | How to compile 45 | ============== 46 | 47 | ###On the Raspberry-PI (Xenomai enabled model B+) 48 | 49 | 1. clone this repo 50 | 2. build the server 51 | 1. cd to sketchy_server 52 | 2. `make` 53 | 3. `make install` 54 | 3. build the driver 55 | 1. cd to sketchy_driver 56 | 2. `make pi` 57 | 3. `make install` 58 | 4. build the (optional) preview-driver 59 | 1. cd to sketchy_driver 60 | 2. `make preview` 61 | 3. `make installp` 62 | 63 | Now cd into the build directory and start the server. 64 | 65 | `sudo nohup ./sketchy-server` 66 | 67 | Now find out the IP-address of your Raspberry-PI and point your webbrowser to port 8000. 68 | `(http://192.168.x.xxx:8000)` You should see this page: 69 | 70 | ![driver UI](main-ui.png) 71 | 72 | 73 | ## GPIO pins Raspberry-PI (model B+) 74 | 75 | These are the pins to be connected to the stepper drivers and the relay to control the solenoid. 76 | 77 | #define RIGHT_CLOCK RPI_V2_GPIO_P1_11 78 | #define RIGHT_DIR RPI_V2_GPIO_P1_12 79 | #define LEFT_CLOCK RPI_V2_GPIO_P1_13 80 | #define LEFT_DIR RPI_V2_GPIO_P1_15 81 | #define SOLENOID RPI_V2_GPIO_P1_16 82 | 83 | 84 | 85 | ## Cross compilation 86 | 87 | For crosscompiling we have created a crosscompile target in the make files. 88 | Our cross-compiling machine has these cross compiling tools installed: 89 | 90 | ####Toolchain 91 | git clone https://github.com/raspberrypi/tools 92 | 93 | You can then copy the toolchain to a common location such as 94 | /tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian, and add 95 | /tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin to 96 | your $PATH in the .bashrc in your home directory. 97 | For 64bit, use /tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin. 98 | While this step is not strictly necessary, it does make it easier for later command lines! 99 | 100 | To find out the lib path of this cross-compiler you need to do this 101 | 102 | $ arm-linux-gnueabihf-gcc -print-sysroot 103 | 104 | this will show where to look for libc. 105 | 106 | also: 107 | 108 | $ arm-linux-gnueabihf-gcc -print-search-dirs 109 | 110 | You need to have rpi compiled libs of xenomai and native. I got them from a xenomaied Raspberry pi from /usr/lib/ 111 | 112 | Then you need to copy these libnative.* and libxenomai.* files into this sysroot in the folder 113 | [sysroot compiler]/usr/lib/ 114 | 115 | That together with some modified targets for using the right compiler include path and you are ready to roll. 116 | 117 | -------------------------------------------------------------------------------- /sketchy_driver/makefile: -------------------------------------------------------------------------------- 1 | # define the C compiler to use 2 | CC = gcc 3 | 4 | # define library paths in addition to /usr/lib 5 | # if I wanted to include libraries not in /usr/lib I'd specify 6 | # their path using -Lpath, something like: 7 | LFLAGS = -Llua-5.2.3 -L../sketchy_shared 8 | CFLAGS_EXTRA = -I/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include 9 | # define any libraries to link into executable: 10 | # if I want to link in libraries (libx.so or libx.a) I use the -llibname 11 | # option, something like (this will link in libmylib.so and libm.so: 12 | LIBS = -lm -llua -ldl 13 | 14 | ifeq ($(type),vplotter) 15 | MACHINE_TYPE = -D__VPLOTTER__ 16 | else 17 | MACHINE_TYPE = 18 | endif 19 | 20 | ifeq ($(MAKECMDGOALS),t) 21 | MAIN = test 22 | CFLAGS = -Wall -g -D__TEST__ $(MACHINE_TYPE) 23 | INCLUDES = -I../sketchy_shared 24 | SRCS = test.c FSNumber.c FSArray.c FSObject.c Point.c Model.c Step.c sketchy.c SpeedManager.c lodepng/lodepng.c Preview.c inih/ini.c Config.c ../sketchy_shared/sketchy-ipc.c 25 | else ifeq ($(MAKECMDGOALS),pi) 26 | MAIN = sketchy-driver 27 | CFLAGS = -Wall -g -D__PI__ $(MACHINE_TYPE) 28 | INCLUDES = -I/usr/include/xenomai -Ibcm2835-1.25 -Ilua-5.2.3 -I../sketchy_shared 29 | LFLAGS = -L/usr/lib/xenomai -Llua-5.2.3 -L../sketchy_shared 30 | LIBS = -lnative -lxenomai -lm -llua -ldl 31 | SRCS = main.c FSNumber.c FSArray.c FSObject.c Point.c Model.c Step.c bcm2835-1.25/bcm2835.c SpeedManager.c sketchy.c inih/ini.c Config.c ../sketchy_shared/sketchy-ipc.c 32 | else ifeq ($(MAKECMDGOALS),pi_cc) 33 | CC = arm-linux-gnueabihf-gcc 34 | MAIN = sketchy-driver 35 | CFLAGS = -Wall -g -D__PI__ $(MACHINE_TYPE) $(CFLAGS_EXTRA) 36 | INCLUDES = -I/usr/include/xenomai -Ibcm2835-1.25 -Ilua-5.2.3 -I../sketchy_shared 37 | LFLAGS = -L/usr/lib/xenomai -Llua-5.2.3 -L../sketchy_shared 38 | LIBS = -lnative -lxenomai -lm -llua -ldl 39 | SRCS = main.c FSNumber.c FSArray.c FSObject.c Point.c Model.c Step.c bcm2835-1.25/bcm2835.c SpeedManager.c sketchy.c inih/ini.c Config.c ../sketchy_shared/sketchy-ipc.c 40 | else ifeq ($(MAKECMDGOALS),preview) 41 | MAIN = sketchy-preview 42 | CFLAGS = -Wall -g -D__SIM__ $(MACHINE_TYPE) 43 | INCLUDES = -I../sketchy_shared 44 | SRCS = main.c FSNumber.c FSArray.c FSObject.c Point.c Model.c Step.c SpeedManager.c sketchy.c lodepng/lodepng.c Preview.c inih/ini.c Config.c ../sketchy_shared/sketchy-ipc.c 45 | else 46 | MAKECMDGOALS = default 47 | MAIN = sketchy-driver 48 | CFLAGS = -Wall -g -D__SIM__ $(MACHINE_TYPE) 49 | INCLUDES = -I../sketchy_shared 50 | SRCS = main.c FSNumber.c FSArray.c FSObject.c Point.c Model.c Step.c SpeedManager.c sketchy.c lodepng/lodepng.c Preview.c inih/ini.c Config.c ../sketchy_shared/sketchy-ipc.c 51 | endif 52 | 53 | 54 | # define the C object files 55 | # 56 | # This uses Suffix Replacement within a macro: 57 | # $(name:string1=string2) 58 | # For each word in 'name' replace 'string1' with 'string2' 59 | # Below we are replacing the suffix .c of all words in the macro SRCS 60 | # with the .o suffix 61 | # 62 | OBJS = $(SRCS:.c=.o) 63 | 64 | # 65 | # The following part of the makefile is generic; it can be used to 66 | # build any executable just by changing the definitions above and by 67 | # deleting dependencies appended to the file from 'make depend' 68 | # 69 | 70 | .PHONY: depend clean test pi pi_cc preview 71 | 72 | $(MAKECMDGOALS):$(MAIN) 73 | @echo sketchy has been compiled 74 | 75 | $(MAIN):$(OBJS) 76 | $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS) 77 | 78 | # this is a suffix replacement rule for building .o's from .c's 79 | # it uses automatic variables $<: the name of the prerequisite of 80 | # the rule(a .c file) and $@: the name of the target of the rule (a .o file) 81 | # (see the gnu make manual section about automatic variables) 82 | .c.o: 83 | $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ 84 | 85 | clean: 86 | $(RM) *.o *~ sketchy test sketchy-driver sketchy-preview 87 | 88 | install: 89 | install sketchy-driver ../build 90 | 91 | installp: 92 | install sketchy-preview ../build 93 | 94 | depend: $(SRCS) 95 | makedepend $(INCLUDES) $^ 96 | 97 | # DO NOT DELETE THIS LINE -- make depend needs it 98 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | return lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int n) { 24 | Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.nupvalues = cast_byte(n); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int n) { 31 | Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; 32 | c->l.p = NULL; 33 | c->l.nupvalues = cast_byte(n); 34 | while (n--) c->l.upvals[n] = NULL; 35 | return c; 36 | } 37 | 38 | 39 | UpVal *luaF_newupval (lua_State *L) { 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 41 | uv->v = &uv->u.value; 42 | setnilvalue(uv->v); 43 | return uv; 44 | } 45 | 46 | 47 | UpVal *luaF_findupval (lua_State *L, StkId level) { 48 | global_State *g = G(L); 49 | GCObject **pp = &L->openupval; 50 | UpVal *p; 51 | UpVal *uv; 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 53 | GCObject *o = obj2gco(p); 54 | lua_assert(p->v != &p->u.value); 55 | lua_assert(!isold(o) || isold(obj2gco(L))); 56 | if (p->v == level) { /* found a corresponding upvalue? */ 57 | if (isdead(g, o)) /* is it dead? */ 58 | changewhite(o); /* resurrect it */ 59 | return p; 60 | } 61 | pp = &p->next; 62 | } 63 | /* not found: create a new one */ 64 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 65 | uv->v = level; /* current value lives in the stack */ 66 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 67 | uv->u.l.next = g->uvhead.u.l.next; 68 | uv->u.l.next->u.l.prev = uv; 69 | g->uvhead.u.l.next = uv; 70 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 71 | return uv; 72 | } 73 | 74 | 75 | static void unlinkupval (UpVal *uv) { 76 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 77 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 78 | uv->u.l.prev->u.l.next = uv->u.l.next; 79 | } 80 | 81 | 82 | void luaF_freeupval (lua_State *L, UpVal *uv) { 83 | if (uv->v != &uv->u.value) /* is it open? */ 84 | unlinkupval(uv); /* remove from open list */ 85 | luaM_free(L, uv); /* free upvalue */ 86 | } 87 | 88 | 89 | void luaF_close (lua_State *L, StkId level) { 90 | UpVal *uv; 91 | global_State *g = G(L); 92 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 93 | GCObject *o = obj2gco(uv); 94 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 95 | L->openupval = uv->next; /* remove from `open' list */ 96 | if (isdead(g, o)) 97 | luaF_freeupval(L, uv); /* free upvalue */ 98 | else { 99 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 100 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 101 | uv->v = &uv->u.value; /* now current value lives here */ 102 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 103 | g->allgc = o; 104 | luaC_checkupvalcolor(g, uv); 105 | } 106 | } 107 | } 108 | 109 | 110 | Proto *luaF_newproto (lua_State *L) { 111 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 112 | f->k = NULL; 113 | f->sizek = 0; 114 | f->p = NULL; 115 | f->sizep = 0; 116 | f->code = NULL; 117 | f->cache = NULL; 118 | f->sizecode = 0; 119 | f->lineinfo = NULL; 120 | f->sizelineinfo = 0; 121 | f->upvalues = NULL; 122 | f->sizeupvalues = 0; 123 | f->numparams = 0; 124 | f->is_vararg = 0; 125 | f->maxstacksize = 0; 126 | f->locvars = NULL; 127 | f->sizelocvars = 0; 128 | f->linedefined = 0; 129 | f->lastlinedefined = 0; 130 | f->source = NULL; 131 | return f; 132 | } 133 | 134 | 135 | void luaF_freeproto (lua_State *L, Proto *f) { 136 | luaM_freearray(L, f->code, f->sizecode); 137 | luaM_freearray(L, f->p, f->sizep); 138 | luaM_freearray(L, f->k, f->sizek); 139 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 140 | luaM_freearray(L, f->locvars, f->sizelocvars); 141 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 142 | luaM_free(L, f); 143 | } 144 | 145 | 146 | /* 147 | ** Look for n-th local variable at line `line' in function `func'. 148 | ** Returns NULL if not found. 149 | */ 150 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 151 | int i; 152 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 153 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 154 | local_number--; 155 | if (local_number == 0) 156 | return getstr(f->locvars[i].varname); 157 | } 158 | } 159 | return NULL; /* not found */ 160 | } 161 | 162 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lua.h" 11 | 12 | #include "lauxlib.h" 13 | #include "lualib.h" 14 | 15 | 16 | /* number of bits to consider in a number */ 17 | #if !defined(LUA_NBITS) 18 | #define LUA_NBITS 32 19 | #endif 20 | 21 | 22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 23 | 24 | /* macro to trim extra bits */ 25 | #define trim(x) ((x) & ALLONES) 26 | 27 | 28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 30 | 31 | 32 | typedef lua_Unsigned b_uint; 33 | 34 | 35 | 36 | static b_uint andaux (lua_State *L) { 37 | int i, n = lua_gettop(L); 38 | b_uint r = ~(b_uint)0; 39 | for (i = 1; i <= n; i++) 40 | r &= luaL_checkunsigned(L, i); 41 | return trim(r); 42 | } 43 | 44 | 45 | static int b_and (lua_State *L) { 46 | b_uint r = andaux(L); 47 | lua_pushunsigned(L, r); 48 | return 1; 49 | } 50 | 51 | 52 | static int b_test (lua_State *L) { 53 | b_uint r = andaux(L); 54 | lua_pushboolean(L, r != 0); 55 | return 1; 56 | } 57 | 58 | 59 | static int b_or (lua_State *L) { 60 | int i, n = lua_gettop(L); 61 | b_uint r = 0; 62 | for (i = 1; i <= n; i++) 63 | r |= luaL_checkunsigned(L, i); 64 | lua_pushunsigned(L, trim(r)); 65 | return 1; 66 | } 67 | 68 | 69 | static int b_xor (lua_State *L) { 70 | int i, n = lua_gettop(L); 71 | b_uint r = 0; 72 | for (i = 1; i <= n; i++) 73 | r ^= luaL_checkunsigned(L, i); 74 | lua_pushunsigned(L, trim(r)); 75 | return 1; 76 | } 77 | 78 | 79 | static int b_not (lua_State *L) { 80 | b_uint r = ~luaL_checkunsigned(L, 1); 81 | lua_pushunsigned(L, trim(r)); 82 | return 1; 83 | } 84 | 85 | 86 | static int b_shift (lua_State *L, b_uint r, int i) { 87 | if (i < 0) { /* shift right? */ 88 | i = -i; 89 | r = trim(r); 90 | if (i >= LUA_NBITS) r = 0; 91 | else r >>= i; 92 | } 93 | else { /* shift left */ 94 | if (i >= LUA_NBITS) r = 0; 95 | else r <<= i; 96 | r = trim(r); 97 | } 98 | lua_pushunsigned(L, r); 99 | return 1; 100 | } 101 | 102 | 103 | static int b_lshift (lua_State *L) { 104 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); 105 | } 106 | 107 | 108 | static int b_rshift (lua_State *L) { 109 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); 110 | } 111 | 112 | 113 | static int b_arshift (lua_State *L) { 114 | b_uint r = luaL_checkunsigned(L, 1); 115 | int i = luaL_checkint(L, 2); 116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) 117 | return b_shift(L, r, -i); 118 | else { /* arithmetic shift for 'negative' number */ 119 | if (i >= LUA_NBITS) r = ALLONES; 120 | else 121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ 122 | lua_pushunsigned(L, r); 123 | return 1; 124 | } 125 | } 126 | 127 | 128 | static int b_rot (lua_State *L, int i) { 129 | b_uint r = luaL_checkunsigned(L, 1); 130 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ 131 | r = trim(r); 132 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 133 | r = (r << i) | (r >> (LUA_NBITS - i)); 134 | lua_pushunsigned(L, trim(r)); 135 | return 1; 136 | } 137 | 138 | 139 | static int b_lrot (lua_State *L) { 140 | return b_rot(L, luaL_checkint(L, 2)); 141 | } 142 | 143 | 144 | static int b_rrot (lua_State *L) { 145 | return b_rot(L, -luaL_checkint(L, 2)); 146 | } 147 | 148 | 149 | /* 150 | ** get field and width arguments for field-manipulation functions, 151 | ** checking whether they are valid. 152 | ** ('luaL_error' called without 'return' to avoid later warnings about 153 | ** 'width' being used uninitialized.) 154 | */ 155 | static int fieldargs (lua_State *L, int farg, int *width) { 156 | int f = luaL_checkint(L, farg); 157 | int w = luaL_optint(L, farg + 1, 1); 158 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 159 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 160 | if (f + w > LUA_NBITS) 161 | luaL_error(L, "trying to access non-existent bits"); 162 | *width = w; 163 | return f; 164 | } 165 | 166 | 167 | static int b_extract (lua_State *L) { 168 | int w; 169 | b_uint r = luaL_checkunsigned(L, 1); 170 | int f = fieldargs(L, 2, &w); 171 | r = (r >> f) & mask(w); 172 | lua_pushunsigned(L, r); 173 | return 1; 174 | } 175 | 176 | 177 | static int b_replace (lua_State *L) { 178 | int w; 179 | b_uint r = luaL_checkunsigned(L, 1); 180 | b_uint v = luaL_checkunsigned(L, 2); 181 | int f = fieldargs(L, 3, &w); 182 | int m = mask(w); 183 | v &= m; /* erase bits outside given width */ 184 | r = (r & ~(m << f)) | (v << f); 185 | lua_pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static const luaL_Reg bitlib[] = { 191 | {"arshift", b_arshift}, 192 | {"band", b_and}, 193 | {"bnot", b_not}, 194 | {"bor", b_or}, 195 | {"bxor", b_xor}, 196 | {"btest", b_test}, 197 | {"extract", b_extract}, 198 | {"lrotate", b_lrot}, 199 | {"lshift", b_lshift}, 200 | {"replace", b_replace}, 201 | {"rrotate", b_rrot}, 202 | {"rshift", b_rshift}, 203 | {NULL, NULL} 204 | }; 205 | 206 | 207 | 208 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 209 | luaL_newlib(L, bitlib); 210 | return 1; 211 | } 212 | 213 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | /* 22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 23 | ** compute its hash 24 | */ 25 | #if !defined(LUAI_HASHLIMIT) 26 | #define LUAI_HASHLIMIT 5 27 | #endif 28 | 29 | 30 | /* 31 | ** equality for long strings 32 | */ 33 | int luaS_eqlngstr (TString *a, TString *b) { 34 | size_t len = a->tsv.len; 35 | lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); 36 | return (a == b) || /* same instance or... */ 37 | ((len == b->tsv.len) && /* equal length and ... */ 38 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 39 | } 40 | 41 | 42 | /* 43 | ** equality for strings 44 | */ 45 | int luaS_eqstr (TString *a, TString *b) { 46 | return (a->tsv.tt == b->tsv.tt) && 47 | (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); 48 | } 49 | 50 | 51 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 52 | unsigned int h = seed ^ cast(unsigned int, l); 53 | size_t l1; 54 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 55 | for (l1 = l; l1 >= step; l1 -= step) 56 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 57 | return h; 58 | } 59 | 60 | 61 | /* 62 | ** resizes the string table 63 | */ 64 | void luaS_resize (lua_State *L, int newsize) { 65 | int i; 66 | stringtable *tb = &G(L)->strt; 67 | /* cannot resize while GC is traversing strings */ 68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 69 | if (newsize > tb->size) { 70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 72 | } 73 | /* rehash */ 74 | for (i=0; isize; i++) { 75 | GCObject *p = tb->hash[i]; 76 | tb->hash[i] = NULL; 77 | while (p) { /* for each node in the list */ 78 | GCObject *next = gch(p)->next; /* save next */ 79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 80 | gch(p)->next = tb->hash[h]; /* chain it */ 81 | tb->hash[h] = p; 82 | resetoldbit(p); /* see MOVE OLD rule */ 83 | p = next; 84 | } 85 | } 86 | if (newsize < tb->size) { 87 | /* shrinking slice must be empty */ 88 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 89 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 90 | } 91 | tb->size = newsize; 92 | } 93 | 94 | 95 | /* 96 | ** creates a new string object 97 | */ 98 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 99 | int tag, unsigned int h, GCObject **list) { 100 | TString *ts; 101 | size_t totalsize; /* total size of TString object */ 102 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 103 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; 104 | ts->tsv.len = l; 105 | ts->tsv.hash = h; 106 | ts->tsv.extra = 0; 107 | memcpy(ts+1, str, l*sizeof(char)); 108 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 109 | return ts; 110 | } 111 | 112 | 113 | /* 114 | ** creates a new short string, inserting it into string table 115 | */ 116 | static TString *newshrstr (lua_State *L, const char *str, size_t l, 117 | unsigned int h) { 118 | GCObject **list; /* (pointer to) list where it will be inserted */ 119 | stringtable *tb = &G(L)->strt; 120 | TString *s; 121 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 122 | luaS_resize(L, tb->size*2); /* too crowded */ 123 | list = &tb->hash[lmod(h, tb->size)]; 124 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); 125 | tb->nuse++; 126 | return s; 127 | } 128 | 129 | 130 | /* 131 | ** checks whether short string exists and reuses it or creates a new one 132 | */ 133 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 134 | GCObject *o; 135 | global_State *g = G(L); 136 | unsigned int h = luaS_hash(str, l, g->seed); 137 | for (o = g->strt.hash[lmod(h, g->strt.size)]; 138 | o != NULL; 139 | o = gch(o)->next) { 140 | TString *ts = rawgco2ts(o); 141 | if (h == ts->tsv.hash && 142 | l == ts->tsv.len && 143 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 144 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 145 | changewhite(o); /* resurrect it */ 146 | return ts; 147 | } 148 | } 149 | return newshrstr(L, str, l, h); /* not found; create a new string */ 150 | } 151 | 152 | 153 | /* 154 | ** new string (with explicit length) 155 | */ 156 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 157 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 158 | return internshrstr(L, str, l); 159 | else { 160 | if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 161 | luaM_toobig(L); 162 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); 163 | } 164 | } 165 | 166 | 167 | /* 168 | ** new zero-terminated string 169 | */ 170 | TString *luaS_new (lua_State *L, const char *str) { 171 | return luaS_newlstr(L, str, strlen(str)); 172 | } 173 | 174 | 175 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 176 | Udata *u; 177 | if (s > MAX_SIZET - sizeof(Udata)) 178 | luaM_toobig(L); 179 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 180 | u->uv.len = s; 181 | u->uv.metatable = NULL; 182 | u->uv.env = e; 183 | return u; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /sketchy_driver/inih/ini.c: -------------------------------------------------------------------------------- 1 | /* inih -- simple .INI file parser 2 | 3 | inih is released under the New BSD license (see LICENSE.txt). Go to the project 4 | home page for more info: 5 | 6 | http://code.google.com/p/inih/ 7 | 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "ini.h" 15 | 16 | #if !INI_USE_STACK 17 | #include 18 | #endif 19 | 20 | #define MAX_SECTION 50 21 | #define MAX_NAME 50 22 | 23 | /* Strip whitespace chars off end of given string, in place. Return s. */ 24 | static char* rstrip(char* s) 25 | { 26 | char* p = s + strlen(s); 27 | while (p > s && isspace((unsigned char)(*--p))) 28 | *p = '\0'; 29 | return s; 30 | } 31 | 32 | /* Return pointer to first non-whitespace char in given string. */ 33 | static char* lskip(const char* s) 34 | { 35 | while (*s && isspace((unsigned char)(*s))) 36 | s++; 37 | return (char*)s; 38 | } 39 | 40 | /* Return pointer to first char c or ';' comment in given string, or pointer to 41 | null at end of string if neither found. ';' must be prefixed by a whitespace 42 | character to register as a comment. */ 43 | static char* find_char_or_comment(const char* s, char c) 44 | { 45 | int was_whitespace = 0; 46 | while (*s && *s != c && !(was_whitespace && *s == ';')) { 47 | was_whitespace = isspace((unsigned char)(*s)); 48 | s++; 49 | } 50 | return (char*)s; 51 | } 52 | 53 | /* Version of strncpy that ensures dest (size bytes) is null-terminated. */ 54 | static char* strncpy0(char* dest, const char* src, size_t size) 55 | { 56 | strncpy(dest, src, size); 57 | dest[size - 1] = '\0'; 58 | return dest; 59 | } 60 | 61 | /* See documentation in header file. */ 62 | int ini_parse_file(FILE* file, 63 | int (*handler)(void*, const char*, const char*, 64 | const char*), 65 | void* user) 66 | { 67 | /* Uses a fair bit of stack (use heap instead if you need to) */ 68 | #if INI_USE_STACK 69 | char line[INI_MAX_LINE]; 70 | #else 71 | char* line; 72 | #endif 73 | char section[MAX_SECTION] = ""; 74 | char prev_name[MAX_NAME] = ""; 75 | 76 | char* start; 77 | char* end; 78 | char* name; 79 | char* value; 80 | int lineno = 0; 81 | int error = 0; 82 | 83 | #if !INI_USE_STACK 84 | line = (char*)malloc(INI_MAX_LINE); 85 | if (!line) { 86 | return -2; 87 | } 88 | #endif 89 | 90 | /* Scan through file line by line */ 91 | while (fgets(line, INI_MAX_LINE, file) != NULL) { 92 | lineno++; 93 | 94 | start = line; 95 | #if INI_ALLOW_BOM 96 | if (lineno == 1 && (unsigned char)start[0] == 0xEF && 97 | (unsigned char)start[1] == 0xBB && 98 | (unsigned char)start[2] == 0xBF) { 99 | start += 3; 100 | } 101 | #endif 102 | start = lskip(rstrip(start)); 103 | 104 | if (*start == ';' || *start == '#') { 105 | /* Per Python ConfigParser, allow '#' comments at start of line */ 106 | } 107 | #if INI_ALLOW_MULTILINE 108 | else if (*prev_name && *start && start > line) { 109 | /* Non-black line with leading whitespace, treat as continuation 110 | of previous name's value (as per Python ConfigParser). */ 111 | if (!handler(user, section, prev_name, start) && !error) 112 | error = lineno; 113 | } 114 | #endif 115 | else if (*start == '[') { 116 | /* A "[section]" line */ 117 | end = find_char_or_comment(start + 1, ']'); 118 | if (*end == ']') { 119 | *end = '\0'; 120 | strncpy0(section, start + 1, sizeof(section)); 121 | *prev_name = '\0'; 122 | } 123 | else if (!error) { 124 | /* No ']' found on section line */ 125 | error = lineno; 126 | } 127 | } 128 | else if (*start && *start != ';') { 129 | /* Not a comment, must be a name[=:]value pair */ 130 | end = find_char_or_comment(start, '='); 131 | if (*end != '=') { 132 | end = find_char_or_comment(start, ':'); 133 | } 134 | if (*end == '=' || *end == ':') { 135 | *end = '\0'; 136 | name = rstrip(start); 137 | value = lskip(end + 1); 138 | end = find_char_or_comment(value, '\0'); 139 | if (*end == ';') 140 | *end = '\0'; 141 | rstrip(value); 142 | 143 | /* Valid name[=:]value pair found, call handler */ 144 | strncpy0(prev_name, name, sizeof(prev_name)); 145 | if (!handler(user, section, name, value) && !error) 146 | error = lineno; 147 | } 148 | else if (!error) { 149 | /* No '=' or ':' found on name[=:]value line */ 150 | error = lineno; 151 | } 152 | } 153 | 154 | #if INI_STOP_ON_FIRST_ERROR 155 | if (error) 156 | break; 157 | #endif 158 | } 159 | 160 | #if !INI_USE_STACK 161 | free(line); 162 | #endif 163 | 164 | return error; 165 | } 166 | 167 | /* See documentation in header file. */ 168 | int ini_parse(const char* filename, 169 | int (*handler)(void*, const char*, const char*, const char*), 170 | void* user) 171 | { 172 | FILE* file; 173 | int error; 174 | 175 | file = fopen(filename, "r"); 176 | if (!file) 177 | return -1; 178 | error = ini_parse_file(file, handler, user); 179 | fclose(file); 180 | return error; 181 | } 182 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSsweepstring 2 42 | #define GCSsweepudata 3 43 | #define GCSsweep 4 44 | #define GCSpause 5 45 | 46 | 47 | #define issweepphase(g) \ 48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 49 | 50 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 51 | 52 | /* 53 | ** macros to tell when main invariant (white objects cannot point to black 54 | ** ones) must be kept. During a non-generational collection, the sweep 55 | ** phase may break the invariant, as objects turned white may point to 56 | ** still-black objects. The invariant is restored when sweep ends and 57 | ** all objects are white again. During a generational collection, the 58 | ** invariant must be kept all times. 59 | */ 60 | 61 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** Outside the collector, the state in generational mode is kept in 66 | ** 'propagate', so 'keepinvariant' is always true. 67 | */ 68 | #define keepinvariantout(g) \ 69 | check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ 70 | g->gcstate <= GCSatomic) 71 | 72 | 73 | /* 74 | ** some useful bit tricks 75 | */ 76 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 77 | #define setbits(x,m) ((x) |= (m)) 78 | #define testbits(x,m) ((x) & (m)) 79 | #define bitmask(b) (1<<(b)) 80 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 81 | #define l_setbit(x,b) setbits(x, bitmask(b)) 82 | #define resetbit(x,b) resetbits(x, bitmask(b)) 83 | #define testbit(x,b) testbits(x, bitmask(b)) 84 | 85 | 86 | /* Layout for bit use in `marked' field: */ 87 | #define WHITE0BIT 0 /* object is white (type 0) */ 88 | #define WHITE1BIT 1 /* object is white (type 1) */ 89 | #define BLACKBIT 2 /* object is black */ 90 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 91 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 92 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 93 | #define OLDBIT 6 /* object is old (only in generational mode) */ 94 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 95 | 96 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 97 | 98 | 99 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 100 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 101 | #define isgray(x) /* neither white nor black */ \ 102 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 103 | 104 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 105 | 106 | /* MOVE OLD rule: whenever an object is moved to the beginning of 107 | a GC list, its old bit must be cleared */ 108 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 109 | 110 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 111 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 112 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 113 | 114 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 115 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 116 | 117 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 118 | 119 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 120 | 121 | 122 | #define luaC_condGC(L,c) \ 123 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 124 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 125 | 126 | 127 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 128 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 129 | 130 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 131 | luaC_barrierback_(L,p); } 132 | 133 | #define luaC_objbarrier(L,p,o) \ 134 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 135 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 136 | 137 | #define luaC_objbarrierback(L,p,o) \ 138 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 139 | 140 | #define luaC_barrierproto(L,p,c) \ 141 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 142 | 143 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 144 | LUAI_FUNC void luaC_step (lua_State *L); 145 | LUAI_FUNC void luaC_forcestep (lua_State *L); 146 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 147 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 148 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 149 | GCObject **list, int offset); 150 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 151 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 152 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 153 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 154 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 155 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | static l_noret error(LoadState* S, const char* why) 31 | { 32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); 33 | luaD_throw(S->L,LUA_ERRSYNTAX); 34 | } 35 | 36 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 37 | #define LoadByte(S) (lu_byte)LoadChar(S) 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 40 | 41 | #if !defined(luai_verifycode) 42 | #define luai_verifycode(L,b,f) /* empty */ 43 | #endif 44 | 45 | static void LoadBlock(LoadState* S, void* b, size_t size) 46 | { 47 | if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); 48 | } 49 | 50 | static int LoadChar(LoadState* S) 51 | { 52 | char x; 53 | LoadVar(S,x); 54 | return x; 55 | } 56 | 57 | static int LoadInt(LoadState* S) 58 | { 59 | int x; 60 | LoadVar(S,x); 61 | if (x<0) error(S,"corrupted"); 62 | return x; 63 | } 64 | 65 | static lua_Number LoadNumber(LoadState* S) 66 | { 67 | lua_Number x; 68 | LoadVar(S,x); 69 | return x; 70 | } 71 | 72 | static TString* LoadString(LoadState* S) 73 | { 74 | size_t size; 75 | LoadVar(S,size); 76 | if (size==0) 77 | return NULL; 78 | else 79 | { 80 | char* s=luaZ_openspace(S->L,S->b,size); 81 | LoadBlock(S,s,size*sizeof(char)); 82 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 83 | } 84 | } 85 | 86 | static void LoadCode(LoadState* S, Proto* f) 87 | { 88 | int n=LoadInt(S); 89 | f->code=luaM_newvector(S->L,n,Instruction); 90 | f->sizecode=n; 91 | LoadVector(S,f->code,n,sizeof(Instruction)); 92 | } 93 | 94 | static void LoadFunction(LoadState* S, Proto* f); 95 | 96 | static void LoadConstants(LoadState* S, Proto* f) 97 | { 98 | int i,n; 99 | n=LoadInt(S); 100 | f->k=luaM_newvector(S->L,n,TValue); 101 | f->sizek=n; 102 | for (i=0; ik[i]); 103 | for (i=0; ik[i]; 106 | int t=LoadChar(S); 107 | switch (t) 108 | { 109 | case LUA_TNIL: 110 | setnilvalue(o); 111 | break; 112 | case LUA_TBOOLEAN: 113 | setbvalue(o,LoadChar(S)); 114 | break; 115 | case LUA_TNUMBER: 116 | setnvalue(o,LoadNumber(S)); 117 | break; 118 | case LUA_TSTRING: 119 | setsvalue2n(S->L,o,LoadString(S)); 120 | break; 121 | default: lua_assert(0); 122 | } 123 | } 124 | n=LoadInt(S); 125 | f->p=luaM_newvector(S->L,n,Proto*); 126 | f->sizep=n; 127 | for (i=0; ip[i]=NULL; 128 | for (i=0; ip[i]=luaF_newproto(S->L); 131 | LoadFunction(S,f->p[i]); 132 | } 133 | } 134 | 135 | static void LoadUpvalues(LoadState* S, Proto* f) 136 | { 137 | int i,n; 138 | n=LoadInt(S); 139 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); 140 | f->sizeupvalues=n; 141 | for (i=0; iupvalues[i].name=NULL; 142 | for (i=0; iupvalues[i].instack=LoadByte(S); 145 | f->upvalues[i].idx=LoadByte(S); 146 | } 147 | } 148 | 149 | static void LoadDebug(LoadState* S, Proto* f) 150 | { 151 | int i,n; 152 | f->source=LoadString(S); 153 | n=LoadInt(S); 154 | f->lineinfo=luaM_newvector(S->L,n,int); 155 | f->sizelineinfo=n; 156 | LoadVector(S,f->lineinfo,n,sizeof(int)); 157 | n=LoadInt(S); 158 | f->locvars=luaM_newvector(S->L,n,LocVar); 159 | f->sizelocvars=n; 160 | for (i=0; ilocvars[i].varname=NULL; 161 | for (i=0; ilocvars[i].varname=LoadString(S); 164 | f->locvars[i].startpc=LoadInt(S); 165 | f->locvars[i].endpc=LoadInt(S); 166 | } 167 | n=LoadInt(S); 168 | for (i=0; iupvalues[i].name=LoadString(S); 169 | } 170 | 171 | static void LoadFunction(LoadState* S, Proto* f) 172 | { 173 | f->linedefined=LoadInt(S); 174 | f->lastlinedefined=LoadInt(S); 175 | f->numparams=LoadByte(S); 176 | f->is_vararg=LoadByte(S); 177 | f->maxstacksize=LoadByte(S); 178 | LoadCode(S,f); 179 | LoadConstants(S,f); 180 | LoadUpvalues(S,f); 181 | LoadDebug(S,f); 182 | } 183 | 184 | /* the code below must be consistent with the code in luaU_header */ 185 | #define N0 LUAC_HEADERSIZE 186 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) 187 | #define N2 N1+2 188 | #define N3 N2+6 189 | 190 | static void LoadHeader(LoadState* S) 191 | { 192 | lu_byte h[LUAC_HEADERSIZE]; 193 | lu_byte s[LUAC_HEADERSIZE]; 194 | luaU_header(h); 195 | memcpy(s,h,sizeof(char)); /* first char already read */ 196 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); 197 | if (memcmp(h,s,N0)==0) return; 198 | if (memcmp(h,s,N1)!=0) error(S,"not a"); 199 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); 200 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); 201 | } 202 | 203 | /* 204 | ** load precompiled chunk 205 | */ 206 | Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 207 | { 208 | LoadState S; 209 | Closure* cl; 210 | if (*name=='@' || *name=='=') 211 | S.name=name+1; 212 | else if (*name==LUA_SIGNATURE[0]) 213 | S.name="binary string"; 214 | else 215 | S.name=name; 216 | S.L=L; 217 | S.Z=Z; 218 | S.b=buff; 219 | LoadHeader(&S); 220 | cl=luaF_newLclosure(L,1); 221 | setclLvalue(L,L->top,cl); incr_top(L); 222 | cl->l.p=luaF_newproto(L); 223 | LoadFunction(&S,cl->l.p); 224 | if (cl->l.p->sizeupvalues != 1) 225 | { 226 | Proto* p=cl->l.p; 227 | cl=luaF_newLclosure(L,cl->l.p->sizeupvalues); 228 | cl->l.p=p; 229 | setclLvalue(L,L->top-1,cl); 230 | } 231 | luai_verifycode(L,buff,cl->l.p); 232 | return cl; 233 | } 234 | 235 | #define MYINT(s) (s[0]-'0') 236 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) 237 | #define FORMAT 0 /* this is the official format */ 238 | 239 | /* 240 | * make header for precompiled chunks 241 | * if you change the code below be sure to update LoadHeader and FORMAT above 242 | * and LUAC_HEADERSIZE in lundump.h 243 | */ 244 | void luaU_header (lu_byte* h) 245 | { 246 | int x=1; 247 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); 248 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); 249 | *h++=cast_byte(VERSION); 250 | *h++=cast_byte(FORMAT); 251 | *h++=cast_byte(*(char*)&x); /* endianness */ 252 | *h++=cast_byte(sizeof(int)); 253 | *h++=cast_byte(sizeof(size_t)); 254 | *h++=cast_byte(sizeof(Instruction)); 255 | *h++=cast_byte(sizeof(lua_Number)); 256 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); 258 | } 259 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for building Lua 2 | # See ../doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= none 8 | 9 | CC= gcc 10 | CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) 11 | LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) 12 | LIBS= -lm $(SYSLIBS) $(MYLIBS) 13 | 14 | AR= ar rcu 15 | RANLIB= ranlib 16 | RM= rm -f 17 | 18 | SYSCFLAGS= 19 | SYSLDFLAGS= 20 | SYSLIBS= 21 | 22 | MYCFLAGS= 23 | MYLDFLAGS= 24 | MYLIBS= 25 | MYOBJS= 26 | 27 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 28 | 29 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 30 | 31 | LUA_A= liblua.a 32 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ 33 | lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ 34 | ltm.o lundump.o lvm.o lzio.o 35 | LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \ 36 | lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o 37 | BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) 38 | 39 | LUA_T= lua 40 | LUA_O= lua.o 41 | 42 | LUAC_T= luac 43 | LUAC_O= luac.o 44 | 45 | ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) 46 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 47 | ALL_A= $(LUA_A) 48 | 49 | # Targets start here. 50 | default: $(PLAT) 51 | 52 | all: $(ALL_T) 53 | 54 | o: $(ALL_O) 55 | 56 | a: $(ALL_A) 57 | 58 | $(LUA_A): $(BASE_O) 59 | $(AR) $@ $(BASE_O) 60 | $(RANLIB) $@ 61 | 62 | $(LUA_T): $(LUA_O) $(LUA_A) 63 | $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 64 | 65 | $(LUAC_T): $(LUAC_O) $(LUA_A) 66 | $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 67 | 68 | clean: 69 | $(RM) $(ALL_T) $(ALL_O) 70 | 71 | depend: 72 | @$(CC) $(CFLAGS) -MM l*.c 73 | 74 | echo: 75 | @echo "PLAT= $(PLAT)" 76 | @echo "CC= $(CC)" 77 | @echo "CFLAGS= $(CFLAGS)" 78 | @echo "LDFLAGS= $(SYSLDFLAGS)" 79 | @echo "LIBS= $(LIBS)" 80 | @echo "AR= $(AR)" 81 | @echo "RANLIB= $(RANLIB)" 82 | @echo "RM= $(RM)" 83 | 84 | # Convenience targets for popular platforms 85 | ALL= all 86 | 87 | none: 88 | @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" 89 | @echo " $(PLATS)" 90 | 91 | aix: 92 | $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" 93 | 94 | ansi: 95 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI" 96 | 97 | bsd: 98 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" 99 | 100 | freebsd: 101 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" 102 | 103 | generic: $(ALL) 104 | 105 | linux: 106 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline" 107 | 108 | macosx: 109 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc 110 | 111 | mingw: 112 | $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ 113 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 114 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe 115 | $(MAKE) "LUAC_T=luac.exe" luac.exe 116 | 117 | posix: 118 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" 119 | 120 | solaris: 121 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" 122 | 123 | # list targets that do not create files (but not all makes understand .PHONY) 124 | .PHONY: all $(PLATS) default o a clean depend echo none 125 | 126 | # DO NOT DELETE 127 | 128 | lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 129 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \ 130 | lvm.h 131 | lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h 132 | lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h 133 | lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h 134 | lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 135 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ 136 | lstring.h ltable.h lvm.h 137 | lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h 138 | lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h 139 | ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h 140 | ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 141 | ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \ 142 | lfunc.h lstring.h lgc.h ltable.h lvm.h 143 | ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 144 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \ 145 | lstring.h ltable.h lundump.h lvm.h 146 | ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ 147 | lzio.h lmem.h lundump.h 148 | lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \ 149 | lstate.h ltm.h lzio.h lmem.h 150 | lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 151 | lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 152 | linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h 153 | liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h 154 | llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \ 155 | lstate.h ltm.h lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h 156 | lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h 157 | lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 158 | ltm.h lzio.h lmem.h ldo.h lgc.h 159 | loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h 160 | lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \ 161 | lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h 162 | lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h 163 | loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h 164 | lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 165 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \ 166 | lstring.h lgc.h ltable.h 167 | lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 168 | ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \ 169 | ltable.h 170 | lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ 171 | ltm.h lzio.h lstring.h lgc.h 172 | lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h 173 | ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 174 | ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h 175 | ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h 176 | ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ 177 | lmem.h lstring.h lgc.h ltable.h 178 | lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h 179 | luac.o: luac.c lua.h luaconf.h lauxlib.h lobject.h llimits.h lstate.h \ 180 | ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h 181 | lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ 182 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h 183 | lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 184 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h 185 | lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ 186 | lzio.h 187 | 188 | -------------------------------------------------------------------------------- /sketchy_server/mongoose/mongoose.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2004-2013 Sergey Lyubka 2 | // Copyright (c) 2013-2014 Cesanta Software Limited 3 | // All rights reserved 4 | // 5 | // This software is dual-licensed: you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License version 2 as 7 | // published by the Free Software Foundation. For the terms of this 8 | // license, see . 9 | // 10 | // You are free to use this software under the terms of the GNU General 11 | // Public License, but WITHOUT ANY WARRANTY; without even the implied 12 | // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 | // See the GNU General Public License for more details. 14 | // 15 | // Alternatively, you can license this software under a commercial 16 | // license, as set out in . 17 | 18 | #ifndef MONGOOSE_HEADER_INCLUDED 19 | #define MONGOOSE_HEADER_INCLUDED 20 | 21 | #define MONGOOSE_VERSION "5.6" 22 | 23 | #include // required for FILE 24 | #include // required for size_t 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif // __cplusplus 29 | 30 | // This structure contains information about HTTP request. 31 | struct mg_connection { 32 | const char *request_method; // "GET", "POST", etc 33 | const char *uri; // URL-decoded URI 34 | const char *http_version; // E.g. "1.0", "1.1" 35 | const char *query_string; // URL part after '?', not including '?', or NULL 36 | 37 | char remote_ip[48]; // Max IPv6 string length is 45 characters 38 | char local_ip[48]; // Local IP address 39 | unsigned short remote_port; // Client's port 40 | unsigned short local_port; // Local port number 41 | 42 | int num_headers; // Number of HTTP headers 43 | struct mg_header { 44 | const char *name; // HTTP header name 45 | const char *value; // HTTP header value 46 | } http_headers[30]; 47 | 48 | char *content; // POST (or websocket message) data, or NULL 49 | size_t content_len; // Data length 50 | 51 | int is_websocket; // Connection is a websocket connection 52 | int status_code; // HTTP status code for HTTP error handler 53 | int wsbits; // First byte of the websocket frame 54 | void *server_param; // Parameter passed to mg_create_server() 55 | void *connection_param; // Placeholder for connection-specific data 56 | void *callback_param; 57 | }; 58 | 59 | struct mg_server; // Opaque structure describing server instance 60 | enum mg_result { MG_FALSE, MG_TRUE, MG_MORE }; 61 | enum mg_event { 62 | MG_POLL = 100, // Callback return value is ignored 63 | MG_CONNECT, // If callback returns MG_FALSE, connect fails 64 | MG_AUTH, // If callback returns MG_FALSE, authentication fails 65 | MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req 66 | MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection 67 | MG_RECV, // Mongoose has received POST data chunk. 68 | // Callback should return a number of bytes to discard from 69 | // the receive buffer, or -1 to close the connection. 70 | MG_CLOSE, // Connection is closed, callback return value is ignored 71 | MG_WS_HANDSHAKE, // New websocket connection, handshake request 72 | MG_WS_CONNECT, // New websocket connection established 73 | MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err 74 | }; 75 | typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event); 76 | 77 | // Websocket opcodes, from http://tools.ietf.org/html/rfc6455 78 | enum { 79 | WEBSOCKET_OPCODE_CONTINUATION = 0x0, 80 | WEBSOCKET_OPCODE_TEXT = 0x1, 81 | WEBSOCKET_OPCODE_BINARY = 0x2, 82 | WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, 83 | WEBSOCKET_OPCODE_PING = 0x9, 84 | WEBSOCKET_OPCODE_PONG = 0xa 85 | }; 86 | 87 | // Server management functions 88 | struct mg_server *mg_create_server(void *server_param, mg_handler_t handler); 89 | void mg_destroy_server(struct mg_server **); 90 | const char *mg_set_option(struct mg_server *, const char *opt, const char *val); 91 | int mg_poll_server(struct mg_server *, int milliseconds); 92 | const char **mg_get_valid_option_names(void); 93 | const char *mg_get_option(const struct mg_server *server, const char *name); 94 | void mg_copy_listeners(struct mg_server *from, struct mg_server *to); 95 | struct mg_connection *mg_next(struct mg_server *, struct mg_connection *); 96 | void mg_wakeup_server(struct mg_server *); 97 | void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...); 98 | struct mg_connection *mg_connect(struct mg_server *, const char *); 99 | 100 | // Connection management functions 101 | void mg_send_status(struct mg_connection *, int status_code); 102 | void mg_send_header(struct mg_connection *, const char *name, const char *val); 103 | size_t mg_send_data(struct mg_connection *, const void *data, int data_len); 104 | size_t mg_printf_data(struct mg_connection *, const char *format, ...); 105 | size_t mg_write(struct mg_connection *, const void *buf, int len); 106 | size_t mg_printf(struct mg_connection *conn, const char *fmt, ...); 107 | 108 | size_t mg_websocket_write(struct mg_connection *, int opcode, 109 | const char *data, size_t data_len); 110 | size_t mg_websocket_printf(struct mg_connection* conn, int opcode, 111 | const char *fmt, ...); 112 | 113 | void mg_send_file(struct mg_connection *, const char *path, const char *); 114 | void mg_send_file_data(struct mg_connection *, int fd); 115 | 116 | const char *mg_get_header(const struct mg_connection *, const char *name); 117 | const char *mg_get_mime_type(const char *name, const char *default_mime_type); 118 | int mg_get_var(const struct mg_connection *conn, const char *var_name, 119 | char *buf, size_t buf_len); 120 | int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); 121 | int mg_parse_multipart(const char *buf, int buf_len, 122 | char *var_name, int var_name_len, 123 | char *file_name, int file_name_len, 124 | const char **data, int *data_len); 125 | 126 | 127 | // Utility functions 128 | void *mg_start_thread(void *(*func)(void *), void *param); 129 | char *mg_md5(char buf[33], ...); 130 | int mg_authorize_digest(struct mg_connection *c, FILE *fp); 131 | int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len); 132 | int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int); 133 | int mg_terminate_ssl(struct mg_connection *c, const char *cert); 134 | int mg_forward(struct mg_connection *c, const char *addr); 135 | void *mg_mmap(FILE *fp, size_t size); 136 | void mg_munmap(void *p, size_t size); 137 | 138 | 139 | // Templates support 140 | struct mg_expansion { 141 | const char *keyword; 142 | void (*handler)(struct mg_connection *); 143 | }; 144 | void mg_template(struct mg_connection *, const char *text, 145 | struct mg_expansion *expansions); 146 | 147 | #ifdef __cplusplus 148 | } 149 | #endif // __cplusplus 150 | 151 | #endif // MONGOOSE_HEADER_INCLUDED 152 | -------------------------------------------------------------------------------- /sketchy_driver/main.c: -------------------------------------------------------------------------------- 1 | //user: rpi - pw: linuxcnc ip: 192.168.0.102 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define NANOSVG_IMPLEMENTATION // Expands implementation 8 | #include "nanosvg/nanosvg.h" 9 | 10 | #include "bool.h" 11 | #include "sketchy.h" 12 | #include "Preview.h" 13 | #include "Config.h" 14 | #include "machine-settings.h" 15 | #include "Model.h" 16 | 17 | #include "lua-5.2.3/lua.h" 18 | #include "lua-5.2.3/lauxlib.h" 19 | #include "lua-5.2.3/lualib.h" 20 | 21 | #include "sketchy-ipc.h" 22 | 23 | 24 | Point *POINT; 25 | 26 | int __moveTo(lua_State *L){ 27 | 28 | DriverCommand *cmd = getCommand(); 29 | if(cmd->commandCode == commandCodeStop || cmd->commandCode == commandCodePreviewAbort){ 30 | Model_setPenMode(penModeManualUp); 31 | return lua_yield (L, 0); 32 | } 33 | 34 | float x = lua_tonumber(L, 1); 35 | float y = lua_tonumber(L, 2); 36 | Point_updateWithXY(POINT,x,y); 37 | Model_moveTo(POINT); 38 | 39 | return 0; 40 | } 41 | 42 | int __penUp(lua_State *L){ 43 | Model_setPenMode(penModeManualUp); 44 | return 0; 45 | } 46 | 47 | int __penDown(lua_State *L){ 48 | Model_setPenMode(penModeManualDown); 49 | return 0; 50 | } 51 | 52 | int __canvasSize(lua_State *L){ 53 | int w = Config_canvasWidth(); 54 | int h = Config_canvasHeight(); 55 | lua_pushnumber(L, w); 56 | lua_pushnumber(L, h); 57 | return 2; 58 | } 59 | 60 | void loadLua(){ 61 | 62 | lua_State *L; 63 | 64 | L = luaL_newstate(); 65 | luaL_openlibs(L); 66 | lua_register(L,"moveTo",__moveTo); 67 | lua_register(L,"penUp",__penUp); 68 | lua_register(L,"penDown",__penDown); 69 | lua_register(L,"canvasSize",__canvasSize); 70 | 71 | if (luaL_loadfile(L, Config_getScriptName())){ 72 | printf("luaL_loadfile() failed scriptname: %s\n",Config_getScriptName()); 73 | } 74 | 75 | if (lua_pcall(L, 0, 0, 0)){ 76 | printf("lua_pcall() failed\n"); 77 | } 78 | 79 | lua_close(L); 80 | } 81 | 82 | void runLuaScript(){ 83 | POINT = Point_allocWithSteps(0 ,0); 84 | loadLua(); 85 | Model_moveHome(); 86 | Model_finish(); 87 | Point_release(POINT); 88 | } 89 | 90 | //SVG handling 91 | static float distPtSeg(float x, float y, float px, float py, float qx, float qy) 92 | { 93 | float pqx, pqy, dx, dy, d, t; 94 | pqx = qx-px; 95 | pqy = qy-py; 96 | dx = x-px; 97 | dy = y-py; 98 | d = pqx*pqx + pqy*pqy; 99 | t = pqx*dx + pqy*dy; 100 | if (d > 0) t /= d; 101 | if (t < 0) t = 0; 102 | else if (t > 1) t = 1; 103 | dx = px + t*pqx - x; 104 | dy = py + t*pqy - y; 105 | return dx*dx + dy*dy; 106 | } 107 | 108 | static void cubicBez(float x1, float y1, float x2, float y2, 109 | float x3, float y3, float x4, float y4, 110 | float tol, int level) 111 | { 112 | float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; 113 | float d; 114 | 115 | if (level > 12) return; 116 | 117 | x12 = (x1+x2)*0.5f; 118 | y12 = (y1+y2)*0.5f; 119 | x23 = (x2+x3)*0.5f; 120 | y23 = (y2+y3)*0.5f; 121 | x34 = (x3+x4)*0.5f; 122 | y34 = (y3+y4)*0.5f; 123 | x123 = (x12+x23)*0.5f; 124 | y123 = (y12+y23)*0.5f; 125 | x234 = (x23+x34)*0.5f; 126 | y234 = (y23+y34)*0.5f; 127 | x1234 = (x123+x234)*0.5f; 128 | y1234 = (y123+y234)*0.5f; 129 | 130 | d = distPtSeg(x1234, y1234, x1,y1, x4,y4); 131 | if (d > tol*tol) { 132 | cubicBez(x1,y1, x12,y12, x123,y123, x1234,y1234, tol, level+1); 133 | cubicBez(x1234,y1234, x234,y234, x34,y34, x4,y4, tol, level+1); 134 | } else { 135 | Point_updateWithXY(POINT,x4,y4); 136 | Model_moveTo(POINT); 137 | } 138 | } 139 | 140 | int drawPath(float* pts, int npts, char closed, float tol) 141 | { 142 | 143 | if(pts[0] > MAX_CANVAS_SIZE_X || pts[1] > MAX_CANVAS_SIZE_Y || pts[0] < 0 || pts[1] < 0 ){ 144 | updateDriverState(driverStateOutOfBoundsError,"","OUT_OF_BOUNDS_ERROR"); 145 | return -1; 146 | } 147 | 148 | Point_updateWithXY(POINT,pts[0],pts[1]); 149 | Model_moveTo(POINT); 150 | Model_setPenMode(penModeManualDown); 151 | 152 | int i; 153 | for (i = 0; i < npts-1; i += 3) { 154 | DriverCommand *cmd = getCommand(); 155 | if(cmd->commandCode == commandCodeStop || cmd->commandCode == commandCodePreviewAbort){ 156 | return -1; 157 | } 158 | float* p = &pts[i*2]; 159 | cubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7], tol, 0); 160 | } 161 | 162 | if (closed) { 163 | Point_updateWithXY(POINT,pts[0],pts[1]); 164 | Model_moveTo(POINT); 165 | } 166 | 167 | Model_setPenMode(penModeManualUp); 168 | 169 | return 0; 170 | } 171 | 172 | void runSVG(){ 173 | 174 | POINT = Point_allocWithSteps(0 ,0); 175 | 176 | Model_setPenMode(penModeManualUp); 177 | 178 | float px = 0.1; 179 | NSVGshape* shape; 180 | NSVGpath* path; 181 | 182 | struct NSVGimage* image; 183 | image = nsvgParseFromFile(Config_getSVGName(), "px", 96); 184 | printf("svg path %s\n",Config_getSVGName()); 185 | printf("svg size: %f x %f\n", image->width, image->height); 186 | 187 | if(image->shapes == NULL){ 188 | updateDriverState(driverStateNoDataFoundInSVGError,"","NO_DATA_IN_SVG_ERROR"); 189 | } 190 | 191 | for (shape = image->shapes; shape != NULL; shape = shape->next) { 192 | for (path = shape->paths; path != NULL; path = path->next) { 193 | int status = drawPath(path->pts, path->npts, path->closed, px * 1.5f); 194 | if(status == -1){ 195 | nsvgDelete(image); 196 | Model_setPenMode(penModeManualUp); 197 | Model_moveHome(); 198 | Model_finish(); 199 | Point_release(POINT); 200 | return; 201 | } 202 | } 203 | } 204 | 205 | nsvgDelete(image); 206 | Model_moveHome(); 207 | Model_finish(); 208 | Point_release(POINT); 209 | 210 | } 211 | 212 | 213 | int main(int argc, char *argv[]){ 214 | 215 | char *inifile = "config/default.ini"; 216 | if(argc == 2){ 217 | inifile = argv[1]; 218 | } 219 | 220 | if (Config_setIniBasePath(inifile) == -1){ 221 | printf("%s inifile name to long.\n", inifile); 222 | return 1; 223 | } 224 | 225 | shmCreate(); 226 | 227 | // 1) load the config 228 | Config_load(inifile); 229 | updateDriverState(driverSatusCodeBusy,inifile,"DRIVER_STATE_BUSSY"); 230 | 231 | int status; 232 | if(Config_getSVGName()){ 233 | // 2) if we have an svg for the motion 234 | status = run(runSVG); 235 | }else{ 236 | // 3) else run the lua script (motion control) 237 | status = run(runLuaScript); 238 | } 239 | 240 | DriverState *state = driverState(); 241 | if(state->statusCode < 3){ 242 | // 4) tell the server we are done if we dont have errors. 243 | setCommand("none",commandCodeNone,0.0,0); 244 | updateDriverState(driverSatusCodeIdle,"","DRIVER_STATE_IDLE"); 245 | }else{ 246 | printf("ERROR code: %i , %s\n",state->statusCode, state->name); 247 | } 248 | return status; 249 | 250 | } 251 | 252 | 253 | 254 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/Makefile_cross: -------------------------------------------------------------------------------- 1 | # Makefile for building Lua 2 | # See ../doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= none 8 | 9 | CC = arm-linux-gnueabihf-gcc 10 | #CROSS_COMPILER = arm-linux-gnueabihf- 11 | CC_CFLAGS = -I/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include 12 | CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(CC_CFLAGS) $(SYSCFLAGS) $(MYCFLAGS) 13 | LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) 14 | LIBS= -lm $(SYSLIBS) $(MYLIBS) 15 | 16 | AR= arm-linux-gnueabihf-ar rcu 17 | RANLIB= arm-linux-gnueabihf-ranlib 18 | RM= rm -f 19 | 20 | SYSCFLAGS= 21 | SYSLDFLAGS= 22 | SYSLIBS= 23 | 24 | MYCFLAGS= 25 | MYLDFLAGS= 26 | MYLIBS= 27 | MYOBJS= 28 | 29 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 30 | 31 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 32 | 33 | LUA_A= liblua.a 34 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ 35 | lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ 36 | ltm.o lundump.o lvm.o lzio.o 37 | LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \ 38 | lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o 39 | BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) 40 | 41 | LUA_T= lua 42 | LUA_O= lua.o 43 | 44 | LUAC_T= luac 45 | LUAC_O= luac.o 46 | 47 | ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) 48 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 49 | ALL_A= $(LUA_A) 50 | 51 | # Targets start here. 52 | default: $(PLAT) 53 | 54 | all: $(ALL_T) 55 | 56 | o: $(ALL_O) 57 | 58 | a: $(ALL_A) 59 | 60 | $(LUA_A): $(BASE_O) 61 | $(AR) $@ $(BASE_O) 62 | $(RANLIB) $@ 63 | 64 | $(LUA_T): $(LUA_O) $(LUA_A) 65 | $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 66 | 67 | $(LUAC_T): $(LUAC_O) $(LUA_A) 68 | $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 69 | 70 | clean: 71 | $(RM) $(ALL_T) $(ALL_O) 72 | 73 | depend: 74 | @$(CC) $(CFLAGS) -MM l*.c 75 | 76 | echo: 77 | @echo "PLAT= $(PLAT)" 78 | @echo "CC= $(CC)" 79 | @echo "CFLAGS= $(CFLAGS)" 80 | @echo "LDFLAGS= $(SYSLDFLAGS)" 81 | @echo "LIBS= $(LIBS)" 82 | @echo "AR= $(AR)" 83 | @echo "RANLIB= $(RANLIB)" 84 | @echo "RM= $(RM)" 85 | 86 | # Convenience targets for popular platforms 87 | ALL= all 88 | 89 | none: 90 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" CC=$(CC) CFLAGS="$(CFLAGS)" 91 | 92 | aix: 93 | $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" 94 | 95 | ansi: 96 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI" 97 | 98 | bsd: 99 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" 100 | 101 | freebsd: 102 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" 103 | 104 | generic: $(ALL) 105 | 106 | linux: 107 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" CC=$(CC) CFLAGS="$(CFLAGS)" 108 | 109 | macosx: 110 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" CC=cc 111 | 112 | mingw: 113 | $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ 114 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 115 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe 116 | $(MAKE) "LUAC_T=luac.exe" luac.exe 117 | 118 | posix: 119 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" 120 | 121 | solaris: 122 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" 123 | 124 | # list targets that do not create files (but not all makes understand .PHONY) 125 | .PHONY: all $(PLATS) default o a clean depend echo none 126 | 127 | # DO NOT DELETE 128 | 129 | lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 130 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \ 131 | lvm.h 132 | lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h 133 | lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h 134 | lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h 135 | lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 136 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ 137 | lstring.h ltable.h lvm.h 138 | lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h 139 | lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h 140 | ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h 141 | ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 142 | ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \ 143 | lfunc.h lstring.h lgc.h ltable.h lvm.h 144 | ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 145 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \ 146 | lstring.h ltable.h lundump.h lvm.h 147 | ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ 148 | lzio.h lmem.h lundump.h 149 | lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \ 150 | lstate.h ltm.h lzio.h lmem.h 151 | lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 152 | lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 153 | linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h 154 | liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h 155 | llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \ 156 | lstate.h ltm.h lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h 157 | lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h 158 | lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 159 | ltm.h lzio.h lmem.h ldo.h lgc.h 160 | loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h 161 | lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \ 162 | lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h 163 | lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h 164 | loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h 165 | lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 166 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \ 167 | lstring.h lgc.h ltable.h 168 | lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 169 | ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \ 170 | ltable.h 171 | lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ 172 | ltm.h lzio.h lstring.h lgc.h 173 | lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h 174 | ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 175 | ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h 176 | ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h 177 | ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ 178 | lmem.h lstring.h lgc.h ltable.h 179 | lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h 180 | luac.o: luac.c lua.h luaconf.h lauxlib.h lobject.h llimits.h lstate.h \ 181 | ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h 182 | lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ 183 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h 184 | lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 185 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h 186 | lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ 187 | lzio.h 188 | 189 | -------------------------------------------------------------------------------- /sketchy_driver/SpeedManager.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "FSObject.h" 6 | #include "SpeedManager.h" 7 | #include "machine-settings.h" 8 | #include "Config.h" 9 | #include "sketchy-ipc.h" 10 | #include "bool.h" 11 | #include "sketchy.h" 12 | 13 | static bool pausingInitialized = false; 14 | static int easeOutDelay = 0; 15 | 16 | SpeedManager *SpeedManager_alloc() 17 | { 18 | SpeedManager *sm = (SpeedManager *) malloc(sizeof(SpeedManager)); 19 | 20 | Point *home = Point_allocWithSteps(0 ,0); 21 | 22 | sm->top = (PathSegment *) malloc(sizeof(PathSegment)); 23 | sm->top->direction = 0.0; 24 | sm->top->next = NULL; 25 | sm->top->x = home->x; 26 | sm->top->y = home->y; 27 | 28 | sm->bottom = sm->top; 29 | sm->queueLength = 0; 30 | sm->length = Config_getLookaheadMM(); 31 | sm->retainCount = 1; 32 | sm->type = "SpeedManager"; 33 | sm->max = 0; 34 | sm->currentX = home->x; 35 | sm->currentY = home->y; 36 | sm->currentDirection = 0.0; 37 | sm->delay = Config_maxDelay(); 38 | sm->targetDelay = Config_maxDelay(); 39 | sm->delayPerDegree = (sm->delay - Config_minDelay()) / 180.0; 40 | sm->delayPerDegreeMove = (sm->delay - Config_minMoveDelay()) / 180.0; 41 | sm->delayStepDraw = fabs((Config_maxDelay() - Config_minDelay()) / sm->length); 42 | sm->delayStepMove = fabs((Config_maxDelay() - Config_minMoveDelay()) / sm->length); 43 | sm->delayStep = sm->delayStepDraw; 44 | sm->usePenChangeInLookAhead = Config_usePenChangeInLookAhead(); 45 | 46 | Point_release(home); 47 | 48 | return sm; 49 | } 50 | 51 | void SpeedManager_resume(SpeedManager *sm){ 52 | Config_reload(); 53 | sm->length = Config_getLookaheadMM(); 54 | sm->delayPerDegree = (Config_maxDelay() - Config_minDelay()) / 180.0; 55 | sm->delayPerDegreeMove = (Config_maxDelay() - Config_minMoveDelay()) / 180.0; 56 | sm->delayStepDraw = fabs((Config_maxDelay() - Config_minDelay()) / sm->length); 57 | sm->delayStepMove = fabs((Config_maxDelay() - Config_minMoveDelay()) / sm->length); 58 | sm->delayStep = sm->delayStepDraw; 59 | sm->usePenChangeInLookAhead = Config_usePenChangeInLookAhead(); 60 | SpeedManager_reduceQueue(sm); 61 | } 62 | 63 | void SpeedManager_copmuteDelay(SpeedManager *sm){ 64 | if(fabs(sm->targetDelay - sm->delay) < sm->delayStep){ 65 | sm->delay = sm->targetDelay; 66 | }else if(sm->targetDelay > sm->delay){ 67 | sm->delay += sm->delayStep; 68 | }else if(sm->targetDelay < sm->delay){ 69 | sm->delay -= sm->delayStep; 70 | } 71 | } 72 | 73 | void SpeedManager_log(SpeedManager *sm){ 74 | if(sm->bottom == sm->top){ 75 | printf("SpeedManager state EMPTY \n"); 76 | return; 77 | } 78 | int i=0; 79 | PathSegment *curr = sm->bottom; 80 | while(curr){ 81 | printf("%i -> x%f y:%f -> direction: %f \n",i,curr->x,curr->y,curr->direction); 82 | curr = curr->next; 83 | i++; 84 | } 85 | printf("- - - - - - \n"); 86 | printf("MAX %f \n",sm->max); 87 | printf("- - - - - - \n"); 88 | } 89 | 90 | void SpeedManager_compute(SpeedManager *sm){ 91 | 92 | float max = 0; 93 | PathSegment *curr = sm->bottom; 94 | int penChangeAhead = 0; 95 | int penUpAhead = 0; 96 | int penDownAhead = 0; 97 | int solenoidState = curr->solenoidState; 98 | 99 | while(curr){ 100 | if(solenoidState != curr->solenoidState){ 101 | penChangeAhead = 1; 102 | if(solenoidState == 0){ 103 | penUpAhead = 0; 104 | penDownAhead = 1; 105 | sm->delayStep = sm->delayStepMove; 106 | }else{ 107 | penUpAhead = 1; 108 | penDownAhead = 0; 109 | sm->delayStep = sm->delayStepDraw; 110 | } 111 | } 112 | if(fabs(curr->direction) > max){ 113 | max = fabs(curr->direction); 114 | } 115 | curr = curr->next; 116 | } 117 | 118 | if(penChangeAhead && sm->usePenChangeInLookAhead){ 119 | sm->targetDelay = Config_maxDelay(); 120 | }else if(max != sm->max){ 121 | sm->max = max; 122 | if(solenoidState == 0){ 123 | sm->targetDelay = Config_minMoveDelay() + sm->max * sm->delayPerDegreeMove; 124 | }else{ 125 | sm->targetDelay = Config_minDelay() + sm->max * sm->delayPerDegree; 126 | } 127 | } 128 | } 129 | 130 | void SpeedManager_append(SpeedManager *sm,float x,float y,int penMode,int solenoidState){ 131 | 132 | float dir = atan2(sm->currentY - y, sm->currentX - x); 133 | 134 | PathSegment *seg = (PathSegment *) malloc(sizeof(PathSegment)); 135 | float tmp = fabs((dir - sm->currentDirection) * DEG); 136 | if(tmp > 180){ 137 | tmp = tmp - 360.0; 138 | } 139 | seg->direction = tmp; 140 | seg->next = NULL; 141 | seg->x = x; 142 | seg->y = y; 143 | seg->penMode = penMode; 144 | seg->solenoidState = solenoidState; 145 | 146 | sm->top->next = seg; 147 | sm->top = seg; 148 | 149 | sm->currentDirection = dir; 150 | sm->currentX = x; 151 | sm->currentY = y; 152 | 153 | if(sm->queueLength >= sm->length-1){ 154 | 155 | DriverCommand *cmd = getCommand(); 156 | 157 | SpeedManager_copmuteDelay(sm); 158 | int computedDelay = sm->delay; 159 | 160 | if(cmd->commandCode == commandCodePause){ 161 | if(!pausingInitialized){ 162 | easeOutDelay = sm->delay; 163 | pausingInitialized = true; 164 | } 165 | easeOutDelay += 5000; 166 | computedDelay = easeOutDelay; 167 | if(easeOutDelay > Config_maxDelay()){ 168 | sm->delay = easeOutDelay; 169 | sketchy_suspend(); 170 | } 171 | }else{ 172 | pausingInitialized = false; 173 | easeOutDelay = 0; 174 | } 175 | 176 | sm->executeCallback(sm->bottom->x,sm->bottom->y,computedDelay,sm->queueLength,sm->bottom->penMode); 177 | PathSegment *newBottom = sm->bottom->next; 178 | free(sm->bottom); 179 | sm->bottom = newBottom; 180 | SpeedManager_compute(sm); 181 | }else{ 182 | sm->queueLength ++; 183 | } 184 | 185 | 186 | 187 | } 188 | 189 | void SpeedManager_setCallback(SpeedManager *sm,void (*executeCallback)(float x,float y, int delay,int cursor,int penMode)){ 190 | sm->executeCallback = executeCallback; 191 | } 192 | 193 | void SpeedManager_release(SpeedManager *sm){ 194 | sm->retainCount --; 195 | if(sm->retainCount == 0){ 196 | PathSegment *p; 197 | PathSegment *curr = sm->bottom; 198 | while(curr){ 199 | p = curr->next; 200 | free(curr); 201 | curr = p; 202 | } 203 | free(sm); 204 | } 205 | } 206 | 207 | void SpeedManager_reduceQueue(SpeedManager *sm){ 208 | PathSegment *curr = sm->bottom; 209 | while(sm->queueLength > sm->length-1){ 210 | SpeedManager_copmuteDelay(sm); 211 | int computedDelay = sm->delay; 212 | sm->executeCallback(sm->bottom->x,sm->bottom->y,computedDelay,sm->queueLength,sm->bottom->penMode); 213 | PathSegment *newBottom = sm->bottom->next; 214 | free(sm->bottom); 215 | sm->bottom = newBottom; 216 | SpeedManager_compute(sm); 217 | sm->queueLength --; 218 | } 219 | } 220 | 221 | void SpeedManager_finish(SpeedManager *sm){ 222 | PathSegment *curr = sm->bottom; 223 | while(curr){ 224 | SpeedManager_copmuteDelay(sm); 225 | sm->executeCallback(curr->x,curr->y,sm->delay,sm->queueLength,curr->penMode); 226 | curr = curr->next; 227 | } 228 | 229 | #ifdef __PI__ 230 | alarm(1); 231 | #endif 232 | 233 | } 234 | 235 | void SpeedManager_retain(SpeedManager *sm){ 236 | FSObject_retain(sm); 237 | } 238 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Standard mathematical library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lmathlib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | #undef PI 21 | #define PI ((lua_Number)(3.1415926535897932384626433832795)) 22 | #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), 78 | luaL_checknumber(L, 2))); 79 | return 1; 80 | } 81 | 82 | static int math_ceil (lua_State *L) { 83 | lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); 84 | return 1; 85 | } 86 | 87 | static int math_floor (lua_State *L) { 88 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); 89 | return 1; 90 | } 91 | 92 | static int math_fmod (lua_State *L) { 93 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), 94 | luaL_checknumber(L, 2))); 95 | return 1; 96 | } 97 | 98 | static int math_modf (lua_State *L) { 99 | lua_Number ip; 100 | lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); 101 | lua_pushnumber(L, ip); 102 | lua_pushnumber(L, fp); 103 | return 2; 104 | } 105 | 106 | static int math_sqrt (lua_State *L) { 107 | lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); 108 | return 1; 109 | } 110 | 111 | static int math_pow (lua_State *L) { 112 | lua_Number x = luaL_checknumber(L, 1); 113 | lua_Number y = luaL_checknumber(L, 2); 114 | lua_pushnumber(L, l_mathop(pow)(x, y)); 115 | return 1; 116 | } 117 | 118 | static int math_log (lua_State *L) { 119 | lua_Number x = luaL_checknumber(L, 1); 120 | lua_Number res; 121 | if (lua_isnoneornil(L, 2)) 122 | res = l_mathop(log)(x); 123 | else { 124 | lua_Number base = luaL_checknumber(L, 2); 125 | if (base == (lua_Number)10.0) res = l_mathop(log10)(x); 126 | else res = l_mathop(log)(x)/l_mathop(log)(base); 127 | } 128 | lua_pushnumber(L, res); 129 | return 1; 130 | } 131 | 132 | #if defined(LUA_COMPAT_LOG10) 133 | static int math_log10 (lua_State *L) { 134 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); 135 | return 1; 136 | } 137 | #endif 138 | 139 | static int math_exp (lua_State *L) { 140 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); 141 | return 1; 142 | } 143 | 144 | static int math_deg (lua_State *L) { 145 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 146 | return 1; 147 | } 148 | 149 | static int math_rad (lua_State *L) { 150 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 151 | return 1; 152 | } 153 | 154 | static int math_frexp (lua_State *L) { 155 | int e; 156 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); 157 | lua_pushinteger(L, e); 158 | return 2; 159 | } 160 | 161 | static int math_ldexp (lua_State *L) { 162 | lua_Number x = luaL_checknumber(L, 1); 163 | int ep = luaL_checkint(L, 2); 164 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); 165 | return 1; 166 | } 167 | 168 | 169 | 170 | static int math_min (lua_State *L) { 171 | int n = lua_gettop(L); /* number of arguments */ 172 | lua_Number dmin = luaL_checknumber(L, 1); 173 | int i; 174 | for (i=2; i<=n; i++) { 175 | lua_Number d = luaL_checknumber(L, i); 176 | if (d < dmin) 177 | dmin = d; 178 | } 179 | lua_pushnumber(L, dmin); 180 | return 1; 181 | } 182 | 183 | 184 | static int math_max (lua_State *L) { 185 | int n = lua_gettop(L); /* number of arguments */ 186 | lua_Number dmax = luaL_checknumber(L, 1); 187 | int i; 188 | for (i=2; i<=n; i++) { 189 | lua_Number d = luaL_checknumber(L, i); 190 | if (d > dmax) 191 | dmax = d; 192 | } 193 | lua_pushnumber(L, dmax); 194 | return 1; 195 | } 196 | 197 | 198 | static int math_random (lua_State *L) { 199 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 200 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 201 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 202 | switch (lua_gettop(L)) { /* check number of arguments */ 203 | case 0: { /* no arguments */ 204 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 205 | break; 206 | } 207 | case 1: { /* only upper limit */ 208 | lua_Number u = luaL_checknumber(L, 1); 209 | luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); 210 | lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ 211 | break; 212 | } 213 | case 2: { /* lower and upper limits */ 214 | lua_Number l = luaL_checknumber(L, 1); 215 | lua_Number u = luaL_checknumber(L, 2); 216 | luaL_argcheck(L, l <= u, 2, "interval is empty"); 217 | lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ 218 | break; 219 | } 220 | default: return luaL_error(L, "wrong number of arguments"); 221 | } 222 | return 1; 223 | } 224 | 225 | 226 | static int math_randomseed (lua_State *L) { 227 | srand(luaL_checkunsigned(L, 1)); 228 | (void)rand(); /* discard first value to avoid undesirable correlations */ 229 | return 0; 230 | } 231 | 232 | 233 | static const luaL_Reg mathlib[] = { 234 | {"abs", math_abs}, 235 | {"acos", math_acos}, 236 | {"asin", math_asin}, 237 | {"atan2", math_atan2}, 238 | {"atan", math_atan}, 239 | {"ceil", math_ceil}, 240 | {"cosh", math_cosh}, 241 | {"cos", math_cos}, 242 | {"deg", math_deg}, 243 | {"exp", math_exp}, 244 | {"floor", math_floor}, 245 | {"fmod", math_fmod}, 246 | {"frexp", math_frexp}, 247 | {"ldexp", math_ldexp}, 248 | #if defined(LUA_COMPAT_LOG10) 249 | {"log10", math_log10}, 250 | #endif 251 | {"log", math_log}, 252 | {"max", math_max}, 253 | {"min", math_min}, 254 | {"modf", math_modf}, 255 | {"pow", math_pow}, 256 | {"rad", math_rad}, 257 | {"random", math_random}, 258 | {"randomseed", math_randomseed}, 259 | {"sinh", math_sinh}, 260 | {"sin", math_sin}, 261 | {"sqrt", math_sqrt}, 262 | {"tanh", math_tanh}, 263 | {"tan", math_tan}, 264 | {NULL, NULL} 265 | }; 266 | 267 | 268 | /* 269 | ** Open math library 270 | */ 271 | LUAMOD_API int luaopen_math (lua_State *L) { 272 | luaL_newlib(L, mathlib); 273 | lua_pushnumber(L, PI); 274 | lua_setfield(L, -2, "pi"); 275 | lua_pushnumber(L, HUGE_VAL); 276 | lua_setfield(L, -2, "huge"); 277 | return 1; 278 | } 279 | 280 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for `luaL_load' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | typedef struct luaL_Reg { 24 | const char *name; 25 | lua_CFunction func; 26 | } luaL_Reg; 27 | 28 | 29 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 30 | #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 31 | 32 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 33 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 34 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 35 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 36 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 37 | size_t *l); 38 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 39 | const char *def, size_t *l); 40 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 41 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 42 | 43 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 44 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 45 | lua_Integer def); 46 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); 47 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, 48 | lua_Unsigned def); 49 | 50 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 52 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 53 | 54 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 | 59 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 | 62 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 63 | const char *const lst[]); 64 | 65 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 | 68 | /* pre-defined references */ 69 | #define LUA_NOREF (-2) 70 | #define LUA_REFNIL (-1) 71 | 72 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 | 75 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 | const char *mode); 77 | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 | const char *name, const char *mode); 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 | 84 | LUALIB_API lua_State *(luaL_newstate) (void); 85 | 86 | LUALIB_API int (luaL_len) (lua_State *L, int idx); 87 | 88 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 | const char *r); 90 | 91 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 | 93 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 | 95 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 | const char *msg, int level); 97 | 98 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 | lua_CFunction openf, int glb); 100 | 101 | /* 102 | ** =============================================================== 103 | ** some useful macros 104 | ** =============================================================== 105 | */ 106 | 107 | 108 | #define luaL_newlibtable(L,l) \ 109 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 | 111 | #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 112 | 113 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 114 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 115 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 116 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 117 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 118 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 119 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 120 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 121 | 122 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 123 | 124 | #define luaL_dofile(L, fn) \ 125 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 | 127 | #define luaL_dostring(L, s) \ 128 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 129 | 130 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 131 | 132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 133 | 134 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 135 | 136 | 137 | /* 138 | ** {====================================================== 139 | ** Generic Buffer manipulation 140 | ** ======================================================= 141 | */ 142 | 143 | typedef struct luaL_Buffer { 144 | char *b; /* buffer address */ 145 | size_t size; /* buffer size */ 146 | size_t n; /* number of characters in buffer */ 147 | lua_State *L; 148 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 149 | } luaL_Buffer; 150 | 151 | 152 | #define luaL_addchar(B,c) \ 153 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 154 | ((B)->b[(B)->n++] = (c))) 155 | 156 | #define luaL_addsize(B,s) ((B)->n += (s)) 157 | 158 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 159 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 160 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 161 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 162 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 163 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 164 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 166 | 167 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 168 | 169 | /* }====================================================== */ 170 | 171 | 172 | 173 | /* 174 | ** {====================================================== 175 | ** File handles for IO library 176 | ** ======================================================= 177 | */ 178 | 179 | /* 180 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 181 | ** initial structure 'luaL_Stream' (it may contain other fields 182 | ** after that initial structure). 183 | */ 184 | 185 | #define LUA_FILEHANDLE "FILE*" 186 | 187 | 188 | typedef struct luaL_Stream { 189 | FILE *f; /* stream (NULL for incompletely created streams) */ 190 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 191 | } luaL_Stream; 192 | 193 | /* }====================================================== */ 194 | 195 | 196 | 197 | /* compatibility with old module system */ 198 | #if defined(LUA_COMPAT_MODULE) 199 | 200 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 201 | int sizehint); 202 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 203 | const luaL_Reg *l, int nup); 204 | 205 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 206 | 207 | #endif 208 | 209 | 210 | #endif 211 | 212 | 213 | -------------------------------------------------------------------------------- /sketchy_driver/sketchy.c: -------------------------------------------------------------------------------- 1 | //user: rpi - pw: linuxcnc ip: 192.168.0.102 2 | #include 3 | #include 4 | #include 5 | #include "sketchy.h" 6 | #include "Model.h" 7 | #include "bool.h" 8 | #include "Config.h" 9 | #include "sketchy-ipc.h" 10 | #include "machine-settings.h" 11 | 12 | #ifdef __PI__ 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #endif 20 | 21 | #ifndef __PI__ 22 | #include "Preview.h" 23 | #endif 24 | 25 | #ifdef __PI__ 26 | RT_TASK draw_task; 27 | RT_TASK watchdog_task; 28 | #endif 29 | 30 | #define RIGHT_CLOCK RPI_V2_GPIO_P1_11 31 | #define RIGHT_DIR RPI_V2_GPIO_P1_12 32 | #define LEFT_CLOCK RPI_V2_GPIO_P1_13 33 | #define LEFT_DIR RPI_V2_GPIO_P1_15 34 | #define SOLENOID RPI_V2_GPIO_P1_16 35 | 36 | StepperMotorDir stepleft = stepperMotorDirNone; 37 | StepperMotorDir stepright = stepperMotorDirNone; 38 | 39 | StepperMotorDir leftdir = stepperMotorDirNone; 40 | StepperMotorDir rightdir = stepperMotorDirNone; 41 | 42 | SolenoidState solenoidstate = solenoidStateUp; 43 | SolenoidState solenoid = solenoidStateUp; 44 | 45 | #ifndef __PI__ 46 | Preview *PREVIEW; 47 | Preview *PREVIEW_PEN_MOVE; 48 | long stepCounter = 0; 49 | #endif 50 | 51 | const char *input_imagename; 52 | int input_threshold; 53 | 54 | static bool paused = false; 55 | 56 | void sketchy_suspend(){ 57 | 58 | if(!paused){ 59 | updateDriverState(driverStatusCodePaused,"",""); 60 | paused = true; 61 | printf("PAUSED\n"); 62 | 63 | #ifdef __PI__ 64 | rt_task_suspend(&draw_task); 65 | #else 66 | // on a non xenomai os this busy loop simulates rt_task_suspend 67 | while(1){ 68 | DriverCommand *cmd = getCommand(); 69 | if(cmd->commandCode != commandCodePause){ 70 | sketchy_resume(); 71 | return; 72 | } 73 | } 74 | 75 | #endif 76 | } 77 | } 78 | 79 | void sketchy_resume(){ 80 | 81 | if(paused){ 82 | Model_resume(); 83 | #ifndef __PI__ 84 | Preview_updateSpeed(PREVIEW, Config_maxDelay(), Config_minDelay()); 85 | #endif 86 | updateDriverState(driverSatusCodeBusy,"",""); 87 | paused = false; 88 | #ifdef __PI__ 89 | printf("-rt_task_resume-\n"); 90 | rt_task_resume(&draw_task); 91 | #else 92 | printf("RESUME\n"); 93 | #endif 94 | 95 | } 96 | } 97 | 98 | #ifdef __PI__ 99 | 100 | // On the raspberry PI / xenomai the watchdog rt_task 101 | // checks if the draw task needs to be resumed 102 | void watch(){ 103 | while(1){ 104 | rt_task_wait_period(NULL); 105 | if(paused){ 106 | DriverCommand *cmd = getCommand(); 107 | if(cmd->commandCode != commandCodePause){ 108 | sketchy_resume(); 109 | } 110 | } 111 | rt_task_set_periodic(&watchdog_task, TM_NOW, 500000); 112 | } 113 | } 114 | 115 | #endif 116 | 117 | void executeStep(Step *step){ 118 | 119 | Point *p = Point_allocWithSteps(BOT->leftsteps,BOT->rightsteps); 120 | bool shouldDraw = (BOT->penMode == penModeManualDown); 121 | 122 | #ifdef __PI__ 123 | 124 | rt_task_wait_period(NULL); 125 | 126 | stepleft = step->leftengine; 127 | stepright = step->rightengine; 128 | 129 | if(shouldDraw){ 130 | solenoid = solenoidStateDown; 131 | }else{ 132 | solenoid = solenoidStateUp; 133 | } 134 | 135 | if(stepleft != leftdir){ 136 | 137 | #ifdef __VPLOTTER__ 138 | 139 | if(stepleft == stepperMotorDirUp){ 140 | bcm2835_gpio_write(LEFT_DIR, HIGH); 141 | }else if(stepleft == stepperMotorDirDown){ 142 | bcm2835_gpio_write(LEFT_DIR, LOW); 143 | } 144 | 145 | #else 146 | //FOR MINI blackstripes with no gearboxes HIGH and LOW should be inverted for left only, NOT RIGHT!! 147 | //the gearboxes are mirrored to make the machine look better 148 | //so the direction signals have to be inverted 149 | if(stepleft == stepperMotorDirUp){ 150 | bcm2835_gpio_write(LEFT_DIR, LOW); 151 | }else if(stepleft == stepperMotorDirDown){ 152 | bcm2835_gpio_write(LEFT_DIR, HIGH); 153 | } 154 | 155 | #endif 156 | 157 | leftdir = stepleft; 158 | 159 | } 160 | 161 | if(stepright != rightdir){ 162 | 163 | if(stepright == stepperMotorDirUp){ 164 | bcm2835_gpio_write(RIGHT_DIR, LOW); 165 | }else if(stepright == stepperMotorDirDown){ 166 | bcm2835_gpio_write(RIGHT_DIR, HIGH); 167 | } 168 | 169 | rightdir = stepright; 170 | 171 | } 172 | 173 | if(solenoidstate != solenoid){ 174 | 175 | if(solenoid == solenoidStateUp){ 176 | bcm2835_gpio_write(SOLENOID, HIGH); 177 | }else if(solenoid == solenoidStateDown){ 178 | bcm2835_gpio_write(SOLENOID, LOW); 179 | } 180 | 181 | solenoidstate = solenoid; 182 | 183 | } 184 | 185 | // sync the stepper steps // 186 | if (stepleft != stepperMotorDirNone) { 187 | bcm2835_gpio_write(LEFT_CLOCK, HIGH); 188 | } 189 | if (stepright != stepperMotorDirNone) { 190 | bcm2835_gpio_write(RIGHT_CLOCK, HIGH); 191 | } 192 | 193 | rt_task_sleep(100); 194 | 195 | if (stepleft != stepperMotorDirNone) { 196 | bcm2835_gpio_write(LEFT_CLOCK, LOW); 197 | } 198 | if (stepright != stepperMotorDirNone) { 199 | bcm2835_gpio_write(RIGHT_CLOCK, LOW); 200 | } 201 | 202 | rt_task_set_periodic(&draw_task, TM_NOW, BOT->delay); 203 | 204 | #else 205 | 206 | int x = floor(p->x); 207 | int y = floor(p->y); 208 | Preview_setPixel(PREVIEW,x,y,BOT->delay, shouldDraw); 209 | Preview_setPixel(PREVIEW_PEN_MOVE,x,y,BOT->delay, !shouldDraw); 210 | stepCounter ++; 211 | if(stepCounter%10000 == 0){ 212 | Preview_save(PREVIEW); 213 | } 214 | 215 | #endif 216 | 217 | Point_release(p); 218 | 219 | } 220 | 221 | void catch_signal(int sig) 222 | { 223 | } 224 | 225 | int run(void (*executeMotion)()){ 226 | 227 | Model_createInstance(); 228 | Model_setExecuteStepCallback(executeStep); 229 | Model_logState(); 230 | 231 | #ifdef __PI__ 232 | 233 | signal(SIGTERM, catch_signal); 234 | signal(SIGINT, catch_signal); 235 | signal(SIGALRM, catch_signal); 236 | 237 | /* Avoids memory swapping for this program */ 238 | mlockall(MCL_CURRENT|MCL_FUTURE); 239 | 240 | if (!bcm2835_init()){ 241 | printf("error\n"); 242 | //return 1; 243 | } 244 | 245 | bcm2835_gpio_fsel(RIGHT_CLOCK, BCM2835_GPIO_FSEL_OUTP); 246 | bcm2835_gpio_fsel(RIGHT_DIR, BCM2835_GPIO_FSEL_OUTP); 247 | bcm2835_gpio_fsel(LEFT_CLOCK, BCM2835_GPIO_FSEL_OUTP); 248 | bcm2835_gpio_fsel(LEFT_DIR, BCM2835_GPIO_FSEL_OUTP); 249 | bcm2835_gpio_fsel(SOLENOID, BCM2835_GPIO_FSEL_OUTP); 250 | 251 | bcm2835_gpio_write(SOLENOID, HIGH); 252 | rt_task_set_periodic(&draw_task, TM_NOW, BOT->delay); 253 | rt_task_set_periodic(&watchdog_task, TM_NOW, 500000); 254 | 255 | /* 256 | * Arguments: &task, 257 | * name, 258 | * stack size (0=default), 259 | * priority, 260 | * mode (FPU, start suspended, ...) 261 | */ 262 | rt_task_create(&draw_task, "printerbot", 0, 99, 0); 263 | rt_task_create(&watchdog_task, "watchdog", 0, 99, 0); 264 | /* 265 | * Arguments: &task, 266 | * task function, 267 | * function argument 268 | */ 269 | rt_task_start(&draw_task, executeMotion, NULL); 270 | rt_task_start(&watchdog_task, &watch, NULL); 271 | 272 | pause(); 273 | 274 | rt_task_delete(&draw_task); 275 | rt_task_delete(&watchdog_task); 276 | 277 | bcm2835_gpio_write(SOLENOID, HIGH); 278 | 279 | 280 | #else 281 | 282 | PREVIEW = Preview_alloc((int)MAX_CANVAS_SIZE_X,(int)MAX_CANVAS_SIZE_Y,"preview_image.png",Config_maxDelay(),Config_minDelay()); 283 | PREVIEW_PEN_MOVE = Preview_alloc((int)MAX_CANVAS_SIZE_X,(int)MAX_CANVAS_SIZE_Y,"pen_move_image.png",Config_maxDelay(),Config_minMoveDelay()); 284 | 285 | report_memory(1); 286 | executeMotion(); 287 | Preview_save(PREVIEW); 288 | Preview_release(PREVIEW); 289 | Preview_save(PREVIEW_PEN_MOVE); 290 | Preview_release(PREVIEW_PEN_MOVE); 291 | 292 | #endif 293 | 294 | Model_release(); 295 | 296 | return 0; 297 | 298 | } 299 | 300 | 301 | 302 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | /* 18 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed. 21 | ** 22 | ** Lua keeps most objects linked in list g->allgc. The link uses field 23 | ** 'next' of the CommonHeader. 24 | ** 25 | ** Strings are kept in several lists headed by the array g->strt.hash. 26 | ** 27 | ** Open upvalues are not subject to independent garbage collection. They 28 | ** are collected together with their respective threads. Lua keeps a 29 | ** double-linked list with all open upvalues (g->uvhead) so that it can 30 | ** mark objects referred by them. (They are always gray, so they must 31 | ** be remarked in the atomic step. Usually their contents would be marked 32 | ** when traversing the respective threads, but the thread may already be 33 | ** dead, while the upvalue is still accessible through closures.) 34 | ** 35 | ** Objects with finalizers are kept in the list g->finobj. 36 | ** 37 | ** The list g->tobefnz links all objects being finalized. 38 | 39 | */ 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | 46 | /* extra stack space to handle TM calls and some other extras */ 47 | #define EXTRA_STACK 5 48 | 49 | 50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51 | 52 | 53 | /* kinds of Garbage Collection */ 54 | #define KGC_NORMAL 0 55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56 | #define KGC_GEN 2 /* generational collection */ 57 | 58 | 59 | typedef struct stringtable { 60 | GCObject **hash; 61 | lu_int32 nuse; /* number of elements */ 62 | int size; 63 | } stringtable; 64 | 65 | 66 | /* 67 | ** information about a call 68 | */ 69 | typedef struct CallInfo { 70 | StkId func; /* function index in the stack */ 71 | StkId top; /* top for this function */ 72 | struct CallInfo *previous, *next; /* dynamic call link */ 73 | short nresults; /* expected number of results from this function */ 74 | lu_byte callstatus; 75 | ptrdiff_t extra; 76 | union { 77 | struct { /* only for Lua functions */ 78 | StkId base; /* base for this function */ 79 | const Instruction *savedpc; 80 | } l; 81 | struct { /* only for C functions */ 82 | int ctx; /* context info. in case of yields */ 83 | lua_CFunction k; /* continuation in case of yields */ 84 | ptrdiff_t old_errfunc; 85 | lu_byte old_allowhook; 86 | lu_byte status; 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** Bits in CallInfo status 94 | */ 95 | #define CIST_LUA (1<<0) /* call is running a Lua function */ 96 | #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97 | #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98 | luaV_execute of previous call */ 99 | #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101 | #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102 | #define CIST_TAIL (1<<6) /* call was tail called */ 103 | #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104 | 105 | 106 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107 | 108 | 109 | /* 110 | ** `global state', shared by all threads of this state 111 | */ 112 | typedef struct global_State { 113 | lua_Alloc frealloc; /* function to reallocate memory */ 114 | void *ud; /* auxiliary data to `frealloc' */ 115 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117 | lu_mem GCmemtrav; /* memory traversed by the GC */ 118 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119 | stringtable strt; /* hash table for strings */ 120 | TValue l_registry; 121 | unsigned int seed; /* randomized seed for hashes */ 122 | lu_byte currentwhite; 123 | lu_byte gcstate; /* state of garbage collector */ 124 | lu_byte gckind; /* kind of GC running */ 125 | lu_byte gcrunning; /* true if GC is running */ 126 | int sweepstrgc; /* position of sweep in `strt' */ 127 | GCObject *allgc; /* list of all collectable objects */ 128 | GCObject *finobj; /* list of collectable objects with finalizers */ 129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130 | GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131 | GCObject *gray; /* list of gray objects */ 132 | GCObject *grayagain; /* list of objects to be traversed atomically */ 133 | GCObject *weak; /* list of tables with weak values */ 134 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135 | GCObject *allweak; /* list of all-weak tables */ 136 | GCObject *tobefnz; /* list of userdata to be GC */ 137 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 138 | Mbuffer buff; /* temporary buffer for string concatenation */ 139 | int gcpause; /* size of pause between successive GCs */ 140 | int gcmajorinc; /* pause between major collections (only in gen. mode) */ 141 | int gcstepmul; /* GC `granularity' */ 142 | lua_CFunction panic; /* to be called in unprotected errors */ 143 | struct lua_State *mainthread; 144 | const lua_Number *version; /* pointer to version number */ 145 | TString *memerrmsg; /* memory-error message */ 146 | TString *tmname[TM_N]; /* array with tag-method names */ 147 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148 | } global_State; 149 | 150 | 151 | /* 152 | ** `per thread' state 153 | */ 154 | struct lua_State { 155 | CommonHeader; 156 | lu_byte status; 157 | StkId top; /* first free slot in the stack */ 158 | global_State *l_G; 159 | CallInfo *ci; /* call info for current function */ 160 | const Instruction *oldpc; /* last pc traced */ 161 | StkId stack_last; /* last free slot in the stack */ 162 | StkId stack; /* stack base */ 163 | int stacksize; 164 | unsigned short nny; /* number of non-yieldable calls in stack */ 165 | unsigned short nCcalls; /* number of nested C calls */ 166 | lu_byte hookmask; 167 | lu_byte allowhook; 168 | int basehookcount; 169 | int hookcount; 170 | lua_Hook hook; 171 | GCObject *openupval; /* list of open upvalues in this stack */ 172 | GCObject *gclist; 173 | struct lua_longjmp *errorJmp; /* current error recover point */ 174 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 175 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 176 | }; 177 | 178 | 179 | #define G(L) (L->l_G) 180 | 181 | 182 | /* 183 | ** Union of all collectable objects 184 | */ 185 | union GCObject { 186 | GCheader gch; /* common header */ 187 | union TString ts; 188 | union Udata u; 189 | union Closure cl; 190 | struct Table h; 191 | struct Proto p; 192 | struct UpVal uv; 193 | struct lua_State th; /* thread */ 194 | }; 195 | 196 | 197 | #define gch(o) (&(o)->gch) 198 | 199 | /* macros to convert a GCObject into a specific value */ 200 | #define rawgco2ts(o) \ 201 | check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 202 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 203 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 204 | #define gco2u(o) (&rawgco2u(o)->uv) 205 | #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 206 | #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 207 | #define gco2cl(o) \ 208 | check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 209 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 210 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 211 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 212 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 213 | 214 | /* macro to convert any Lua object into a GCObject */ 215 | #define obj2gco(v) (cast(GCObject *, (v))) 216 | 217 | 218 | /* actual number of total bytes allocated */ 219 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 220 | 221 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 222 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 223 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 224 | LUAI_FUNC void luaE_freeCI (lua_State *L); 225 | 226 | 227 | #endif 228 | 229 | -------------------------------------------------------------------------------- /sketchy_driver/Model.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Model.h" 5 | #include "FSObject.h" 6 | #include "Point.h" 7 | #include "SpeedManager.h" 8 | #include "Step.h" 9 | #include "machine-settings.h" 10 | #include "Config.h" 11 | #include "sketchy-ipc.h" 12 | #include "bool.h" 13 | 14 | #ifdef __APPLE__ 15 | #include 16 | #endif 17 | 18 | void report_memory(int id) { 19 | 20 | #ifdef __APPLE__ 21 | 22 | struct task_basic_info info; 23 | mach_msg_type_number_t size = sizeof(info); 24 | 25 | kern_return_t kerr = task_info(mach_task_self(), 26 | TASK_BASIC_INFO, 27 | (task_info_t)&info, 28 | &size); 29 | 30 | if( kerr == KERN_SUCCESS ) { 31 | printf("ID %i Memory in use (in MBs): %f\n", id,(info.resident_size/1024.0)/1024.0); 32 | } else { 33 | printf("Error with task_info(): %s\n", mach_error_string(kerr)); 34 | } 35 | 36 | #endif 37 | 38 | } 39 | 40 | void Model_resume(){ 41 | SpeedManager_resume(BOT->speedManager); 42 | } 43 | 44 | void Model_addStep(int left, int right){ 45 | 46 | if(left == stepperMotorDirUp){ 47 | BOT->leftsteps ++; 48 | }else if(left == stepperMotorDirDown){ 49 | BOT->leftsteps --; 50 | } 51 | 52 | if(right == stepperMotorDirUp){ 53 | BOT->rightsteps ++; 54 | }else if(right == stepperMotorDirDown){ 55 | BOT->rightsteps --; 56 | } 57 | 58 | } 59 | 60 | void Model_createInstance(){ 61 | BOT = (BotState *) malloc(sizeof(BotState)); 62 | BOT->home = Point_allocWithSteps(0 ,0); 63 | BOT->currentLocation = Point_allocWithSteps(0 ,0); 64 | BOT->leftsteps = BOT->home->left_steps; 65 | BOT->rightsteps = BOT->home->right_steps; 66 | BOT->retainCount = 1; 67 | BOT->delay = Config_maxDelay(); 68 | BOT->type = "BotState"; 69 | BOT->penMode = penModeManualUp; 70 | 71 | sm = SpeedManager_alloc(); 72 | BOT->speedManager = sm; 73 | SpeedManager_setCallback(sm,SpeedManager_callback); 74 | 75 | Model_toPoint = Point_allocWithSteps(0 ,0); 76 | } 77 | 78 | void Model_setPenMode(PenMode mode){ 79 | BOT->scheduledPenMode = mode; 80 | } 81 | 82 | void Model_logState(){ 83 | printf("##############################################\n"); 84 | printf("# MACHINE STATE leftsteps %i rightsteps %i \n",BOT->leftsteps,BOT->rightsteps); 85 | printf("# current location x:%f y:%f \n",BOT->currentLocation->x,BOT->currentLocation->y); 86 | printf("##############################################\n"); 87 | } 88 | 89 | void Model_release(){ 90 | 91 | BOT->speedManager = NULL; 92 | SpeedManager_release(sm); 93 | Point_release(Model_toPoint); 94 | 95 | report_memory(2); 96 | Model_logState(); 97 | 98 | BOT->retainCount --; 99 | if(BOT->retainCount == 0){ 100 | Point_release(BOT->currentLocation); 101 | Point_release(BOT->home); 102 | free(BOT); 103 | } 104 | } 105 | 106 | void Model_retain(){ 107 | FSObject_retain(BOT); 108 | } 109 | 110 | void Model_generateSteps(Point *to){ 111 | 112 | //Point_log(to); 113 | 114 | int delta_steps_left = to->left_steps - BOT->leftsteps; 115 | int delta_steps_right = to->right_steps - BOT->rightsteps; 116 | 117 | int largest = MAX(abs(delta_steps_left),abs(delta_steps_right)); 118 | int smallest = MIN(abs(delta_steps_left),abs(delta_steps_right)); 119 | 120 | //printf("LEFT %i RIGHT %i MAX %i \n\n",delta_steps_left,delta_steps_right,largest); 121 | 122 | StepperMotorDir stepperdir_left = stepperMotorDirNone; 123 | StepperMotorDir stepperdir_right = stepperMotorDirNone; 124 | 125 | if(delta_steps_left < 0){ 126 | stepperdir_left = stepperMotorDirDown; 127 | }else if(delta_steps_left > 0){ 128 | stepperdir_left = stepperMotorDirUp; 129 | }else{ 130 | stepperdir_left = stepperMotorDirNone; 131 | } 132 | 133 | if(delta_steps_right < 0){ 134 | stepperdir_right = stepperMotorDirDown; 135 | }else if(delta_steps_right > 0){ 136 | stepperdir_right = stepperMotorDirUp; 137 | }else{ 138 | stepperdir_right = stepperMotorDirNone; 139 | } 140 | 141 | Step *step = Step_alloc(stepperMotorDirNone, stepperMotorDirNone); 142 | 143 | // printf("INPUT largest %i smallest %i\n\n",largest,smallest); 144 | 145 | int switchmode = 0; 146 | float factor = (float)largest / (float)smallest; 147 | 148 | int skip = round(factor); 149 | 150 | if(factor < 2.0 && factor > 1.0){ 151 | skip = round((float)largest / (float)(largest-smallest)); 152 | switchmode = 1; 153 | } 154 | 155 | int insertcount = 0; 156 | int largestcount = 0; 157 | 158 | StepperMotorDir skipperValue; 159 | 160 | if(abs(delta_steps_left) > abs(delta_steps_right)){ 161 | skipperValue = stepperdir_right; 162 | }else{ 163 | skipperValue = stepperdir_left; 164 | } 165 | 166 | int i = 0; 167 | for(i = 0; i< largest; i++){ 168 | 169 | StepperMotorDir skipper; 170 | 171 | if(switchmode){ 172 | 173 | skipper = skipperValue; 174 | if(i%skip == 0 && insertcount < (largest-smallest)){ 175 | skipper = stepperMotorDirNone; 176 | insertcount ++; 177 | } 178 | 179 | }else{ 180 | 181 | skipper = stepperMotorDirNone; 182 | if(i%skip == 0 && insertcount < smallest){ 183 | skipper = skipperValue; 184 | insertcount ++; 185 | } 186 | 187 | } 188 | 189 | largestcount ++; 190 | 191 | if(abs(delta_steps_left) > abs(delta_steps_right)){ 192 | Step_update(step,stepperdir_left,skipper); 193 | }else{ 194 | Step_update(step,skipper,stepperdir_right); 195 | } 196 | 197 | Model_addStep(step->leftengine,step->rightengine); 198 | 199 | BOT->executeStepCallback(step); 200 | 201 | } 202 | 203 | // if(switchmode){ 204 | // printf("skip %i largest %i smallest %i\n\n",skip,largestcount,largest-insertcount); 205 | // }else{ 206 | // printf("skip %i largest %i smallest %i\n\n",skip,largestcount,insertcount); 207 | // } 208 | 209 | 210 | Step_release(step); 211 | 212 | } 213 | 214 | bool willDrawForLevelAtPoint(Point *point){ 215 | return (BOT->scheduledPenMode == penModeManualDown); 216 | } 217 | 218 | void Model_computeSegments(Point *dest){ 219 | 220 | float deltaX = BOT->currentLocation->x - dest->x; 221 | float deltaY = BOT->currentLocation->y - dest->y; 222 | 223 | float length = sqrt(deltaX*deltaX + deltaY*deltaY); 224 | 225 | int numsteps = round(length/LINE_SEGMENT_SIZE_MM); 226 | float numspaces = (float)numsteps; 227 | 228 | float xstep = deltaX/numspaces; 229 | float ystep = deltaY/numspaces; 230 | 231 | float x = BOT->currentLocation->x; 232 | float y = BOT->currentLocation->y; 233 | 234 | int i=0; 235 | for (i=0; i < numsteps-1; i++){ 236 | x = x - xstep; 237 | y = y - ystep; 238 | Point *p = Point_allocWithXY(x,y); 239 | bool willDraw = willDrawForLevelAtPoint(p); 240 | Point_release(p); 241 | SpeedManager_append(sm,x,y,BOT->scheduledPenMode,willDraw); 242 | } 243 | 244 | Point *p = Point_allocWithXY(dest->x,dest->y); 245 | bool willDraw = willDrawForLevelAtPoint(p); 246 | SpeedManager_append(sm,dest->x,dest->y,BOT->scheduledPenMode,willDraw); 247 | Point_release(p); 248 | 249 | } 250 | 251 | void Model_finish(){ 252 | SpeedManager_finish(sm); 253 | } 254 | 255 | void SpeedManager_callback(float x, float y, int delay,int cursor,int penMode){ 256 | //printf("callback x %f y %f delay %i \n",x,y,delay); 257 | BOT->penMode = penMode; 258 | BOT->delay = delay; 259 | Point_updateWithXY(Model_toPoint,x,y); 260 | Model_generateSteps(Model_toPoint); 261 | } 262 | 263 | void Model_moveHome(){ 264 | //Model_moveTo(BOT->home); 265 | Model_computeSegments(BOT->home); 266 | Point_updateWithXY(BOT->currentLocation,BOT->home->x,BOT->home->y); 267 | } 268 | 269 | void Model_moveTo(Point *dest){ 270 | 271 | int w = Config_getCanvasWidth(); 272 | int h = Config_getCanvasHeight(); 273 | 274 | Point_updateWithXY(dest,dest->x + (MAX_CANVAS_SIZE_X/2.0 - w/2.0),dest->y + (MAX_CANVAS_SIZE_Y/2.0 - h/2.0)); 275 | 276 | Model_computeSegments(dest); 277 | Point_updateWithXY(BOT->currentLocation,dest->x,dest->y); 278 | } 279 | 280 | void Model_setExecuteStepCallback(void (*executeStepCallback)(Step *step)){ 281 | BOT->executeStepCallback = executeStepCallback; 282 | } 283 | 284 | 285 | -------------------------------------------------------------------------------- /sketchy_driver/lua-5.2.3/ltablib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Library for Table Manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltablib_c 11 | #define LUA_LIB 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) 20 | 21 | 22 | 23 | #if defined(LUA_COMPAT_MAXN) 24 | static int maxn (lua_State *L) { 25 | lua_Number max = 0; 26 | luaL_checktype(L, 1, LUA_TTABLE); 27 | lua_pushnil(L); /* first key */ 28 | while (lua_next(L, 1)) { 29 | lua_pop(L, 1); /* remove value */ 30 | if (lua_type(L, -1) == LUA_TNUMBER) { 31 | lua_Number v = lua_tonumber(L, -1); 32 | if (v > max) max = v; 33 | } 34 | } 35 | lua_pushnumber(L, max); 36 | return 1; 37 | } 38 | #endif 39 | 40 | 41 | static int tinsert (lua_State *L) { 42 | int e = aux_getn(L, 1) + 1; /* first empty element */ 43 | int pos; /* where to insert new element */ 44 | switch (lua_gettop(L)) { 45 | case 2: { /* called with only 2 arguments */ 46 | pos = e; /* insert new element at the end */ 47 | break; 48 | } 49 | case 3: { 50 | int i; 51 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 52 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); 53 | for (i = e; i > pos; i--) { /* move up elements */ 54 | lua_rawgeti(L, 1, i-1); 55 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 56 | } 57 | break; 58 | } 59 | default: { 60 | return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 61 | } 62 | } 63 | lua_rawseti(L, 1, pos); /* t[pos] = v */ 64 | return 0; 65 | } 66 | 67 | 68 | static int tremove (lua_State *L) { 69 | int size = aux_getn(L, 1); 70 | int pos = luaL_optint(L, 2, size); 71 | if (pos != size) /* validate 'pos' if given */ 72 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); 73 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ 74 | for ( ; pos < size; pos++) { 75 | lua_rawgeti(L, 1, pos+1); 76 | lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 77 | } 78 | lua_pushnil(L); 79 | lua_rawseti(L, 1, pos); /* t[pos] = nil */ 80 | return 1; 81 | } 82 | 83 | 84 | static void addfield (lua_State *L, luaL_Buffer *b, int i) { 85 | lua_rawgeti(L, 1, i); 86 | if (!lua_isstring(L, -1)) 87 | luaL_error(L, "invalid value (%s) at index %d in table for " 88 | LUA_QL("concat"), luaL_typename(L, -1), i); 89 | luaL_addvalue(b); 90 | } 91 | 92 | 93 | static int tconcat (lua_State *L) { 94 | luaL_Buffer b; 95 | size_t lsep; 96 | int i, last; 97 | const char *sep = luaL_optlstring(L, 2, "", &lsep); 98 | luaL_checktype(L, 1, LUA_TTABLE); 99 | i = luaL_optint(L, 3, 1); 100 | last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); 101 | luaL_buffinit(L, &b); 102 | for (; i < last; i++) { 103 | addfield(L, &b, i); 104 | luaL_addlstring(&b, sep, lsep); 105 | } 106 | if (i == last) /* add last value (if interval was not empty) */ 107 | addfield(L, &b, i); 108 | luaL_pushresult(&b); 109 | return 1; 110 | } 111 | 112 | 113 | /* 114 | ** {====================================================== 115 | ** Pack/unpack 116 | ** ======================================================= 117 | */ 118 | 119 | static int pack (lua_State *L) { 120 | int n = lua_gettop(L); /* number of elements to pack */ 121 | lua_createtable(L, n, 1); /* create result table */ 122 | lua_pushinteger(L, n); 123 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ 124 | if (n > 0) { /* at least one element? */ 125 | int i; 126 | lua_pushvalue(L, 1); 127 | lua_rawseti(L, -2, 1); /* insert first element */ 128 | lua_replace(L, 1); /* move table into index 1 */ 129 | for (i = n; i >= 2; i--) /* assign other elements */ 130 | lua_rawseti(L, 1, i); 131 | } 132 | return 1; /* return table */ 133 | } 134 | 135 | 136 | static int unpack (lua_State *L) { 137 | int i, e, n; 138 | luaL_checktype(L, 1, LUA_TTABLE); 139 | i = luaL_optint(L, 2, 1); 140 | e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); 141 | if (i > e) return 0; /* empty range */ 142 | n = e - i + 1; /* number of elements */ 143 | if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 144 | return luaL_error(L, "too many results to unpack"); 145 | lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 146 | while (i++ < e) /* push arg[i + 1...e] */ 147 | lua_rawgeti(L, 1, i); 148 | return n; 149 | } 150 | 151 | /* }====================================================== */ 152 | 153 | 154 | 155 | /* 156 | ** {====================================================== 157 | ** Quicksort 158 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; 159 | ** Addison-Wesley, 1993.) 160 | ** ======================================================= 161 | */ 162 | 163 | 164 | static void set2 (lua_State *L, int i, int j) { 165 | lua_rawseti(L, 1, i); 166 | lua_rawseti(L, 1, j); 167 | } 168 | 169 | static int sort_comp (lua_State *L, int a, int b) { 170 | if (!lua_isnil(L, 2)) { /* function? */ 171 | int res; 172 | lua_pushvalue(L, 2); 173 | lua_pushvalue(L, a-1); /* -1 to compensate function */ 174 | lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 175 | lua_call(L, 2, 1); 176 | res = lua_toboolean(L, -1); 177 | lua_pop(L, 1); 178 | return res; 179 | } 180 | else /* a < b? */ 181 | return lua_compare(L, a, b, LUA_OPLT); 182 | } 183 | 184 | static void auxsort (lua_State *L, int l, int u) { 185 | while (l < u) { /* for tail recursion */ 186 | int i, j; 187 | /* sort elements a[l], a[(l+u)/2] and a[u] */ 188 | lua_rawgeti(L, 1, l); 189 | lua_rawgeti(L, 1, u); 190 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 191 | set2(L, l, u); /* swap a[l] - a[u] */ 192 | else 193 | lua_pop(L, 2); 194 | if (u-l == 1) break; /* only 2 elements */ 195 | i = (l+u)/2; 196 | lua_rawgeti(L, 1, i); 197 | lua_rawgeti(L, 1, l); 198 | if (sort_comp(L, -2, -1)) /* a[i]= P */ 217 | while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 218 | if (i>=u) luaL_error(L, "invalid order function for sorting"); 219 | lua_pop(L, 1); /* remove a[i] */ 220 | } 221 | /* repeat --j until a[j] <= P */ 222 | while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 223 | if (j<=l) luaL_error(L, "invalid order function for sorting"); 224 | lua_pop(L, 1); /* remove a[j] */ 225 | } 226 | if (j