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 12:22:04 2013 +0000
Revision:
1:3b4d6ea39002
Parent:
0:389e183c9e47
Child:
2:4400650fe664
Small cleanup to main.cpp

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