Test program for Pawn 4 interpreter. Looks for a main.amx on the mbed 'drive', loads it, and calls the main() function within it.

Dependencies:   Pawn4 mbed

Welcome to the Pawn 4 mbed test program. This program is designed to show off all features of the Pawn mbed port. Currently, it will do the following:

- Create a Pawn VM instance - Add the following modules to the Pawn VM - mbed - console (used only for printf currently) - time (date/time, delay functions) - Look for a main.amx file at the root of the mbed file system. If found, will attempt to load whole file into memory. IMPORTANT: when the program opens the file on the mbed file system, the mbed will unmount itself from any connected PC/Mac, possibly resulting in a rude-looking OS message (I am looking at you OS X). It will reappear when the program closes the main.amx file. - Attempt to execute the main function in the loaded script - When the main routine returns, it will currently sit in a loop blinking LED4. It may be better to simply re-run the main routine. Perhaps a special return value can determine what we do...

Also see the Pawn4 library project for details on how to set up your development environment on the mbed itself and how to build scripts.

Committer:
tylerwilson
Date:
Thu Nov 15 17:48:16 2012 +0000
Revision:
0:389e183c9e47
Child:
1:3b4d6ea39002
First example for Pawn 4 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerwilson 0:389e183c9e47 1 //
tylerwilson 0:389e183c9e47 2 // Simple Pawn 4.x test, built for the mbed LPC11U24 version
tylerwilson 0:389e183c9e47 3 //
tylerwilson 0:389e183c9e47 4 // Copyright (c) 2012 Tyler Wilson
tylerwilson 0:389e183c9e47 5 //
tylerwilson 0:389e183c9e47 6 #include <stdarg.h>
tylerwilson 0:389e183c9e47 7 #include <stdlib.h>
tylerwilson 0:389e183c9e47 8
tylerwilson 0:389e183c9e47 9 #include "mbed.h"
tylerwilson 0:389e183c9e47 10
tylerwilson 0:389e183c9e47 11 #include "amx.h"
tylerwilson 0:389e183c9e47 12 #include "amxaux.h"
tylerwilson 0:389e183c9e47 13 #include "amxconsole.h"
tylerwilson 0:389e183c9e47 14 #include "amxmbed.h"
tylerwilson 0:389e183c9e47 15 #include "amxpool.h"
tylerwilson 0:389e183c9e47 16
tylerwilson 0:389e183c9e47 17 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
tylerwilson 0:389e183c9e47 18 #define MAX_IMAGE 1024*32
tylerwilson 0:389e183c9e47 19 #elif defined(TARGET_LPC11U24)
tylerwilson 0:389e183c9e47 20 #define MAX_IMAGE 1024*4
tylerwilson 0:389e183c9e47 21 #endif
tylerwilson 0:389e183c9e47 22
tylerwilson 0:389e183c9e47 23 // Objects we may need
tylerwilson 0:389e183c9e47 24 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
tylerwilson 0:389e183c9e47 25 Serial pc(USBTX, USBRX);
tylerwilson 0:389e183c9e47 26 LocalFileSystem local("local");
tylerwilson 0:389e183c9e47 27
tylerwilson 0:389e183c9e47 28 // local prototypes
tylerwilson 0:389e183c9e47 29 //int aux_LoadProgram(AMX* amx, const char* filename);
tylerwilson 0:389e183c9e47 30 //int aux_FreeProgram(AMX* amx);
tylerwilson 0:389e183c9e47 31 void mbed_set_serial(Serial* serial);
tylerwilson 0:389e183c9e47 32 //int heap_stats_callback(void* pBuffer, char const* pFormatString, ...);
tylerwilson 0:389e183c9e47 33
tylerwilson 0:389e183c9e47 34
tylerwilson 0:389e183c9e47 35 int main()
tylerwilson 0:389e183c9e47 36 {
tylerwilson 0:389e183c9e47 37 // set up our default speed
tylerwilson 0:389e183c9e47 38 pc.baud(115200);
tylerwilson 0:389e183c9e47 39
tylerwilson 0:389e183c9e47 40 // so the Pawn interpreter sends/gets to/from the right place
tylerwilson 0:389e183c9e47 41 mbed_set_serial(&pc);
tylerwilson 0:389e183c9e47 42
tylerwilson 0:389e183c9e47 43 pc.printf("Pawn running on ");
tylerwilson 0:389e183c9e47 44 #if defined(TARGET_LPC1768)
tylerwilson 0:389e183c9e47 45 pc.printf("mbed LPC1768");
tylerwilson 0:389e183c9e47 46 #elif defined(TARGET_LPC2368)
tylerwilson 0:389e183c9e47 47 pc.printf("mbed LPC2368");
tylerwilson 0:389e183c9e47 48 #elif defined(TARGET_LPC11U24)
tylerwilson 0:389e183c9e47 49 pc.printf("mbed LPC11U24");
tylerwilson 0:389e183c9e47 50 #else
tylerwilson 0:389e183c9e47 51 pc.printf("unknown");
tylerwilson 0:389e183c9e47 52 #endif
tylerwilson 0:389e183c9e47 53 pc.printf("\n\r");
tylerwilson 0:389e183c9e47 54
tylerwilson 0:389e183c9e47 55 int err = AMX_ERR_NONE;
tylerwilson 0:389e183c9e47 56 AMX amx;
tylerwilson 0:389e183c9e47 57
tylerwilson 0:389e183c9e47 58 // init pool for the overlays (not on M0 [most likely])
tylerwilson 0:389e183c9e47 59 // void* pool = malloc(4096);
tylerwilson 0:389e183c9e47 60 // amx_poolinit(pool, 4096);
tylerwilson 0:389e183c9e47 61
tylerwilson 0:389e183c9e47 62 // how much memory to completely load and run this script
tylerwilson 0:389e183c9e47 63 size_t size = aux_ProgramSize("/local/main.amx");
tylerwilson 0:389e183c9e47 64 pc.printf("/local/main.amx needs %d of memory\n\r", size);
tylerwilson 0:389e183c9e47 65
tylerwilson 0:389e183c9e47 66 pc.printf("loading /local/main.amx\n\r");
tylerwilson 0:389e183c9e47 67 err = aux_LoadProgram(&amx, "/local/main.amx", 0);
tylerwilson 0:389e183c9e47 68
tylerwilson 0:389e183c9e47 69 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
tylerwilson 0:389e183c9e47 70 pc.printf("Finished loading, with result %s.\n\r", aux_StrError(err));
tylerwilson 0:389e183c9e47 71 #else
tylerwilson 0:389e183c9e47 72 pc.printf("Finished loading, with result %d.\n\r", err);
tylerwilson 0:389e183c9e47 73 #endif
tylerwilson 0:389e183c9e47 74
tylerwilson 0:389e183c9e47 75 if (err == AMX_ERR_NONE)
tylerwilson 0:389e183c9e47 76 {
tylerwilson 0:389e183c9e47 77 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
tylerwilson 0:389e183c9e47 78 // some debug output. remove on M0 part due to low memory
tylerwilson 0:389e183c9e47 79
tylerwilson 0:389e183c9e47 80 int count = 0, r = 0, i = 0;
tylerwilson 0:389e183c9e47 81 ucell uaddr;
tylerwilson 0:389e183c9e47 82 cell *addr;
tylerwilson 0:389e183c9e47 83
tylerwilson 0:389e183c9e47 84 // what natives this script calls
tylerwilson 0:389e183c9e47 85 r = amx_NumNatives(&amx, &count);
tylerwilson 0:389e183c9e47 86 pc.printf("natives: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 87 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 88 char temp[16];
tylerwilson 0:389e183c9e47 89 r = amx_GetNative(&amx, i, temp);
tylerwilson 0:389e183c9e47 90 pc.printf("native: %s\n\r", temp);
tylerwilson 0:389e183c9e47 91 }
tylerwilson 0:389e183c9e47 92
tylerwilson 0:389e183c9e47 93 // what public functions this script exposes
tylerwilson 0:389e183c9e47 94 r = amx_NumPublics(&amx, &count);
tylerwilson 0:389e183c9e47 95 pc.printf("publics: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 96 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 97 char temp[16];
tylerwilson 0:389e183c9e47 98 r = amx_GetPublic(&amx, i, temp, &uaddr);
tylerwilson 0:389e183c9e47 99 pc.printf("public: %s, %x\n\r", temp, uaddr);
tylerwilson 0:389e183c9e47 100 }
tylerwilson 0:389e183c9e47 101
tylerwilson 0:389e183c9e47 102 // what public variables are defined in this script
tylerwilson 0:389e183c9e47 103 r = amx_NumPubVars(&amx, &count);
tylerwilson 0:389e183c9e47 104 pc.printf("pubvars: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 105 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 106 char temp[16];
tylerwilson 0:389e183c9e47 107 r = amx_GetPubVar(&amx, i, temp, &addr);
tylerwilson 0:389e183c9e47 108 pc.printf("pubvar: %s, %x\n\r", temp, addr);
tylerwilson 0:389e183c9e47 109 }
tylerwilson 0:389e183c9e47 110
tylerwilson 0:389e183c9e47 111 // what tags are defined in this script
tylerwilson 0:389e183c9e47 112 r = amx_NumTags(&amx, &count);
tylerwilson 0:389e183c9e47 113 pc.printf("tags: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 114 pc.printf("\n\r");
tylerwilson 0:389e183c9e47 115 #endif
tylerwilson 0:389e183c9e47 116
tylerwilson 0:389e183c9e47 117 // for print and friends
tylerwilson 0:389e183c9e47 118 amx_ConsoleInit(&amx);
tylerwilson 0:389e183c9e47 119
tylerwilson 0:389e183c9e47 120 // for mbed-specific functions
tylerwilson 0:389e183c9e47 121 amx_mbedInit(&amx);
tylerwilson 0:389e183c9e47 122
tylerwilson 0:389e183c9e47 123 // make the first call into the main function.
tylerwilson 0:389e183c9e47 124 cell ret;
tylerwilson 0:389e183c9e47 125 pc.printf("calling main() via amx_Exec\n\r");
tylerwilson 0:389e183c9e47 126 err = amx_Exec(&amx, &ret, AMX_EXEC_MAIN);
tylerwilson 0:389e183c9e47 127 pc.printf("amx_Exec returned %d, main returned %d\n\r", err, ret);
tylerwilson 0:389e183c9e47 128
tylerwilson 0:389e183c9e47 129 // as long as the script is waiting, we will keep running
tylerwilson 0:389e183c9e47 130 while (err == AMX_ERR_SLEEP)
tylerwilson 0:389e183c9e47 131 {
tylerwilson 0:389e183c9e47 132 // let other parts of the system run (needed? script should be calling wait as well)
tylerwilson 0:389e183c9e47 133 // wait_ms(10);
tylerwilson 0:389e183c9e47 134
tylerwilson 0:389e183c9e47 135 err = amx_Exec(&amx, &ret, AMX_EXEC_CONT);
tylerwilson 0:389e183c9e47 136 pc.printf("amx_Exec returned %d, main returned %d\n\r", err, ret);
tylerwilson 0:389e183c9e47 137 }
tylerwilson 0:389e183c9e47 138
tylerwilson 0:389e183c9e47 139 // should not get here in most cases, but we should be clean anyway
tylerwilson 0:389e183c9e47 140 pc.printf("calling auxFreeProgram\n\r");
tylerwilson 0:389e183c9e47 141 aux_FreeProgram(&amx);
tylerwilson 0:389e183c9e47 142 pc.printf("called auxFreeProgram\n\r");
tylerwilson 0:389e183c9e47 143 }
tylerwilson 0:389e183c9e47 144
tylerwilson 0:389e183c9e47 145 // go into a loop so the user knows we have exited the script
tylerwilson 0:389e183c9e47 146 while (true)
tylerwilson 0:389e183c9e47 147 {
tylerwilson 0:389e183c9e47 148 led4 = !led4;
tylerwilson 0:389e183c9e47 149 wait_ms(500);
tylerwilson 0:389e183c9e47 150 }
tylerwilson 0:389e183c9e47 151 }
tylerwilson 0:389e183c9e47 152
tylerwilson 0:389e183c9e47 153 #if 0
tylerwilson 0:389e183c9e47 154 // additions to amx.h (for CC254x):
tylerwilson 0:389e183c9e47 155 //
tylerwilson 0:389e183c9e47 156 #if defined __ICC8051__
tylerwilson 0:389e183c9e47 157 #define HAVE_STDINT_H 0
tylerwilson 0:389e183c9e47 158 #define AMX_ANSIONLY 1
tylerwilson 0:389e183c9e47 159 #endif
tylerwilson 0:389e183c9e47 160
tylerwilson 0:389e183c9e47 161 // additions to osdefs.h (for mbed):
tylerwilson 0:389e183c9e47 162 //
tylerwilson 0:389e183c9e47 163 //#if defined(__ARMCC_VERSION)
tylerwilson 0:389e183c9e47 164 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24)
tylerwilson 0:389e183c9e47 165 #define AMX_ANSIONLY 1
tylerwilson 0:389e183c9e47 166 #define AMX_NODYNALOAD 1
tylerwilson 0:389e183c9e47 167 #define AMX_TERMINAL 1
tylerwilson 0:389e183c9e47 168
tylerwilson 0:389e183c9e47 169 #define stime(x) (x)
tylerwilson 0:389e183c9e47 170
tylerwilson 0:389e183c9e47 171 // for the io functions putc, getc, etc.
tylerwilson 0:389e183c9e47 172 // #include "_amxmbed.h"
tylerwilson 0:389e183c9e47 173 #endif
tylerwilson 0:389e183c9e47 174 #endif