Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2013, 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26
pythontech 0:5868e8752d44 27 #include <stdint.h>
pythontech 0:5868e8752d44 28 #include <stdlib.h>
pythontech 0:5868e8752d44 29 #include <stddef.h>
pythontech 0:5868e8752d44 30 #include <string.h>
pythontech 0:5868e8752d44 31 #include <assert.h>
pythontech 0:5868e8752d44 32
pythontech 0:5868e8752d44 33 #include "py/binary.h"
pythontech 0:5868e8752d44 34 #include "py/smallint.h"
pythontech 0:5868e8752d44 35 #include "py/objint.h"
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 // Helpers to work with binary-encoded data
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 #ifndef alignof
pythontech 0:5868e8752d44 40 #define alignof(type) offsetof(struct { char c; type t; }, t)
pythontech 0:5868e8752d44 41 #endif
pythontech 0:5868e8752d44 42
pythontech 0:5868e8752d44 43 size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
pythontech 0:5868e8752d44 44 size_t size = 0;
pythontech 0:5868e8752d44 45 int align = 1;
pythontech 0:5868e8752d44 46 switch (struct_type) {
pythontech 0:5868e8752d44 47 case '<': case '>':
pythontech 0:5868e8752d44 48 switch (val_type) {
pythontech 0:5868e8752d44 49 case 'b': case 'B':
pythontech 0:5868e8752d44 50 size = 1; break;
pythontech 0:5868e8752d44 51 case 'h': case 'H':
pythontech 0:5868e8752d44 52 size = 2; break;
pythontech 0:5868e8752d44 53 case 'i': case 'I':
pythontech 0:5868e8752d44 54 size = 4; break;
pythontech 0:5868e8752d44 55 case 'l': case 'L':
pythontech 0:5868e8752d44 56 size = 4; break;
pythontech 0:5868e8752d44 57 case 'q': case 'Q':
pythontech 0:5868e8752d44 58 size = 8; break;
pythontech 0:5868e8752d44 59 case 'P': case 'O': case 'S':
pythontech 0:5868e8752d44 60 size = sizeof(void*); break;
pythontech 0:5868e8752d44 61 case 'f':
pythontech 0:5868e8752d44 62 size = sizeof(float); break;
pythontech 0:5868e8752d44 63 case 'd':
pythontech 0:5868e8752d44 64 size = sizeof(double); break;
pythontech 0:5868e8752d44 65 }
pythontech 0:5868e8752d44 66 break;
pythontech 0:5868e8752d44 67 case '@': {
pythontech 0:5868e8752d44 68 // TODO:
pythontech 0:5868e8752d44 69 // The simplest heuristic for alignment is to align by value
pythontech 0:5868e8752d44 70 // size, but that doesn't work for "bigger than int" types,
pythontech 0:5868e8752d44 71 // for example, long long may very well have long alignment
pythontech 0:5868e8752d44 72 // So, we introduce separate alignment handling, but having
pythontech 0:5868e8752d44 73 // formal support for that is different from actually supporting
pythontech 0:5868e8752d44 74 // particular (or any) ABI.
pythontech 0:5868e8752d44 75 switch (val_type) {
pythontech 0:5868e8752d44 76 case BYTEARRAY_TYPECODE:
pythontech 0:5868e8752d44 77 case 'b': case 'B':
pythontech 0:5868e8752d44 78 align = size = 1; break;
pythontech 0:5868e8752d44 79 case 'h': case 'H':
pythontech 0:5868e8752d44 80 align = alignof(short);
pythontech 0:5868e8752d44 81 size = sizeof(short); break;
pythontech 0:5868e8752d44 82 case 'i': case 'I':
pythontech 0:5868e8752d44 83 align = alignof(int);
pythontech 0:5868e8752d44 84 size = sizeof(int); break;
pythontech 0:5868e8752d44 85 case 'l': case 'L':
pythontech 0:5868e8752d44 86 align = alignof(long);
pythontech 0:5868e8752d44 87 size = sizeof(long); break;
pythontech 0:5868e8752d44 88 case 'q': case 'Q':
pythontech 0:5868e8752d44 89 align = alignof(long long);
pythontech 0:5868e8752d44 90 size = sizeof(long long); break;
pythontech 0:5868e8752d44 91 case 'P': case 'O': case 'S':
pythontech 0:5868e8752d44 92 align = alignof(void*);
pythontech 0:5868e8752d44 93 size = sizeof(void*); break;
pythontech 0:5868e8752d44 94 case 'f':
pythontech 0:5868e8752d44 95 align = alignof(float);
pythontech 0:5868e8752d44 96 size = sizeof(float); break;
pythontech 0:5868e8752d44 97 case 'd':
pythontech 0:5868e8752d44 98 align = alignof(double);
pythontech 0:5868e8752d44 99 size = sizeof(double); break;
pythontech 0:5868e8752d44 100 }
pythontech 0:5868e8752d44 101 }
pythontech 0:5868e8752d44 102 }
pythontech 0:5868e8752d44 103 if (palign != NULL) {
pythontech 0:5868e8752d44 104 *palign = align;
pythontech 0:5868e8752d44 105 }
pythontech 0:5868e8752d44 106 return size;
pythontech 0:5868e8752d44 107 }
pythontech 0:5868e8752d44 108
pythontech 0:5868e8752d44 109 mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
pythontech 0:5868e8752d44 110 mp_int_t val = 0;
pythontech 0:5868e8752d44 111 switch (typecode) {
pythontech 0:5868e8752d44 112 case 'b':
pythontech 0:5868e8752d44 113 val = ((signed char*)p)[index];
pythontech 0:5868e8752d44 114 break;
pythontech 0:5868e8752d44 115 case BYTEARRAY_TYPECODE:
pythontech 0:5868e8752d44 116 case 'B':
pythontech 0:5868e8752d44 117 val = ((unsigned char*)p)[index];
pythontech 0:5868e8752d44 118 break;
pythontech 0:5868e8752d44 119 case 'h':
pythontech 0:5868e8752d44 120 val = ((short*)p)[index];
pythontech 0:5868e8752d44 121 break;
pythontech 0:5868e8752d44 122 case 'H':
pythontech 0:5868e8752d44 123 val = ((unsigned short*)p)[index];
pythontech 0:5868e8752d44 124 break;
pythontech 0:5868e8752d44 125 case 'i':
pythontech 0:5868e8752d44 126 return mp_obj_new_int(((int*)p)[index]);
pythontech 0:5868e8752d44 127 case 'I':
pythontech 0:5868e8752d44 128 return mp_obj_new_int_from_uint(((unsigned int*)p)[index]);
pythontech 0:5868e8752d44 129 case 'l':
pythontech 0:5868e8752d44 130 return mp_obj_new_int(((long*)p)[index]);
pythontech 0:5868e8752d44 131 case 'L':
pythontech 0:5868e8752d44 132 return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
pythontech 0:5868e8752d44 133 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
pythontech 0:5868e8752d44 134 case 'q':
pythontech 0:5868e8752d44 135 return mp_obj_new_int_from_ll(((long long*)p)[index]);
pythontech 0:5868e8752d44 136 case 'Q':
pythontech 0:5868e8752d44 137 return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
pythontech 0:5868e8752d44 138 #endif
pythontech 0:5868e8752d44 139 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 140 case 'f':
pythontech 0:5868e8752d44 141 return mp_obj_new_float(((float*)p)[index]);
pythontech 0:5868e8752d44 142 case 'd':
pythontech 0:5868e8752d44 143 return mp_obj_new_float(((double*)p)[index]);
pythontech 0:5868e8752d44 144 #endif
pythontech 0:5868e8752d44 145 // Extension to CPython: array of objects
pythontech 0:5868e8752d44 146 case 'O':
pythontech 0:5868e8752d44 147 return ((mp_obj_t*)p)[index];
pythontech 0:5868e8752d44 148 // Extension to CPython: array of pointers
pythontech 0:5868e8752d44 149 case 'P':
pythontech 0:5868e8752d44 150 return mp_obj_new_int((mp_int_t)(uintptr_t)((void**)p)[index]);
pythontech 0:5868e8752d44 151 }
pythontech 0:5868e8752d44 152 return MP_OBJ_NEW_SMALL_INT(val);
pythontech 0:5868e8752d44 153 }
pythontech 0:5868e8752d44 154
pythontech 0:5868e8752d44 155 // The long long type is guaranteed to hold at least 64 bits, and size is at
pythontech 0:5868e8752d44 156 // most 8 (for q and Q), so we will always be able to parse the given data
pythontech 0:5868e8752d44 157 // and fit it into a long long.
pythontech 0:5868e8752d44 158 long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src) {
pythontech 0:5868e8752d44 159 int delta;
pythontech 0:5868e8752d44 160 if (!big_endian) {
pythontech 0:5868e8752d44 161 delta = -1;
pythontech 0:5868e8752d44 162 src += size - 1;
pythontech 0:5868e8752d44 163 } else {
pythontech 0:5868e8752d44 164 delta = 1;
pythontech 0:5868e8752d44 165 }
pythontech 0:5868e8752d44 166
pythontech 0:5868e8752d44 167 long long val = 0;
pythontech 0:5868e8752d44 168 if (is_signed && *src & 0x80) {
pythontech 0:5868e8752d44 169 val = -1;
pythontech 0:5868e8752d44 170 }
pythontech 0:5868e8752d44 171 for (uint i = 0; i < size; i++) {
pythontech 0:5868e8752d44 172 val <<= 8;
pythontech 0:5868e8752d44 173 val |= *src;
pythontech 0:5868e8752d44 174 src += delta;
pythontech 0:5868e8752d44 175 }
pythontech 0:5868e8752d44 176
pythontech 0:5868e8752d44 177 return val;
pythontech 0:5868e8752d44 178 }
pythontech 0:5868e8752d44 179
pythontech 0:5868e8752d44 180 #define is_signed(typecode) (typecode > 'Z')
pythontech 0:5868e8752d44 181 mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
pythontech 0:5868e8752d44 182 byte *p = *ptr;
pythontech 0:5868e8752d44 183 mp_uint_t align;
pythontech 0:5868e8752d44 184
pythontech 0:5868e8752d44 185 size_t size = mp_binary_get_size(struct_type, val_type, &align);
pythontech 0:5868e8752d44 186 if (struct_type == '@') {
pythontech 0:5868e8752d44 187 // Make pointer aligned
pythontech 0:5868e8752d44 188 p = (byte*)MP_ALIGN(p, (size_t)align);
pythontech 0:5868e8752d44 189 #if MP_ENDIANNESS_LITTLE
pythontech 0:5868e8752d44 190 struct_type = '<';
pythontech 0:5868e8752d44 191 #else
pythontech 0:5868e8752d44 192 struct_type = '>';
pythontech 0:5868e8752d44 193 #endif
pythontech 0:5868e8752d44 194 }
pythontech 0:5868e8752d44 195 *ptr = p + size;
pythontech 0:5868e8752d44 196
pythontech 0:5868e8752d44 197 long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
pythontech 0:5868e8752d44 198
pythontech 0:5868e8752d44 199 if (val_type == 'O') {
pythontech 0:5868e8752d44 200 return (mp_obj_t)(mp_uint_t)val;
pythontech 0:5868e8752d44 201 } else if (val_type == 'S') {
pythontech 0:5868e8752d44 202 const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val;
pythontech 0:5868e8752d44 203 return mp_obj_new_str(s_val, strlen(s_val), false);
pythontech 0:5868e8752d44 204 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 205 } else if (val_type == 'f') {
pythontech 0:5868e8752d44 206 union { uint32_t i; float f; } fpu = {val};
pythontech 0:5868e8752d44 207 return mp_obj_new_float(fpu.f);
pythontech 0:5868e8752d44 208 } else if (val_type == 'd') {
pythontech 0:5868e8752d44 209 union { uint64_t i; double f; } fpu = {val};
pythontech 0:5868e8752d44 210 return mp_obj_new_float(fpu.f);
pythontech 0:5868e8752d44 211 #endif
pythontech 0:5868e8752d44 212 } else if (is_signed(val_type)) {
pythontech 0:5868e8752d44 213 if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
pythontech 0:5868e8752d44 214 return mp_obj_new_int((mp_int_t)val);
pythontech 0:5868e8752d44 215 } else {
pythontech 0:5868e8752d44 216 return mp_obj_new_int_from_ll(val);
pythontech 0:5868e8752d44 217 }
pythontech 0:5868e8752d44 218 } else {
pythontech 0:5868e8752d44 219 if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
pythontech 0:5868e8752d44 220 return mp_obj_new_int_from_uint((mp_uint_t)val);
pythontech 0:5868e8752d44 221 } else {
pythontech 0:5868e8752d44 222 return mp_obj_new_int_from_ull(val);
pythontech 0:5868e8752d44 223 }
pythontech 0:5868e8752d44 224 }
pythontech 0:5868e8752d44 225 }
pythontech 0:5868e8752d44 226
pythontech 0:5868e8752d44 227 void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
pythontech 0:5868e8752d44 228 if (MP_ENDIANNESS_LITTLE && !big_endian) {
pythontech 0:5868e8752d44 229 memcpy(dest, &val, val_sz);
pythontech 0:5868e8752d44 230 } else if (MP_ENDIANNESS_BIG && big_endian) {
pythontech 0:5868e8752d44 231 // only copy the least-significant val_sz bytes
pythontech 0:5868e8752d44 232 memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
pythontech 0:5868e8752d44 233 } else {
pythontech 0:5868e8752d44 234 const byte *src;
pythontech 0:5868e8752d44 235 if (MP_ENDIANNESS_LITTLE) {
pythontech 0:5868e8752d44 236 src = (const byte*)&val + val_sz;
pythontech 0:5868e8752d44 237 } else {
pythontech 0:5868e8752d44 238 src = (const byte*)&val + sizeof(mp_uint_t);
pythontech 0:5868e8752d44 239 }
pythontech 0:5868e8752d44 240 while (val_sz--) {
pythontech 0:5868e8752d44 241 *dest++ = *--src;
pythontech 0:5868e8752d44 242 }
pythontech 0:5868e8752d44 243 }
pythontech 0:5868e8752d44 244 }
pythontech 0:5868e8752d44 245
pythontech 0:5868e8752d44 246 void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
pythontech 0:5868e8752d44 247 byte *p = *ptr;
pythontech 0:5868e8752d44 248 mp_uint_t align;
pythontech 0:5868e8752d44 249
pythontech 0:5868e8752d44 250 size_t size = mp_binary_get_size(struct_type, val_type, &align);
pythontech 0:5868e8752d44 251 if (struct_type == '@') {
pythontech 0:5868e8752d44 252 // Make pointer aligned
pythontech 0:5868e8752d44 253 p = (byte*)MP_ALIGN(p, (size_t)align);
pythontech 0:5868e8752d44 254 if (MP_ENDIANNESS_LITTLE) {
pythontech 0:5868e8752d44 255 struct_type = '<';
pythontech 0:5868e8752d44 256 } else {
pythontech 0:5868e8752d44 257 struct_type = '>';
pythontech 0:5868e8752d44 258 }
pythontech 0:5868e8752d44 259 }
pythontech 0:5868e8752d44 260 *ptr = p + size;
pythontech 0:5868e8752d44 261
pythontech 0:5868e8752d44 262 mp_uint_t val;
pythontech 0:5868e8752d44 263 switch (val_type) {
pythontech 0:5868e8752d44 264 case 'O':
pythontech 0:5868e8752d44 265 val = (mp_uint_t)val_in;
pythontech 0:5868e8752d44 266 break;
pythontech 0:5868e8752d44 267 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 268 case 'f': {
pythontech 0:5868e8752d44 269 union { uint32_t i; float f; } fp_sp;
pythontech 0:5868e8752d44 270 fp_sp.f = mp_obj_get_float(val_in);
pythontech 0:5868e8752d44 271 val = fp_sp.i;
pythontech 0:5868e8752d44 272 break;
pythontech 0:5868e8752d44 273 }
pythontech 0:5868e8752d44 274 case 'd': {
pythontech 0:5868e8752d44 275 union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp;
pythontech 0:5868e8752d44 276 fp_dp.f = mp_obj_get_float(val_in);
pythontech 0:5868e8752d44 277 if (BYTES_PER_WORD == 8) {
pythontech 0:5868e8752d44 278 val = fp_dp.i64;
pythontech 0:5868e8752d44 279 } else {
pythontech 0:5868e8752d44 280 int be = struct_type == '>';
pythontech 0:5868e8752d44 281 mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
pythontech 0:5868e8752d44 282 p += sizeof(uint32_t);
pythontech 0:5868e8752d44 283 val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
pythontech 0:5868e8752d44 284 }
pythontech 0:5868e8752d44 285 break;
pythontech 0:5868e8752d44 286 }
pythontech 0:5868e8752d44 287 #endif
pythontech 0:5868e8752d44 288 default:
pythontech 0:5868e8752d44 289 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
pythontech 0:5868e8752d44 290 if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
pythontech 0:5868e8752d44 291 mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
pythontech 0:5868e8752d44 292 return;
pythontech 0:5868e8752d44 293 } else
pythontech 0:5868e8752d44 294 #endif
pythontech 0:5868e8752d44 295 {
pythontech 0:5868e8752d44 296 val = mp_obj_get_int(val_in);
pythontech 0:5868e8752d44 297 // sign extend if needed
pythontech 0:5868e8752d44 298 if (BYTES_PER_WORD < 8 && size > sizeof(val) && is_signed(val_type) && (mp_int_t)val < 0) {
pythontech 0:5868e8752d44 299 memset(p + sizeof(val), 0xff, size - sizeof(val));
pythontech 0:5868e8752d44 300 }
pythontech 0:5868e8752d44 301 }
pythontech 0:5868e8752d44 302 }
pythontech 0:5868e8752d44 303
pythontech 0:5868e8752d44 304 mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
pythontech 0:5868e8752d44 305 }
pythontech 0:5868e8752d44 306
pythontech 0:5868e8752d44 307 void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
pythontech 0:5868e8752d44 308 switch (typecode) {
pythontech 0:5868e8752d44 309 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 310 case 'f':
pythontech 0:5868e8752d44 311 ((float*)p)[index] = mp_obj_get_float(val_in);
pythontech 0:5868e8752d44 312 break;
pythontech 0:5868e8752d44 313 case 'd':
pythontech 0:5868e8752d44 314 ((double*)p)[index] = mp_obj_get_float(val_in);
pythontech 0:5868e8752d44 315 break;
pythontech 0:5868e8752d44 316 #endif
pythontech 0:5868e8752d44 317 // Extension to CPython: array of objects
pythontech 0:5868e8752d44 318 case 'O':
pythontech 0:5868e8752d44 319 ((mp_obj_t*)p)[index] = val_in;
pythontech 0:5868e8752d44 320 break;
pythontech 0:5868e8752d44 321 default:
pythontech 0:5868e8752d44 322 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
pythontech 0:5868e8752d44 323 if ((typecode | 0x20) == 'q' && MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
pythontech 0:5868e8752d44 324 mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
pythontech 0:5868e8752d44 325 sizeof(long long), (byte*)&((long long*)p)[index]);
pythontech 0:5868e8752d44 326 return;
pythontech 0:5868e8752d44 327 }
pythontech 0:5868e8752d44 328 #endif
pythontech 0:5868e8752d44 329 mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
pythontech 0:5868e8752d44 330 }
pythontech 0:5868e8752d44 331 }
pythontech 0:5868e8752d44 332
pythontech 0:5868e8752d44 333 void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val) {
pythontech 0:5868e8752d44 334 switch (typecode) {
pythontech 0:5868e8752d44 335 case 'b':
pythontech 0:5868e8752d44 336 ((signed char*)p)[index] = val;
pythontech 0:5868e8752d44 337 break;
pythontech 0:5868e8752d44 338 case BYTEARRAY_TYPECODE:
pythontech 0:5868e8752d44 339 case 'B':
pythontech 0:5868e8752d44 340 ((unsigned char*)p)[index] = val;
pythontech 0:5868e8752d44 341 break;
pythontech 0:5868e8752d44 342 case 'h':
pythontech 0:5868e8752d44 343 ((short*)p)[index] = val;
pythontech 0:5868e8752d44 344 break;
pythontech 0:5868e8752d44 345 case 'H':
pythontech 0:5868e8752d44 346 ((unsigned short*)p)[index] = val;
pythontech 0:5868e8752d44 347 break;
pythontech 0:5868e8752d44 348 case 'i':
pythontech 0:5868e8752d44 349 ((int*)p)[index] = val;
pythontech 0:5868e8752d44 350 break;
pythontech 0:5868e8752d44 351 case 'I':
pythontech 0:5868e8752d44 352 ((unsigned int*)p)[index] = val;
pythontech 0:5868e8752d44 353 break;
pythontech 0:5868e8752d44 354 case 'l':
pythontech 0:5868e8752d44 355 ((long*)p)[index] = val;
pythontech 0:5868e8752d44 356 break;
pythontech 0:5868e8752d44 357 case 'L':
pythontech 0:5868e8752d44 358 ((unsigned long*)p)[index] = val;
pythontech 0:5868e8752d44 359 break;
pythontech 0:5868e8752d44 360 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
pythontech 0:5868e8752d44 361 case 'q':
pythontech 0:5868e8752d44 362 ((long long*)p)[index] = val;
pythontech 0:5868e8752d44 363 case 'Q':
pythontech 0:5868e8752d44 364 ((unsigned long long*)p)[index] = val;
pythontech 0:5868e8752d44 365 break;
pythontech 0:5868e8752d44 366 #endif
pythontech 0:5868e8752d44 367 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 368 case 'f':
pythontech 0:5868e8752d44 369 ((float*)p)[index] = val;
pythontech 0:5868e8752d44 370 break;
pythontech 0:5868e8752d44 371 case 'd':
pythontech 0:5868e8752d44 372 ((double*)p)[index] = val;
pythontech 0:5868e8752d44 373 break;
pythontech 0:5868e8752d44 374 #endif
pythontech 0:5868e8752d44 375 // Extension to CPython: array of pointers
pythontech 0:5868e8752d44 376 case 'P':
pythontech 0:5868e8752d44 377 ((void**)p)[index] = (void*)(uintptr_t)val;
pythontech 0:5868e8752d44 378 }
pythontech 0:5868e8752d44 379 }