Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
amxmbed.cpp@2:01588bd27169, 2013-05-22 (annotated)
- Committer:
- tylerwilson
- Date:
- Wed May 22 16:30:01 2013 +0000
- Revision:
- 2:01588bd27169
- Parent:
- 0:3ab1d2d14eb3
- Child:
- 3:185fdbb7ccf0
- Some additional cleanup; - Fix stime function (call mbed set_time function); - Add explicit amxtime.h header file for inclusion in main function
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 | 2:01588bd27169 | 42 | char *amx_gets(char* s, int i) |
tylerwilson | 0:3ab1d2d14eb3 | 43 | { |
tylerwilson | 2:01588bd27169 | 44 | return port?port->gets(s,i):0; |
tylerwilson | 0:3ab1d2d14eb3 | 45 | } |
tylerwilson | 0:3ab1d2d14eb3 | 46 | |
tylerwilson | 0:3ab1d2d14eb3 | 47 | int amx_termctl(int,int) |
tylerwilson | 0:3ab1d2d14eb3 | 48 | { |
tylerwilson | 2:01588bd27169 | 49 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 50 | } |
tylerwilson | 0:3ab1d2d14eb3 | 51 | |
tylerwilson | 0:3ab1d2d14eb3 | 52 | void amx_clrscr(void) |
tylerwilson | 0:3ab1d2d14eb3 | 53 | { |
tylerwilson | 0:3ab1d2d14eb3 | 54 | } |
tylerwilson | 0:3ab1d2d14eb3 | 55 | |
tylerwilson | 0:3ab1d2d14eb3 | 56 | void amx_clreol(void) |
tylerwilson | 0:3ab1d2d14eb3 | 57 | { |
tylerwilson | 0:3ab1d2d14eb3 | 58 | } |
tylerwilson | 0:3ab1d2d14eb3 | 59 | |
tylerwilson | 0:3ab1d2d14eb3 | 60 | int amx_gotoxy(int x,int y) |
tylerwilson | 0:3ab1d2d14eb3 | 61 | { |
tylerwilson | 0:3ab1d2d14eb3 | 62 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 63 | } |
tylerwilson | 0:3ab1d2d14eb3 | 64 | |
tylerwilson | 0:3ab1d2d14eb3 | 65 | void amx_wherexy(int *x,int *y) |
tylerwilson | 0:3ab1d2d14eb3 | 66 | { |
tylerwilson | 0:3ab1d2d14eb3 | 67 | } |
tylerwilson | 0:3ab1d2d14eb3 | 68 | |
tylerwilson | 0:3ab1d2d14eb3 | 69 | unsigned int amx_setattr(int foregr,int backgr,int highlight) |
tylerwilson | 0:3ab1d2d14eb3 | 70 | { |
tylerwilson | 0:3ab1d2d14eb3 | 71 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 72 | } |
tylerwilson | 0:3ab1d2d14eb3 | 73 | |
tylerwilson | 0:3ab1d2d14eb3 | 74 | void amx_console(int columns, int lines, int flags) |
tylerwilson | 0:3ab1d2d14eb3 | 75 | { |
tylerwilson | 0:3ab1d2d14eb3 | 76 | } |
tylerwilson | 0:3ab1d2d14eb3 | 77 | |
tylerwilson | 0:3ab1d2d14eb3 | 78 | void amx_viewsize(int *width,int *height) |
tylerwilson | 0:3ab1d2d14eb3 | 79 | { |
tylerwilson | 0:3ab1d2d14eb3 | 80 | } |
tylerwilson | 0:3ab1d2d14eb3 | 81 | |
tylerwilson | 0:3ab1d2d14eb3 | 82 | int amx_kbhit(void) |
tylerwilson | 0:3ab1d2d14eb3 | 83 | { |
tylerwilson | 0:3ab1d2d14eb3 | 84 | return port?port->readable():0; |
tylerwilson | 0:3ab1d2d14eb3 | 85 | } |
tylerwilson | 0:3ab1d2d14eb3 | 86 | |
tylerwilson | 0:3ab1d2d14eb3 | 87 | static cell AMX_NATIVE_CALL n_digitalOpen(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 88 | { |
tylerwilson | 0:3ab1d2d14eb3 | 89 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 90 | DigitalOut* self = new DigitalOut((PinName)params[1]); |
tylerwilson | 0:3ab1d2d14eb3 | 91 | // port->printf("digitalOpen(0x%x) returns 0x%x\n\r", (PinName)params[1], self); |
tylerwilson | 0:3ab1d2d14eb3 | 92 | return (cell)self; |
tylerwilson | 0:3ab1d2d14eb3 | 93 | } |
tylerwilson | 0:3ab1d2d14eb3 | 94 | |
tylerwilson | 0:3ab1d2d14eb3 | 95 | static cell AMX_NATIVE_CALL n_digitalRead(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 96 | { |
tylerwilson | 0:3ab1d2d14eb3 | 97 | // port->printf("digitalRead\n\r"); |
tylerwilson | 0:3ab1d2d14eb3 | 98 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 99 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 100 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 101 | { |
tylerwilson | 0:3ab1d2d14eb3 | 102 | return obj->read() != 0; |
tylerwilson | 0:3ab1d2d14eb3 | 103 | } |
tylerwilson | 0:3ab1d2d14eb3 | 104 | |
tylerwilson | 0:3ab1d2d14eb3 | 105 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 106 | } |
tylerwilson | 0:3ab1d2d14eb3 | 107 | |
tylerwilson | 0:3ab1d2d14eb3 | 108 | static cell AMX_NATIVE_CALL n_digitalWrite(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 109 | { |
tylerwilson | 0:3ab1d2d14eb3 | 110 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 111 | // port->printf("digitalWrite(0x%x, %d)\n\r", obj, params[2]); |
tylerwilson | 0:3ab1d2d14eb3 | 112 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 113 | { |
tylerwilson | 0:3ab1d2d14eb3 | 114 | obj->write(params[2]); |
tylerwilson | 0:3ab1d2d14eb3 | 115 | } |
tylerwilson | 0:3ab1d2d14eb3 | 116 | |
tylerwilson | 0:3ab1d2d14eb3 | 117 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 118 | } |
tylerwilson | 0:3ab1d2d14eb3 | 119 | |
tylerwilson | 0:3ab1d2d14eb3 | 120 | static cell AMX_NATIVE_CALL n_digitalClose(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 121 | { |
tylerwilson | 0:3ab1d2d14eb3 | 122 | // port->printf("digitalClose\n\r"); |
tylerwilson | 0:3ab1d2d14eb3 | 123 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 124 | DigitalOut* obj = (DigitalOut*)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 125 | if (obj) |
tylerwilson | 0:3ab1d2d14eb3 | 126 | { |
tylerwilson | 0:3ab1d2d14eb3 | 127 | delete obj; |
tylerwilson | 0:3ab1d2d14eb3 | 128 | } |
tylerwilson | 0:3ab1d2d14eb3 | 129 | |
tylerwilson | 0:3ab1d2d14eb3 | 130 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 131 | } |
tylerwilson | 0:3ab1d2d14eb3 | 132 | |
tylerwilson | 0:3ab1d2d14eb3 | 133 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
tylerwilson | 0:3ab1d2d14eb3 | 134 | static cell AMX_NATIVE_CALL n_wait(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 135 | { |
tylerwilson | 0:3ab1d2d14eb3 | 136 | float amount = amx_ctof(params[1]); |
tylerwilson | 0:3ab1d2d14eb3 | 137 | |
tylerwilson | 0:3ab1d2d14eb3 | 138 | wait(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 139 | |
tylerwilson | 0:3ab1d2d14eb3 | 140 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 141 | } |
tylerwilson | 0:3ab1d2d14eb3 | 142 | #endif |
tylerwilson | 0:3ab1d2d14eb3 | 143 | |
tylerwilson | 0:3ab1d2d14eb3 | 144 | static cell AMX_NATIVE_CALL n_wait_ms(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 145 | { |
tylerwilson | 0:3ab1d2d14eb3 | 146 | int amount = (int)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 147 | // port->printf("waiting %d ms\n\r", amount); |
tylerwilson | 0:3ab1d2d14eb3 | 148 | wait_ms(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 149 | |
tylerwilson | 0:3ab1d2d14eb3 | 150 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 151 | } |
tylerwilson | 0:3ab1d2d14eb3 | 152 | |
tylerwilson | 0:3ab1d2d14eb3 | 153 | static cell AMX_NATIVE_CALL n_wait_us(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 154 | { |
tylerwilson | 0:3ab1d2d14eb3 | 155 | int amount = (int)params[1]; |
tylerwilson | 0:3ab1d2d14eb3 | 156 | wait_us(amount); |
tylerwilson | 0:3ab1d2d14eb3 | 157 | return 0; |
tylerwilson | 0:3ab1d2d14eb3 | 158 | } |
tylerwilson | 0:3ab1d2d14eb3 | 159 | |
tylerwilson | 0:3ab1d2d14eb3 | 160 | static cell AMX_NATIVE_CALL n_kbhit(AMX *amx, const cell *params) |
tylerwilson | 0:3ab1d2d14eb3 | 161 | { |
tylerwilson | 0:3ab1d2d14eb3 | 162 | return amx_kbhit() != 0; |
tylerwilson | 0:3ab1d2d14eb3 | 163 | } |
tylerwilson | 0:3ab1d2d14eb3 | 164 | |
tylerwilson | 0:3ab1d2d14eb3 | 165 | |
tylerwilson | 0:3ab1d2d14eb3 | 166 | const AMX_NATIVE_INFO mbed_Natives[] = { |
tylerwilson | 0:3ab1d2d14eb3 | 167 | { "digitalOpen", n_digitalOpen }, |
tylerwilson | 0:3ab1d2d14eb3 | 168 | { "digitalRead", n_digitalRead }, |
tylerwilson | 0:3ab1d2d14eb3 | 169 | { "digitalWrite", n_digitalWrite }, |
tylerwilson | 0:3ab1d2d14eb3 | 170 | { "digitalClose", n_digitalClose }, |
tylerwilson | 0:3ab1d2d14eb3 | 171 | |
tylerwilson | 0:3ab1d2d14eb3 | 172 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
tylerwilson | 0:3ab1d2d14eb3 | 173 | { "wait", n_wait }, // uses a float, which we do not support on LPC11U24 version |
tylerwilson | 0:3ab1d2d14eb3 | 174 | #endif |
tylerwilson | 0:3ab1d2d14eb3 | 175 | { "wait_ms", n_wait_ms }, |
tylerwilson | 0:3ab1d2d14eb3 | 176 | { "wait_us", n_wait_us }, |
tylerwilson | 0:3ab1d2d14eb3 | 177 | |
tylerwilson | 0:3ab1d2d14eb3 | 178 | { "kbhit", n_kbhit }, |
tylerwilson | 0:3ab1d2d14eb3 | 179 | { NULL, NULL } /* terminator */ |
tylerwilson | 0:3ab1d2d14eb3 | 180 | }; |
tylerwilson | 0:3ab1d2d14eb3 | 181 | |
tylerwilson | 0:3ab1d2d14eb3 | 182 | int AMXEXPORT AMXAPI amx_mbedInit(AMX *amx) |
tylerwilson | 0:3ab1d2d14eb3 | 183 | { |
tylerwilson | 0:3ab1d2d14eb3 | 184 | return amx_Register(amx, mbed_Natives, -1); |
tylerwilson | 0:3ab1d2d14eb3 | 185 | } |
tylerwilson | 0:3ab1d2d14eb3 | 186 | |
tylerwilson | 0:3ab1d2d14eb3 | 187 | int AMXEXPORT AMXAPI amx_mbedCleanup(AMX *amx) |
tylerwilson | 0:3ab1d2d14eb3 | 188 | { |
tylerwilson | 0:3ab1d2d14eb3 | 189 | (void)amx; |
tylerwilson | 0:3ab1d2d14eb3 | 190 | return AMX_ERR_NONE; |
tylerwilson | 0:3ab1d2d14eb3 | 191 | } |