main.py

00001 # This file is Copyright 2006, 2007, 2009 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 # 
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING up one directory from this.
00012 
00013 #
00014 # This is a sample application that calls native functions.
00015 #
00016 
00017 
00018 """__NATIVE__
00019 #include <stdio.h>
00020 #include <avr/io.h>
00021 #include <util/delay.h>
00022 #include "libmmb103.h"
00023 """
00024 
00025 
00026 #
00027 # Do this at the very start to show that PyMite is running
00028 #
00029 if False:
00030     import sys
00031     print sys.heap() # (1762,3328)
00032 
00033     s = "It's alive!"
00034     print s
00035 
00036     import mmb
00037     mmb.lcd_print("PyMite on MMB103")
00038 
00039     while True:
00040         mmb.lcd_set_line(1)
00041         if mmb.dip_get(1):
00042             mmb.lcd_print("dip1 = ON ")
00043         else:
00044             mmb.lcd_print("dip1 = OFF")
00045         mmb.sleepms(50)
00046         mmb.sleepms(50)
00047         mmb.sleepms(50)
00048         mmb.sleepms(50)
00049         mmb.sleepms(50)
00050 
00051 
00052 #
00053 # Duplicating ipm functions here so I do not have to import ipm (spend heap)
00054 #
00055 
00056 #
00057 # Receives an image over the platform's standard connection.
00058 # Returns the image in a string object
00059 #
00060 def _getImg():
00061     """__NATIVE__
00062     PmReturn_t retval;
00063     uint8_t imgType;
00064     uint16_t imgSize;
00065     uint8_t *pchunk;
00066     pPmString_t pimg;
00067     uint16_t i;
00068     uint8_t b;
00069 
00070     /* Get the image type (skip any trash at beginning) */
00071     do
00072     {
00073         retval = plat_getByte(&imgType);
00074         PM_RETURN_IF_ERROR(retval);
00075     }
00076     while (imgType != OBJ_TYPE_CIM);
00077 
00078     /* Quit if a code image type was not received */
00079     if (imgType != OBJ_TYPE_CIM)
00080     {
00081         PM_RAISE(retval, PM_RET_EX_STOP);
00082         return retval;
00083     }
00084 
00085     /* Get the image size (little endien) */
00086     retval = plat_getByte(&b);
00087     PM_RETURN_IF_ERROR(retval);
00088     imgSize = b;
00089     retval = plat_getByte(&b);
00090     PM_RETURN_IF_ERROR(retval);
00091     imgSize |= (b << 8);
00092 
00093     /* Get space for String obj */
00094     retval = heap_getChunk(sizeof(PmString_t) + imgSize, &pchunk);
00095     PM_RETURN_IF_ERROR(retval);
00096     pimg = (pPmString_t)pchunk;
00097 
00098     /* Set the string object's fields */
00099     OBJ_SET_TYPE(pimg, OBJ_TYPE_STR);
00100     pimg->length = imgSize;
00101 
00102     /* Start the image with the bytes that have already been received */
00103     i = 0;
00104     pimg->val[i++] = imgType;
00105     pimg->val[i++] = imgSize & 0xFF;
00106     pimg->val[i++] = (imgSize >> 8) & 0xFF;
00107 
00108     /* Get the remaining bytes in the image */
00109     for(; i < imgSize; i++)
00110     {
00111         retval = plat_getByte(&b);
00112         PM_RETURN_IF_ERROR(retval);
00113 
00114         pimg->val[i] = b;
00115     }
00116 
00117     /* Return the image as a string object on the stack */
00118     NATIVE_SET_TOS((pPmObj_t)pimg);
00119     return retval;
00120     """
00121     pass
00122 
00123 
00124 #
00125 # Runs the target device-side interactive session.
00126 #
00127 #def ipm():
00128 g = {}
00129 while 1:
00130     # Wait for a code image, make a code object from it
00131     # and evaluate the code object.
00132     rv = eval(Co(_getImg()), g)
00133 
00134     # Send a byte to indicate completion of evaluation
00135     print '\x04',
00136 
00137 
00138 
00139 # :mode=c:

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9