LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / libraries / PyMite / lib / sys.py
bloba9c434bd95a26f3cf4e142645861fdd5f0e92f07
1 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
3 # This file is part of the Python-on-a-Chip program.
4 # Python-on-a-Chip is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
7 # Python-on-a-Chip is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
11 # is seen in the file COPYING in this directory.
13 ## @package sys
14 # @brief Provides PyMite's system module, sys
16 # USAGE
17 # -----
19 # import sys
22 #### TODO
23 # modules = None #set ptr to dict w/native func
24 # platform string or device id, rand
25 # ver = "0.1" # XXX compile date & platform?
26 # Example: sys.version = '2.4.1 (#1, Feb 26 2006, 16:26:36) \n[GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)]'
29 maxint = 0x7FFFFFFF # 2147483647
32 def exit(val):
33 """__NATIVE__
34 pPmObj_t pval = C_NULL;
35 PmReturn_t retval;
37 /* If no arg given, assume return 0 */
38 if (NATIVE_GET_NUM_ARGS() == 0)
40 NATIVE_SET_TOS(PM_ZERO);
43 /* If 1 arg given, put it on stack */
44 else if (NATIVE_GET_NUM_ARGS() == 1)
46 pval = NATIVE_GET_LOCAL(0);
47 NATIVE_SET_TOS(pval);
50 /* If wrong number of args, raise TypeError */
51 else
53 PM_RAISE(retval, PM_RET_EX_TYPE);
54 return retval;
57 /* Raise the SystemExit exception */
58 PM_RAISE(retval, PM_RET_EX_EXIT);
59 return retval;
60 """
61 pass
65 # Runs the Garbage Collector
67 def gc():
68 """__NATIVE__
69 PmReturn_t retval = PM_RET_OK;
70 #ifdef HAVE_GC
71 /* If wrong number of args, raise TypeError */
72 if (NATIVE_GET_NUM_ARGS() != 0)
74 PM_RAISE(retval, PM_RET_EX_TYPE);
75 return retval;
78 retval = heap_gcRun();
79 #endif
80 NATIVE_SET_TOS(PM_NONE);
82 return retval;
83 """
84 pass
88 # Gets a byte from the platform's default I/O
89 # Returns the byte in the LSB of the returned integer
91 def getb():
92 """__NATIVE__
93 uint8_t b;
94 pPmObj_t pb;
95 PmReturn_t retval;
97 /* If wrong number of args, raise TypeError */
98 if (NATIVE_GET_NUM_ARGS() != 0)
100 PM_RAISE(retval, PM_RET_EX_TYPE);
101 return retval;
104 retval = plat_getByte(&b);
105 PM_RETURN_IF_ERROR(retval);
107 retval = int_new((int32_t)b, &pb);
108 NATIVE_SET_TOS(pb);
109 return retval;
111 pass
115 # Returns a tuple containing the amout of heap available and the maximum
117 def heap():
118 """__NATIVE__
119 PmReturn_t retval;
120 pPmObj_t pavail;
121 pPmObj_t pmax;
122 pPmObj_t ptup;
123 uint8_t objid;
125 /* If wrong number of args, raise TypeError */
126 if (NATIVE_GET_NUM_ARGS() != 0)
128 PM_RAISE(retval, PM_RET_EX_TYPE);
129 return retval;
132 /* Allocate a tuple to store the return values */
133 retval = tuple_new(2, &ptup);
134 PM_RETURN_IF_ERROR(retval);
136 /* Get the maximum heap size */
137 heap_gcPushTempRoot(ptup, &objid);
138 retval = int_new(PM_HEAP_SIZE, &pmax);
139 if (retval != PM_RET_OK)
141 heap_gcPopTempRoot(objid);
142 return retval;
145 /* Allocate an int to hold the amount of heap available */
146 retval = int_new(heap_getAvail() - sizeof(PmInt_t), &pavail);
147 heap_gcPopTempRoot(objid);
148 PM_RETURN_IF_ERROR(retval);
150 /* Put the two heap values in the tuple */
151 ((pPmTuple_t)ptup)->val[0] = pavail;
152 ((pPmTuple_t)ptup)->val[1] = pmax;
154 /* Return the tuple on the stack */
155 NATIVE_SET_TOS(ptup);
157 return retval;
159 pass
163 # Sends the LSB of the integer out the platform's default I/O
165 def putb(b):
166 """__NATIVE__
167 uint8_t b;
168 pPmObj_t pb;
169 PmReturn_t retval;
171 pb = NATIVE_GET_LOCAL(0);
173 /* If wrong number of args, raise TypeError */
174 if (NATIVE_GET_NUM_ARGS() != 1)
176 PM_RAISE(retval, PM_RET_EX_TYPE);
177 return retval;
180 /* If arg is not an int, raise TypeError */
181 if (OBJ_GET_TYPE(pb) != OBJ_TYPE_INT)
183 PM_RAISE(retval, PM_RET_EX_TYPE);
184 return retval;
187 b = ((pPmInt_t)pb)->val & 0xFF;
188 retval = plat_putByte(b);
189 NATIVE_SET_TOS(PM_NONE);
190 return retval;
192 pass
196 # Runs the given function in a thread sharing the current global namespace
198 def runInThread(f):
199 """__NATIVE__
200 PmReturn_t retval;
201 pPmObj_t pf;
203 /* If wrong number of args, raise TypeError */
204 if (NATIVE_GET_NUM_ARGS() != 1)
206 PM_RAISE(retval, PM_RET_EX_TYPE);
207 return retval;
210 /* If arg is not a function, raise TypeError */
211 pf = NATIVE_GET_LOCAL(0);
212 if (OBJ_GET_TYPE(pf) != OBJ_TYPE_FXN)
214 PM_RAISE(retval, PM_RET_EX_TYPE);
215 return retval;
218 retval = interp_addThread((pPmFunc_t)pf);
219 NATIVE_SET_TOS(PM_NONE);
220 return retval;
222 pass
226 # Returns the number of milliseconds since the PyMite VM was initialized
228 def time():
229 """__NATIVE__
230 uint32_t t;
231 pPmObj_t pt;
232 PmReturn_t retval;
234 /* If wrong number of args, raise TypeError */
235 if (NATIVE_GET_NUM_ARGS() != 0)
237 PM_RAISE(retval, PM_RET_EX_TYPE);
238 return retval;
241 /* Get the system time (milliseconds since init) */
242 retval = plat_getMsTicks(&t);
243 PM_RETURN_IF_ERROR(retval);
246 * Raise ValueError if there is an overflow
247 * (plat_getMsTicks is unsigned; int is signed)
249 if ((int32_t)t < 0)
251 PM_RAISE(retval, PM_RET_EX_VAL);
252 return retval;
255 /* Return an int object with the time value */
256 retval = int_new((int32_t)t, &pt);
257 NATIVE_SET_TOS(pt);
258 return retval;
260 pass
264 # Waits in a busy loop for the given number of milliseconds
266 def wait(ms):
267 t = time() + ms
268 while time() < t:
269 pass
272 # :mode=c: