This is the open source Pawn interpreter ported to mbed. See here: http://www.compuphase.com/pawn/pawn.htm and here: http://code.google.com/p/pawnscript/
Some instructions:
- Put the attached include folder next to your source, so when you compile you get all the proper definitions
- Use the attached main.p as a starting point if you wish
- Compile your main.p into main.amx - Put your main.amx on the mbed 'drive'
- Reset and be amazed.
Important Compile Notes:
- You should use the -S# option to define a smaller default stack size. Start with -S64 and go up from there if needed.
- To use on the Cortex-M0 version of the mbed (LPC11U24), you MUST include the TARGET=3 command-line option as well, so the pin names are properly defined. In the future this may be handled on the native code side.
Known Issues:
At the moment it appears the kbhit() function is not working right - at least on my mac. Will continue testing on Windows.Working fine.
Todo:
- Add more wrappers for the mbed peripherals
- Add Pawn overlay support, to allow much larger scripts to run (even on the LPC11U24)
amxmbed.cpp@0:3ab1d2d14eb3, 2012-11-15 (annotated)
- Committer:
- tylerwilson
- Date:
- Thu Nov 15 17:41:21 2012 +0000
- Revision:
- 0:3ab1d2d14eb3
- Child:
- 2:01588bd27169
Initial Pawn 4.x interpreter for mbed check-in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerwilson | 0:3ab1d2d14eb3 | 1 | /** |
tylerwilson | 0:3ab1d2d14eb3 | 2 | * Interface between Pawn interpreter and mbed platform. |
tylerwilson | 0:3ab1d2d14eb3 | 3 | * |
tylerwilson | 0:3ab1d2d14eb3 | 4 | * Copyright 2011 Pulse-Robotics, Inc. |
tylerwilson | 0:3ab1d2d14eb3 | 5 | * Author: Tyler Wilson |
tylerwilson | 0:3ab1d2d14eb3 | 6 | */ |
tylerwilson | 0:3ab1d2d14eb3 | 7 | |
tylerwilson | 0:3ab1d2d14eb3 | 8 | #include <assert.h> |
tylerwilson | 0:3ab1d2d14eb3 | 9 | |
tylerwilson | 0:3ab1d2d14eb3 | 10 | #include "_amxmbed.h" |
tylerwilson | 0:3ab1d2d14eb3 | 11 | #include "amxmbed.h" |
tylerwilson | 0:3ab1d2d14eb3 | 12 | #include "amx.h" |
tylerwilson | 0:3ab1d2d14eb3 | 13 | #include "mbed.h" |
tylerwilson | 0:3ab1d2d14eb3 | 14 | |
tylerwilson | 0:3ab1d2d14eb3 | 15 | Serial* port = 0; |
tylerwilson | 0:3ab1d2d14eb3 | 16 | |
tylerwilson | 0:3ab1d2d14eb3 | 17 | void mbed_set_serial(Serial* serial) |
tylerwilson | 0:3ab1d2d14eb3 | 18 | { |
tylerwilson | 0:3ab1d2d14eb3 | 19 | port = serial; |
tylerwilson | 0:3ab1d2d14eb3 | 20 | } |
tylerwilson | 0:3ab1d2d14eb3 | 21 | |
tylerwilson | 0:3ab1d2d14eb3 | 22 | int amx_putstr(const char *s) |
tylerwilson | 0:3ab1d2d14eb3 | 23 | { |
tylerwilson | 0:3ab1d2d14eb3 | 24 | return port?port->printf(s):0; |
tylerwilson | 0:3ab1d2d14eb3 | 25 | } |
tylerwilson | 0:3ab1d2d14eb3 | 26 | |
tylerwilson | 0:3ab1d2d14eb3 | 27 | int amx_putchar(int c) |
tylerwilson | 0:3ab1d2d14eb3 | 28 | { |
tylerwilson | 0:3ab1d2d14eb3 | 29 | return port?port->putc(c):0; |
tylerwilson | 0:3ab1d2d14eb3 | 30 | } |
tylerwilson | 0:3ab1d2d14eb3 | 31 | |
tylerwilson | 0:3ab1d2d14eb3 | 32 | int amx_fflush(void) |
tylerwilson | 0:3ab1d2d14eb3 | 33 | { |
tylerwilson | 0:3ab1d2d14eb3 | 34 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 35 | } |
tylerwilson | 0:3ab1d2d14eb3 | 36 | |
tylerwilson | 0:3ab1d2d14eb3 | 37 | int amx_getch(void) |
tylerwilson | 0:3ab1d2d14eb3 | 38 | { |
tylerwilson | 0:3ab1d2d14eb3 | 39 | return port?port->getc():0; |
tylerwilson | 0:3ab1d2d14eb3 | 40 | } |
tylerwilson | 0:3ab1d2d14eb3 | 41 | |
tylerwilson | 0:3ab1d2d14eb3 | 42 | char *amx_gets(char*, int) |
tylerwilson | 0:3ab1d2d14eb3 | 43 | { |
tylerwilson | 0:3ab1d2d14eb3 | 44 | } |
tylerwilson | 0:3ab1d2d14eb3 | 45 | |
tylerwilson | 0:3ab1d2d14eb3 | 46 | int amx_termctl(int,int) |
tylerwilson | 0:3ab1d2d14eb3 | 47 | { |
tylerwilson | 0:3ab1d2d14eb3 | 48 | } |
tylerwilson | 0:3ab1d2d14eb3 | 49 | |
tylerwilson | 0:3ab1d2d14eb3 | 50 | void amx_clrscr(void) |
tylerwilson | 0:3ab1d2d14eb3 | 51 | { |
tylerwilson | 0:3ab1d2d14eb3 | 52 | } |
tylerwilson | 0:3ab1d2d14eb3 | 53 | |
tylerwilson | 0:3ab1d2d14eb3 | 54 | void amx_clreol(void) |
tylerwilson | 0:3ab1d2d14eb3 | 55 | { |
tylerwilson | 0:3ab1d2d14eb3 | 56 | } |
tylerwilson | 0:3ab1d2d14eb3 | 57 | |
tylerwilson | 0:3ab1d2d14eb3 | 58 | int amx_gotoxy(int x,int y) |
tylerwilson | 0:3ab1d2d14eb3 | 59 | { |
tylerwilson | 0:3ab1d2d14eb3 | 60 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 61 | } |
tylerwilson | 0:3ab1d2d14eb3 | 62 | |
tylerwilson | 0:3ab1d2d14eb3 | 63 | void amx_wherexy(int *x,int *y) |
tylerwilson | 0:3ab1d2d14eb3 | 64 | { |
tylerwilson | 0:3ab1d2d14eb3 | 65 | } |
tylerwilson | 0:3ab1d2d14eb3 | 66 | |
tylerwilson | 0:3ab1d2d14eb3 | 67 | unsigned int amx_setattr(int foregr,int backgr,int highlight) |
tylerwilson | 0:3ab1d2d14eb3 | 68 | { |
tylerwilson | 0:3ab1d2d14eb3 | 69 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 70 | } |
tylerwilson | 0:3ab1d2d14eb3 | 71 | |
tylerwilson | 0:3ab1d2d14eb3 | 72 | void amx_console(int columns, int lines, int flags) |
tylerwilson | 0:3ab1d2d14eb3 | 73 | { |
tylerwilson | 0:3ab1d2d14eb3 | 74 | } |
tylerwilson | 0:3ab1d2d14eb3 | 75 | |
tylerwilson | 0:3ab1d2d14eb3 | 76 | void amx_viewsize(int *width,int *height) |
tylerwilson | 0:3ab1d2d14eb3 | 77 | { |
tylerwilson | 0:3ab1d2d14eb3 | 78 | } |
tylerwilson | 0:3ab1d2d14eb3 | 79 | |
tylerwilson | 0:3ab1d2d14eb3 | 80 | int amx_kbhit(void) |
tylerwilson | 0:3ab1d2d14eb3 | 81 | { |
tylerwilson | 0:3ab1d2d14eb3 | 82 | return port?port->readable():0; |
tylerwilson | 0:3ab1d2d14eb3 | 83 | } |
tylerwilson | 0:3ab1d2d14eb3 | 84 | |
tylerwilson | 0:3ab1d2d14eb3 | 85 | static cell AMX_NATIVE_CALL n_digitalOpen(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 86 | { |
tylerwilson | 0:3ab1d2d14eb3 | 87 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 88 | DigitalOut* self = new DigitalOut((PinName)params[1]); |
tylerwilson | 0:3ab1d2d14eb3 | 89 | // port->printf("digitalOpen(0x%x) returns 0x%x\n\r", (PinName)params[1], self); |
tylerwilson | 0:3ab1d2d14eb3 | 90 | return (cell)self; |
tylerwilson | 0:3ab1d2d14eb3 | 91 | } |
tylerwilson | 0:3ab1d2d14eb3 | 92 | |
tylerwilson | 0:3ab1d2d14eb3 | 93 | static cell AMX_NATIVE_CALL n_digitalRead(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 94 | { |
tylerwilson | 0:3ab1d2d14eb3 | 95 | // port->printf("digitalRead\n\r"); |
tylerwilson | 0:3ab1d2d14eb3 | 96 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 97 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 98 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 99 | { |
tylerwilson | 0:3ab1d2d14eb3 | 100 | return obj->read() != 0; |
tylerwilson | 0:3ab1d2d14eb3 | 101 | } |
tylerwilson | 0:3ab1d2d14eb3 | 102 | |
tylerwilson | 0:3ab1d2d14eb3 | 103 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 104 | } |
tylerwilson | 0:3ab1d2d14eb3 | 105 | |
tylerwilson | 0:3ab1d2d14eb3 | 106 | static cell AMX_NATIVE_CALL n_digitalWrite(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 107 | { |
tylerwilson | 0:3ab1d2d14eb3 | 108 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 109 | // port->printf("digitalWrite(0x%x, %d)\n\r", obj, params[2]); |
tylerwilson | 0:3ab1d2d14eb3 | 110 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 111 | { |
tylerwilson | 0:3ab1d2d14eb3 | 112 | obj->write(params[2]); |
tylerwilson | 0:3ab1d2d14eb3 | 113 | } |
tylerwilson | 0:3ab1d2d14eb3 | 114 | |
tylerwilson | 0:3ab1d2d14eb3 | 115 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 116 | } |
tylerwilson | 0:3ab1d2d14eb3 | 117 | |
tylerwilson | 0:3ab1d2d14eb3 | 118 | static cell AMX_NATIVE_CALL n_digitalClose(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 119 | { |
tylerwilson | 0:3ab1d2d14eb3 | 120 | // port->printf("digitalClose\n\r"); |
tylerwilson | 0:3ab1d2d14eb3 | 121 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 122 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 123 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 124 | { |
tylerwilson | 0:3ab1d2d14eb3 | 125 | delete obj; |
tylerwilson | 0:3ab1d2d14eb3 | 126 | } |
tylerwilson | 0:3ab1d2d14eb3 | 127 | |
tylerwilson | 0:3ab1d2d14eb3 | 128 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 129 | } |
tylerwilson | 0:3ab1d2d14eb3 | 130 | |
tylerwilson | 0:3ab1d2d14eb3 | 131 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
tylerwilson | 0:3ab1d2d14eb3 | 132 | static cell AMX_NATIVE_CALL n_wait(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 133 | { |
tylerwilson | 0:3ab1d2d14eb3 | 134 | float amount = amx_ctof(params[1]); |
tylerwilson | 0:3ab1d2d14eb3 | 135 | |
tylerwilson | 0:3ab1d2d14eb3 | 136 | wait(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 137 | |
tylerwilson | 0:3ab1d2d14eb3 | 138 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 139 | } |
tylerwilson | 0:3ab1d2d14eb3 | 140 | #endif |
tylerwilson | 0:3ab1d2d14eb3 | 141 | |
tylerwilson | 0:3ab1d2d14eb3 | 142 | static cell AMX_NATIVE_CALL n_wait_ms(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 143 | { |
tylerwilson | 0:3ab1d2d14eb3 | 144 | int amount = (int)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 145 | // port->printf("waiting %d ms\n\r", amount); |
tylerwilson | 0:3ab1d2d14eb3 | 146 | wait_ms(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 147 | |
tylerwilson | 0:3ab1d2d14eb3 | 148 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 149 | } |
tylerwilson | 0:3ab1d2d14eb3 | 150 | |
tylerwilson | 0:3ab1d2d14eb3 | 151 | static cell AMX_NATIVE_CALL n_wait_us(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 152 | { |
tylerwilson | 0:3ab1d2d14eb3 | 153 | int amount = (int)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 154 | wait_us(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 155 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 156 | } |
tylerwilson | 0:3ab1d2d14eb3 | 157 | |
tylerwilson | 0:3ab1d2d14eb3 | 158 | static cell AMX_NATIVE_CALL n_kbhit(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 159 | { |
tylerwilson | 0:3ab1d2d14eb3 | 160 | return amx_kbhit() != 0; |
tylerwilson | 0:3ab1d2d14eb3 | 161 | } |
tylerwilson | 0:3ab1d2d14eb3 | 162 | |
tylerwilson | 0:3ab1d2d14eb3 | 163 | |
tylerwilson | 0:3ab1d2d14eb3 | 164 | const AMX_NATIVE_INFO mbed_Natives[] = { |
tylerwilson | 0:3ab1d2d14eb3 | 165 | { "digitalOpen", n_digitalOpen }, |
tylerwilson | 0:3ab1d2d14eb3 | 166 | { "digitalRead", n_digitalRead }, |
tylerwilson | 0:3ab1d2d14eb3 | 167 | { "digitalWrite", n_digitalWrite }, |
tylerwilson | 0:3ab1d2d14eb3 | 168 | { "digitalClose", n_digitalClose }, |
tylerwilson | 0:3ab1d2d14eb3 | 169 | |
tylerwilson | 0:3ab1d2d14eb3 | 170 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
tylerwilson | 0:3ab1d2d14eb3 | 171 | { "wait", n_wait }, // uses a float, which we do not support on LPC11U24 version |
tylerwilson | 0:3ab1d2d14eb3 | 172 | #endif |
tylerwilson | 0:3ab1d2d14eb3 | 173 | { "wait_ms", n_wait_ms }, |
tylerwilson | 0:3ab1d2d14eb3 | 174 | { "wait_us", n_wait_us }, |
tylerwilson | 0:3ab1d2d14eb3 | 175 | |
tylerwilson | 0:3ab1d2d14eb3 | 176 | { "kbhit", n_kbhit }, |
tylerwilson | 0:3ab1d2d14eb3 | 177 | { NULL, NULL } /* terminator */ |
tylerwilson | 0:3ab1d2d14eb3 | 178 | }; |
tylerwilson | 0:3ab1d2d14eb3 | 179 | |
tylerwilson | 0:3ab1d2d14eb3 | 180 | int AMXEXPORT AMXAPI amx_mbedInit(AMX *amx) |
tylerwilson | 0:3ab1d2d14eb3 | 181 | { |
tylerwilson | 0:3ab1d2d14eb3 | 182 | return amx_Register(amx, mbed_Natives, -1); |
tylerwilson | 0:3ab1d2d14eb3 | 183 | } |
tylerwilson | 0:3ab1d2d14eb3 | 184 | |
tylerwilson | 0:3ab1d2d14eb3 | 185 | int AMXEXPORT AMXAPI amx_mbedCleanup(AMX *amx) |
tylerwilson | 0:3ab1d2d14eb3 | 186 | { |
tylerwilson | 0:3ab1d2d14eb3 | 187 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 188 | return AMX_ERR_NONE; |
tylerwilson | 0:3ab1d2d14eb3 | 189 | } |