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:
Wed May 22 16:30:54 2013 +0000
Revision:
2:4400650fe664
Parent:
1:3b4d6ea39002
- Add Timer module to default VM environment; - Add Cleanup calls to the included modules (mbed, console and timer thus far)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerwilson 0:389e183c9e47 1 //
tylerwilson 1:3b4d6ea39002 2 // Simple Pawn 4.x test, built for the mbed LPC1786 and LPC11U24 versions
tylerwilson 0:389e183c9e47 3 //
tylerwilson 1:3b4d6ea39002 4 // Copyright (c) 2012-2013 Pulse-Robotics, Inc.
tylerwilson 1:3b4d6ea39002 5 //
tylerwilson 1:3b4d6ea39002 6 // Author(s): Tyler Wilson
tylerwilson 0:389e183c9e47 7 //
tylerwilson 0:389e183c9e47 8 #include <stdarg.h>
tylerwilson 0:389e183c9e47 9 #include <stdlib.h>
tylerwilson 0:389e183c9e47 10
tylerwilson 0:389e183c9e47 11 #include "mbed.h"
tylerwilson 0:389e183c9e47 12
tylerwilson 2:4400650fe664 13 #include "osdefs.h"
tylerwilson 0:389e183c9e47 14 #include "amx.h"
tylerwilson 0:389e183c9e47 15 #include "amxaux.h"
tylerwilson 0:389e183c9e47 16 #include "amxconsole.h"
tylerwilson 2:4400650fe664 17 #include "amxtime.h"
tylerwilson 0:389e183c9e47 18 #include "amxmbed.h"
tylerwilson 0:389e183c9e47 19 #include "amxpool.h"
tylerwilson 0:389e183c9e47 20
tylerwilson 0:389e183c9e47 21 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
tylerwilson 0:389e183c9e47 22 #define MAX_IMAGE 1024*32
tylerwilson 0:389e183c9e47 23 #elif defined(TARGET_LPC11U24)
tylerwilson 0:389e183c9e47 24 #define MAX_IMAGE 1024*4
tylerwilson 0:389e183c9e47 25 #endif
tylerwilson 0:389e183c9e47 26
tylerwilson 0:389e183c9e47 27 // Objects we may need
tylerwilson 1:3b4d6ea39002 28 //DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
tylerwilson 1:3b4d6ea39002 29 DigitalOut led4(LED4);
tylerwilson 0:389e183c9e47 30 Serial pc(USBTX, USBRX);
tylerwilson 0:389e183c9e47 31 LocalFileSystem local("local");
tylerwilson 0:389e183c9e47 32
tylerwilson 0:389e183c9e47 33 // local prototypes
tylerwilson 0:389e183c9e47 34 void mbed_set_serial(Serial* serial);
tylerwilson 0:389e183c9e47 35 //int heap_stats_callback(void* pBuffer, char const* pFormatString, ...);
tylerwilson 0:389e183c9e47 36
tylerwilson 0:389e183c9e47 37
tylerwilson 0:389e183c9e47 38 int main()
tylerwilson 0:389e183c9e47 39 {
tylerwilson 0:389e183c9e47 40 pc.baud(115200);
tylerwilson 1:3b4d6ea39002 41 mbed_set_serial(&pc); // so the interp. sends and gets to the right place
tylerwilson 0:389e183c9e47 42
tylerwilson 1:3b4d6ea39002 43 // pc.printf("LEDs: 0x%x,0x%x,0x%x,0x%x\n\r", LED1, LED2, LED3, LED4);
tylerwilson 1:3b4d6ea39002 44
tylerwilson 1:3b4d6ea39002 45 // heap test
tylerwilson 1:3b4d6ea39002 46 // char OutputBuffer[256];
tylerwilson 1:3b4d6ea39002 47 // OutputBuffer[0] = '\0';
tylerwilson 1:3b4d6ea39002 48 // _heapstats(heap_stats_callback, OutputBuffer);
tylerwilson 1:3b4d6ea39002 49 // pc.printf("%s\n\r", OutputBuffer);
tylerwilson 1:3b4d6ea39002 50
tylerwilson 0:389e183c9e47 51 int err = AMX_ERR_NONE;
tylerwilson 0:389e183c9e47 52 AMX amx;
tylerwilson 0:389e183c9e47 53
tylerwilson 1:3b4d6ea39002 54 // init pool
tylerwilson 0:389e183c9e47 55 // void* pool = malloc(4096);
tylerwilson 0:389e183c9e47 56 // amx_poolinit(pool, 4096);
tylerwilson 0:389e183c9e47 57
tylerwilson 0:389e183c9e47 58 size_t size = aux_ProgramSize("/local/main.amx");
tylerwilson 0:389e183c9e47 59 pc.printf("/local/main.amx needs %d of memory\n\r", size);
tylerwilson 0:389e183c9e47 60
tylerwilson 0:389e183c9e47 61 pc.printf("loading /local/main.amx\n\r");
tylerwilson 0:389e183c9e47 62 err = aux_LoadProgram(&amx, "/local/main.amx", 0);
tylerwilson 0:389e183c9e47 63 pc.printf("Finished loading, with result %s.\n\r", aux_StrError(err));
tylerwilson 0:389e183c9e47 64
tylerwilson 0:389e183c9e47 65 if (err == AMX_ERR_NONE)
tylerwilson 0:389e183c9e47 66 {
tylerwilson 0:389e183c9e47 67 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
tylerwilson 1:3b4d6ea39002 68 int count = 0, r = 0, i=0;
tylerwilson 0:389e183c9e47 69 ucell uaddr;
tylerwilson 0:389e183c9e47 70 cell *addr;
tylerwilson 0:389e183c9e47 71
tylerwilson 0:389e183c9e47 72 r = amx_NumNatives(&amx, &count);
tylerwilson 0:389e183c9e47 73 pc.printf("natives: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 74 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 75 char temp[16];
tylerwilson 0:389e183c9e47 76 r = amx_GetNative(&amx, i, temp);
tylerwilson 0:389e183c9e47 77 pc.printf("native: %s\n\r", temp);
tylerwilson 0:389e183c9e47 78 }
tylerwilson 0:389e183c9e47 79
tylerwilson 0:389e183c9e47 80 r = amx_NumPublics(&amx, &count);
tylerwilson 0:389e183c9e47 81 pc.printf("publics: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 82 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 83 char temp[16];
tylerwilson 0:389e183c9e47 84 r = amx_GetPublic(&amx, i, temp, &uaddr);
tylerwilson 0:389e183c9e47 85 pc.printf("public: %s, %x\n\r", temp, uaddr);
tylerwilson 0:389e183c9e47 86 }
tylerwilson 0:389e183c9e47 87
tylerwilson 0:389e183c9e47 88 r = amx_NumPubVars(&amx, &count);
tylerwilson 0:389e183c9e47 89 pc.printf("pubvars: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 90 for (i=0; i<count; i++) {
tylerwilson 0:389e183c9e47 91 char temp[16];
tylerwilson 0:389e183c9e47 92 r = amx_GetPubVar(&amx, i, temp, &addr);
tylerwilson 0:389e183c9e47 93 pc.printf("pubvar: %s, %x\n\r", temp, addr);
tylerwilson 0:389e183c9e47 94 }
tylerwilson 0:389e183c9e47 95
tylerwilson 0:389e183c9e47 96 r = amx_NumTags(&amx, &count);
tylerwilson 0:389e183c9e47 97 pc.printf("tags: %d (result:%d)\n\r", count, r);
tylerwilson 0:389e183c9e47 98 pc.printf("\n\r");
tylerwilson 0:389e183c9e47 99 #endif
tylerwilson 1:3b4d6ea39002 100
tylerwilson 0:389e183c9e47 101 // for print and friends
tylerwilson 0:389e183c9e47 102 amx_ConsoleInit(&amx);
tylerwilson 2:4400650fe664 103 // for time functions
tylerwilson 2:4400650fe664 104 amx_TimeInit(&amx);
tylerwilson 0:389e183c9e47 105 // for mbed-specific functions
tylerwilson 0:389e183c9e47 106 amx_mbedInit(&amx);
tylerwilson 0:389e183c9e47 107
tylerwilson 0:389e183c9e47 108 cell ret;
tylerwilson 0:389e183c9e47 109 pc.printf("calling main() via amx_Exec\n\r");
tylerwilson 0:389e183c9e47 110 err = amx_Exec(&amx, &ret, AMX_EXEC_MAIN);
tylerwilson 0:389e183c9e47 111 pc.printf("amx_Exec returned %d, main returned %d\n\r", err, ret);
tylerwilson 0:389e183c9e47 112
tylerwilson 0:389e183c9e47 113 while (err == AMX_ERR_SLEEP)
tylerwilson 0:389e183c9e47 114 {
tylerwilson 1:3b4d6ea39002 115 // let other parts of the system run
tylerwilson 1:3b4d6ea39002 116 wait_ms(10);
tylerwilson 0:389e183c9e47 117
tylerwilson 0:389e183c9e47 118 err = amx_Exec(&amx, &ret, AMX_EXEC_CONT);
tylerwilson 0:389e183c9e47 119 pc.printf("amx_Exec returned %d, main returned %d\n\r", err, ret);
tylerwilson 0:389e183c9e47 120 }
tylerwilson 0:389e183c9e47 121
tylerwilson 2:4400650fe664 122 // clean up our extension modules, in reverse order
tylerwilson 2:4400650fe664 123 amx_mbedCleanup(&amx);
tylerwilson 2:4400650fe664 124 amx_TimeCleanup(&amx);
tylerwilson 2:4400650fe664 125 amx_ConsoleCleanup(&amx);
tylerwilson 2:4400650fe664 126
tylerwilson 0:389e183c9e47 127 pc.printf("calling auxFreeProgram\n\r");
tylerwilson 0:389e183c9e47 128 aux_FreeProgram(&amx);
tylerwilson 0:389e183c9e47 129 pc.printf("called auxFreeProgram\n\r");
tylerwilson 0:389e183c9e47 130 }
tylerwilson 0:389e183c9e47 131
tylerwilson 0:389e183c9e47 132 while (true)
tylerwilson 0:389e183c9e47 133 {
tylerwilson 0:389e183c9e47 134 led4 = !led4;
tylerwilson 1:3b4d6ea39002 135 wait_ms(400);
tylerwilson 0:389e183c9e47 136 }
tylerwilson 0:389e183c9e47 137 }
tylerwilson 0:389e183c9e47 138
tylerwilson 0:389e183c9e47 139 #if 0
tylerwilson 1:3b4d6ea39002 140 int heap_stats_callback(void* pBuffer, char const* pFormatString, ...)
tylerwilson 1:3b4d6ea39002 141 {
tylerwilson 1:3b4d6ea39002 142 char* pStringEnd = (char*)pBuffer + strlen((char*)pBuffer);
tylerwilson 1:3b4d6ea39002 143 va_list valist;
tylerwilson 1:3b4d6ea39002 144
tylerwilson 1:3b4d6ea39002 145 va_start(valist, pFormatString);
tylerwilson 1:3b4d6ea39002 146
tylerwilson 1:3b4d6ea39002 147 return vsprintf(pStringEnd, pFormatString, valist);
tylerwilson 1:3b4d6ea39002 148 }
tylerwilson 1:3b4d6ea39002 149 #endif
tylerwilson 1:3b4d6ea39002 150
tylerwilson 1:3b4d6ea39002 151 // additions to amx.h:
tylerwilson 0:389e183c9e47 152 //
tylerwilson 0:389e183c9e47 153 #if defined __ICC8051__
tylerwilson 0:389e183c9e47 154 #define HAVE_STDINT_H 0
tylerwilson 0:389e183c9e47 155 #define AMX_ANSIONLY 1
tylerwilson 0:389e183c9e47 156 #endif
tylerwilson 0:389e183c9e47 157
tylerwilson 1:3b4d6ea39002 158 // additions to osdefs.h:
tylerwilson 1:3b4d6ea39002 159 #if defined(__ARMCC_VERSION)
tylerwilson 0:389e183c9e47 160 #define AMX_ANSIONLY 1
tylerwilson 0:389e183c9e47 161 #define AMX_NODYNALOAD 1
tylerwilson 0:389e183c9e47 162 // for the io functions putc, getc, etc.
tylerwilson 1:3b4d6ea39002 163 #include "_amxmbed.h"
tylerwilson 1:3b4d6ea39002 164 #endif