mmb.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 # MMB Access Module
00015 #
00016 # Provides access to the MegaMiniBoard103
00017 # by wrapping libmmb103 functions.
00018 # Use of this module requires that mmb.py
00019 # be run through the pmImgCreator,
00020 # the output from that be compiled
00021 # and linked with libmmb103.a and the PyMite system.
00022 #
00023 # USAGE
00024 # -----
00025 #
00026 # import mmb
00027 # mmb.init()
00028 #
00029 
00030 """__NATIVE__
00031 #include <stdio.h>
00032 #include <avr/io.h>
00033 #include <util/delay.h>
00034 #include "libmmb103.h"
00035 """
00036 
00037 
00038 def adc_get(c):
00039     """__NATIVE__
00040     /* The arg is the ADC channel to read */
00041     pPmObj_t pc = C_NULL;
00042     pPmObj_t pr = C_NULL;
00043     uint8_t chan;
00044     PmReturn_t retval;
00045 
00046     /* If wrong number of args, throw type exception */
00047     if (NATIVE_GET_NUM_ARGS() != 1)
00048     {
00049         PM_RAISE(retval, PM_RET_EX_TYPE);
00050         return retval;
00051     }
00052 
00053     /* Get arg, throw type exception if it's not an int */
00054     pc = NATIVE_GET_LOCAL(0);
00055     if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
00056     {
00057         PM_RAISE(retval, PM_RET_EX_TYPE);
00058         return retval;
00059     }
00060 
00061     /* Get the channel number */
00062     chan = (uint8_t)(((pPmInt_t)pc)->val & 0x03);
00063 
00064     /* Return new int from the conversion result on the stack */
00065     retval = int_new(mmb_adc_get(chan), &pr);
00066     NATIVE_SET_TOS(pr);
00067 
00068     return retval;
00069     """
00070     pass
00071 
00072 
00073 #def beep(f, ms):
00074 #    """__NATIVE__
00075 #    pPmObj_t pf = C_NULL;
00076 #    pPmObj_t pms = C_NULL;
00077 #    int16_t f;
00078 #    int16_t ms;
00079 #    PmReturn_t retval;
00080 #
00081 #    /* If wrong number of args, throw type exception */
00082 #    if (NATIVE_GET_NUM_ARGS() != 2)
00083 #    {
00084 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00085 #        return retval;
00086 #    }
00087 #
00088 #    /* Get the args, throw exception if needed */
00089 #    pf = NATIVE_GET_LOCAL(0);
00090 #    pms = NATIVE_GET_LOCAL(1);
00091 #    if ((OBJ_GET_TYPE(pf) != OBJ_TYPE_INT) ||
00092 #        (OBJ_GET_TYPE(pms) != OBJ_TYPE_INT))
00093 #    {
00094 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00095 #        return retval;
00096 #    }
00097 #
00098 #    /* Get frequency and duration values */
00099 #    f = (int16_t)((pPmInt_t)pf)->val;
00100 #    ms = (int16_t)((pPmInt_t)pms)->val;
00101 #
00102 #    /* Call mmb's beep fxn */
00103 #    mmb_beep(f, ms);
00104 #
00105 #    /* Return none obj on stack */
00106 #    NATIVE_SET_TOS(PM_NONE);
00107 #
00108 #    return PM_RET_OK;
00109 #    """
00110 #    pass
00111 
00112 
00113 #def dig_get(c):
00114 #    """__NATIVE__
00115 #    pPmObj_t pc = C_NULL;
00116 #    pPmObj_t pr = C_NULL;
00117 #    int8_t chan;
00118 #    PmReturn_t retval;
00119 #
00120 #    /* If wrong number of args, throw type exception */
00121 #    if (NATIVE_GET_NUM_ARGS() != 1)
00122 #    {
00123 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00124 #        return retval;
00125 #    }
00126 #
00127 #    /* Get the arg, throw exception if needed */
00128 #    pc = NATIVE_GET_LOCAL(0);
00129 #    if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
00130 #    {
00131 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00132 #        return retval;
00133 #    }
00134 #
00135 #    /* Get the channel value */
00136 #    chan = (int8_t)(((pPmInt_t)pc)->val & (int8_t)0x03);
00137 #
00138 #    /* Return new int with digital value on stack */
00139 #    retval = int_new(mmb_dig_get(chan), &pr);
00140 #    NATIVE_SET_TOS(pr);
00141 #
00142 #    return retval;
00143 #    """
00144 #    pass
00145 
00146 
00147 #def dig_get_byte():
00148 #    """__NATIVE__
00149 #    pPmObj_t pr = C_NULL;
00150 #    PmReturn_t retval;
00151 #
00152 #    /* If wrong number of args, throw type exception */
00153 #    if (NATIVE_GET_NUM_ARGS() != 0)
00154 #    {
00155 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00156 #        return retval;
00157 #    }
00158 #
00159 #    /* Return new int from DIG port value on stack */
00160 #    retval = int_new(mmb_dig_get_byte(), &pr);
00161 #    NATIVE_SET_TOS(pr);
00162 #
00163 #    return retval;
00164 #    """
00165 #    pass
00166 
00167 
00168 def dip_get(c):
00169     """__NATIVE__
00170     pPmObj_t pc = C_NULL;
00171     pPmObj_t pr = C_NULL;
00172     int8_t chan;
00173     PmReturn_t retval;
00174 
00175     /* If no args, get all dip switches as one byte */
00176     if (NATIVE_GET_NUM_ARGS() == 0)
00177     {
00178         retval = int_new(mmb_dip_get_byte(), &pr);
00179     }
00180 
00181     /* If one arg, get the designated dip switch */
00182     else if (NATIVE_GET_NUM_ARGS() == 1)
00183     {
00184         /* Raise TypeError if arg is not an int */
00185         pc = NATIVE_GET_LOCAL(0);
00186         if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
00187         {
00188             PM_RAISE(retval, PM_RET_EX_TYPE);
00189             return retval;
00190         }
00191 
00192         /* Get the channel number and read that dip switch */
00193         chan = (int8_t)(((pPmInt_t)pc)->val & (int8_t)0x03);
00194         retval = int_new(mmb_dip_get(chan), &pr);
00195     }
00196 
00197     /* Raise TypeError if wrong number of args */
00198     else
00199     {
00200         PM_RAISE(retval, PM_RET_EX_TYPE);
00201     }
00202 
00203     NATIVE_SET_TOS(pr);
00204     return retval;
00205     """
00206     pass
00207 
00208 
00209 #def init():
00210 #    """__NATIVE__
00211 #    PmReturn_t retval;
00212 #
00213 #    /* If wrong number of args, throw type exception */
00214 #    if (NATIVE_GET_NUM_ARGS() != 0)
00215 #    {
00216 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00217 #        return retval;
00218 #    }
00219 #
00220 #    /* Init board */
00221 #    mmb_init(BAUD_19200, ADC_CK_DIV_128, PWM_CK_DIV_8, 4, 20);
00222 #
00223 #    /* Return none obj on stack */
00224 #    NATIVE_SET_TOS(PM_NONE);
00225 #
00226 #    return PM_RET_OK;
00227 #    """
00228 #    pass
00229 
00230 
00231 #def lcd_cls():
00232 #    """__NATIVE__
00233 #    PmReturn_t retval;
00234 #
00235 #    /* If wrong number of args, throw type exception */
00236 #    if (NATIVE_GET_NUM_ARGS() != 0)
00237 #    {
00238 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00239 #        return retval;
00240 #    }
00241 #
00242 #    /* Clear the LCD screen */
00243 #    mmb_lcd_clr_scr();
00244 #
00245 #    /* Return none obj on stack */
00246 #    NATIVE_SET_TOS(PM_NONE);
00247 #
00248 #    return PM_RET_OK;
00249 #    """
00250 #    pass
00251 
00252 
00253 def lcd_print(ps):
00254     """__NATIVE__
00255     pPmObj_t ps = C_NULL;
00256     uint8_t *s = C_NULL;
00257     PmReturn_t retval;
00258 
00259     /* If wrong number of args, throw type exception */
00260     if (NATIVE_GET_NUM_ARGS() != 1)
00261     {
00262         PM_RAISE(retval, PM_RET_EX_TYPE);
00263         return retval;
00264     }
00265 
00266     /* Get the arg, throw type exception if needed */
00267     ps = NATIVE_GET_LOCAL(0);
00268     if (OBJ_GET_TYPE(ps) != OBJ_TYPE_STR)
00269     {
00270         PM_RAISE(retval, PM_RET_EX_TYPE);
00271         return retval;
00272     }
00273 
00274     /* Get a pointer to the string */
00275     s = ((pPmString_t)ps)->val;
00276 
00277     /* Print the string on the mmb's lcd */
00278     mmb_lcd_print_str(s);
00279 
00280     /* Return none obj on stack */
00281     NATIVE_SET_TOS(PM_NONE);
00282 
00283     return PM_RET_OK;
00284     """
00285     pass
00286 
00287 
00288 def lcd_set_line(n):
00289     """__NATIVE__
00290     pPmObj_t pn = C_NULL;
00291     uint8_t n;
00292     PmReturn_t retval;
00293 
00294     /* If wrong number of args, throw type exception */
00295     if (NATIVE_GET_NUM_ARGS() != 1)
00296     {
00297         PM_RAISE(retval, PM_RET_EX_TYPE);
00298         return retval;
00299     }
00300 
00301     /* Get the arg, throw type exception if needed */
00302     pn = NATIVE_GET_LOCAL(0);
00303     if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00304     {
00305         PM_RAISE(retval, PM_RET_EX_TYPE);
00306         return retval;
00307     }
00308 
00309     /* Get the line number and set the cursor to that line */
00310     n = (uint8_t)((pPmInt_t)pn)->val;
00311     mmb_lcd_set_cursor(n, 0);
00312 
00313     /* Return none obj on stack */
00314     NATIVE_SET_TOS(PM_NONE);
00315 
00316     return PM_RET_OK;
00317     """
00318     pass
00319 
00320 
00321 #def lcd_title_screen():
00322 #    """__NATIVE__
00323 #    PmReturn_t retval;
00324 #
00325 #    /* If wrong number of args, throw type exception */
00326 #    if (NATIVE_GET_NUM_ARGS() != 0)
00327 #    {
00328 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00329 #        return retval;
00330 #    }
00331 #
00332 #    mmb_lcd_title_screen();
00333 #
00334 #    /* Return none obj on stack */
00335 #    NATIVE_SET_TOS(PM_NONE);
00336 #
00337 #    return PM_RET_OK;
00338 #    """
00339 #    pass
00340 
00341 
00342 def set_pwm_a(n):
00343     """__NATIVE__
00344     pPmObj_t pn = C_NULL;
00345     int16_t duty;
00346     PmReturn_t retval;
00347 
00348     /* If wrong number of args, throw type exception */
00349     if (NATIVE_GET_NUM_ARGS() != 1)
00350     {
00351         PM_RAISE(retval, PM_RET_EX_TYPE);
00352         return retval;
00353     }
00354 
00355     /* Get the arg, throw exception if needed */
00356     pn = NATIVE_GET_LOCAL(0);
00357     if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00358     {
00359         PM_RAISE(retval, PM_RET_EX_TYPE);
00360         return retval;
00361     }
00362 
00363     /* Get the duty cycle value */
00364     duty = (int16_t)((pPmInt_t)pn)->val;
00365 
00366     mmb_set_mota_pwm(duty);
00367 
00368     /* Return none obj on stack */
00369     NATIVE_SET_TOS(PM_NONE);
00370 
00371     return PM_RET_OK;
00372     """
00373     pass
00374 
00375 
00376 #def set_pwm_b(n):
00377 #    """__NATIVE__
00378 #    pPmObj_t pn = C_NULL;
00379 #    int16_t duty;
00380 #    PmReturn_t retval;
00381 #
00382 #    /* If wrong number of args, throw type exception */
00383 #    if (NATIVE_GET_NUM_ARGS() != 1)
00384 #    {
00385 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00386 #        return retval;
00387 #    }
00388 #
00389 #    /* Get the arg, throw type exception if needed */
00390 #    pn = NATIVE_GET_LOCAL(0);
00391 #    if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00392 #    {
00393 #        PM_RAISE(retval, PM_RET_EX_TYPE);
00394 #        return retval;
00395 #    }
00396 #
00397 #    /* Get the duty cycle value */
00398 #    duty = (int16_t)((pPmInt_t)pn)->val;
00399 #
00400 #    mmb_set_motb_pwm(duty);
00401 #
00402 #    /* Return none obj on stack */
00403 #    NATIVE_SET_TOS(PM_NONE);
00404 #
00405 #    return PM_RET_OK;
00406 #    """
00407 #    pass
00408 
00409 
00410 def sleepms(ms):
00411     """__NATIVE__
00412     pPmObj_t pms = C_NULL;
00413     int16_t ms;
00414     PmReturn_t retval;
00415 
00416     /* If wrong number of args, throw type exception */
00417     if (NATIVE_GET_NUM_ARGS() != 1)
00418     {
00419         PM_RAISE(retval, PM_RET_EX_TYPE);
00420         return retval;
00421     }
00422 
00423     /* Get the arg, throw type exception if needed */
00424     pms = NATIVE_GET_LOCAL(0);
00425     if (OBJ_GET_TYPE(pms) != OBJ_TYPE_INT)
00426     {
00427         PM_RAISE(retval, PM_RET_EX_TYPE);
00428         return retval;
00429     }
00430 
00431     /* Get the delay value, apply API limits and call delay fxn */
00432     ms = (int16_t)((pPmInt_t)pms)->val;
00433     if (ms < 0) ms = 0;
00434     else if (ms > 65) ms = 65;
00435     _delay_ms(ms);
00436 
00437     /* Return none obj on stack */
00438     NATIVE_SET_TOS(PM_NONE);
00439 
00440     return PM_RET_OK;
00441     """
00442     pass
00443 
00444 # :mode=c:

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