Commits

Anonymous committed d02a0bb Draft

add function python object return

  • Participants
  • Parent commits 7462539

Comments (0)

Files changed (1)

src/platform/stm32f4/wire.py

     PmReturn_t retval = PM_RET_OK;
 
     pPmObj_t paddress;
+    int32_t naddress;
     pPmObj_t pdirection;
+    int32_t ndirection;
 
     uint8_t address;
     uint8_t direction;
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
     }
-    address = (uint8_t)((pPmInt_t)paddress)->val;
+
+    /* Raise ValueError if arg is not int within range(256) */
+    naddress = ((pPmInt_t)pn)->val;
+    if ((naddress < 0) || (naddress > 255))
+    {
+        PM_RAISE(retval, PM_RET_EX_VAL);
+        return retval;
+    }
+    address = (uint8_t)naddress;
 
     pdirection = NATIVE_GET_LOCAL(1);
     if (OBJ_GET_TYPE(pdirection) != OBJ_TYPE_INT)
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
     }
-    direction = (uint8_t)((pPmInt_t)pdirection)->val;
+    ndirection = ((pPmInt_t)pdirection)->val;
+    if ((ndirection < 0) || (ndirection > 255))
+    {
+        PM_RAISE(retval, PM_RET_EX_VAL);
+        return retval;
+    }
+    direction = (uint8_t)ndirection;
 
 	// wait until I2C1 is not busy anymore
 	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
     """__NATIVE__
     PmReturn_t retval = PM_RET_OK;
     pPmObj_t pdat;
+    int32_t ndat;
     uint8_t data;
 
     if(NATIVE_GET_NUM_ARGS() != 1)
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
     }
-    data = (uint8_t)((pPmInt_t)pdat)->val;
+    ndat = ((pPmInt_t)pdat)->val
+    if ((ndat < 0) || (ndat > 255))
+    {
+        PM_RAISE(retval, PM_RET_EX_VAL);
+        return retval;
+    }
+    data = (uint8_t)ndat;
  
 	I2C_SendData(I2C1, data);
 	// wait for I2C1 EV8_2 --> byte has been transmitted
 # and acknowledges the byte (requests another byte)
 def I2C_read_ack():
     """__NATIVE__
+    PmReturn_t retval = PM_RET_OK;
+    pPmObj_t p0;
+
 	// enable acknowledge of recieved data
 	I2C_AcknowledgeConfig(I2C1, ENABLE);
 	// wait until one byte has been received
 	while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
 	// read data from I2C data register and return data byte
 	uint8_t data = I2C_ReceiveData(I2C1);
-	return data;
+
+    // build python output
+    retval = int_new( *(uint32_t *)(data), &p0);
+    NATIVE_SET_TOS(p0);
+    return retval;
     """
 
 # This function reads one byte from the slave device
 # and doesn't acknowledge the recieved data 
 def I2C_read_nack():
     """__NATIVE__
+	PmReturn_t retval = PM_RET_OK;
+	pPmObj_t p0;
+
 	// disabe acknowledge of received data
 	I2C_AcknowledgeConfig(I2C1, DISABLE);
 	// wait until one byte has been received
 	while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
 	// read data from I2C data register and return data byte
 	uint8_t data = I2C_ReceiveData(I2C1);
-	return data;
+
+    // build python output
+	int_new( *(uint32_t *)(data) , &p0);
+	NATIVE_SET_TOS(p0);
+    return retval;
     """
     pass
 
 		I2C_stop()
         print received_data
 
+# :mode=c: