Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot-dev-mbed5-deprecated ISL29011
Fork of mdot-examples by
Revision 5:c9ab5062cfc3, committed 2017-12-01
- Comitter:
- SDesign2018
- Date:
- Fri Dec 01 20:22:20 2017 +0000
- Parent:
- 4:b0ce6385d008
- Child:
- 6:3b41238872a8
- Commit message:
- Added option 8 and 9; 8 to read SPI and 9 to write though not calling function yet;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/inc/itoa.h Fri Dec 01 20:22:20 2017 +0000 @@ -0,0 +1,10 @@ +#ifndef __ITOA_H +#define __ITOA_H + +#include <string.h> + +char* itoa(int num, char* str, int base); + +void reverse(char *s); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/itoa.cpp Fri Dec 01 20:22:20 2017 +0000
@@ -0,0 +1,60 @@
+#include <string.h>
+#include "itoa.h"
+using namespace std;
+
+// Implementation of itoa()
+char* itoa(int num, char* str, int base)
+{
+ int i = 0;
+ bool isNegative = false;
+
+ /* Handle 0 explicitely, otherwise empty string is printed for 0 */
+ if (num == 0)
+ {
+ str[i++] = '0';
+ str[i] = '\0';
+ return str;
+ }
+
+ // In standard itoa(), negative numbers are handled only with
+ // base 10. Otherwise numbers are considered unsigned.
+ if (num < 0 && base == 10)
+ {
+ isNegative = true;
+ num = -num;
+ }
+
+ // Process individual digits
+ while (num != 0)
+ {
+ int rem = num % base;
+ str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
+ num = num/base;
+ }
+
+ // If number is negative, append '-'
+ if (isNegative)
+ str[i++] = '-';
+
+ str[i] = '\0'; // Append string terminator
+
+ // Reverse the string
+ reverse(str);
+
+ return str;
+}
+
+/*
+** reverse string in place
+*/
+void reverse(char *s){
+char *j;
+int c;
+
+ j = s + strlen(s) - 1;
+ while(s < j) {
+ c = *s;
+ *s++ = *j;
+ *j-- = c;
+ }
+}
\ No newline at end of file
--- a/peer_to_peer_example.cpp Fri Dec 01 01:07:57 2017 +0000
+++ b/peer_to_peer_example.cpp Fri Dec 01 20:22:20 2017 +0000
@@ -1,9 +1,11 @@
+#include <stdlib.h>
+#include <string.h>
+#include <mbed.h>
#include "dot_util.h"
#include "RadioEvent.h"
-#include <string.h>
-#include <mbed.h>
+#include "itoa.h"
+
-
/////////////////////////////////////////////////////////////////////////////
// -------------------- DOT LIBRARY REQUIRED ------------------------------//
// * Because these example programs can be used for both mDot and xDot //
@@ -38,7 +40,7 @@
DigitalOut SPI_CS(D10);
//InterruptIn INT1();
//InterruptIn INT2();
-DigitalOut CS(D10); // Used for CS chip select
+//DigitalOut CS(D10); // Used for CS chip select
// ADXL372 Slave I2C
I2C ADXL372(I2C_SDA, I2C_SCL); // (D14,D15) (MISO, CS)
@@ -73,7 +75,7 @@
char * accelerometerI2CRead(int hexAddress);
void ADXL372Reset(void);
void BitBangSPIWrite(const unsigned char regAddr, const unsigned char regData);
-unsigned char BitBangSPIRead (const unsigned char regAddr);
+uint8_t BitBangSPIRead (const unsigned char regAddr);
int ADT7410Write(unsigned char registerAddress, unsigned char data);
char * ADT7410Read(int hex);
@@ -86,13 +88,15 @@
void printMenu(){
pc.printf("Please eneter a debug option: \n\r"
- "1: Read converted values from Accelerometer ADXL372\n\r"
+ "1: I2C Read converted values from Accelerometer ADXL372\n\r"
"2: Read converted values from Temperature ADT7410\n\r"
"3: Read raw values from Accelerometer ADXL372\n\r"
"4: Read raw values from Temperature ADT7410\n\r"
"5: Initialize Accelerometer\n\r"
"6: Reset Accelerometer\n\r"
- "7: Send Temperature data\n\r");
+ "7: Send Temperature data\n\r"
+ "8: SPI Read values from Accelerometer ADXL372\n\r"
+ "9: SPI Write to a register for ADXL372\n\r");
}
@@ -126,7 +130,7 @@
int main() {
// Custom event handler for automatically displaying RX data
- interruptEverything.attach(&interruptReadTemperature, 7.0);
+ //interruptEverything.attach(&interruptReadTemperature, 7.0);
RadioEvent events;
uint32_t tx_frequency;
uint8_t tx_datarate;
@@ -286,7 +290,7 @@
for(int i = 0; i < 15; ++i){
regAddress = 0x08; // This is the register address for XData
accelValues = accelerometerI2CRead(regAddress);
- XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short in, remove last 4 flag bits
+ XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int, remove last 4 flag bits
YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
@@ -299,7 +303,7 @@
regAddress = 0x00;
rawTempValues = ADT7410Read(regAddress);
convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes into
- // a short int variable, remove last 3
+ // a short int variable(16 bits), remove last 3 bits
pc.printf("\n %d: Temperature is: 0x%x \n\r", i+1, convertedTempValue);
}
@@ -310,7 +314,7 @@
for(int i = 0; i < 15; ++i){
regAddress = 0x08;
accelValues = accelerometerI2CRead(regAddress);
- XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short in, remove last 4 flag bits
+ XData = ((*(accelValues + 0) << 8) | *(accelValues + 1)) >> 4; // Combine two bytes into short int(16 bits), remove last 4 flag bits
YData = ((*(accelValues + 2) << 8) | *(accelValues + 3)) >> 4;
ZData = ((*(accelValues + 4) << 8) | *(accelValues + 5)) >> 4;
pc.printf("\n %d: X:: H: %x | L: %x | Y:: H: %x | L: %x | Z: H: %x | L: %x \n\r", i+1, *(accelValues + 0), *(accelValues + 1), *(accelValues + 2), *(accelValues + 3), *(accelValues + 4), *(accelValues + 5));
@@ -341,7 +345,43 @@
tx_data.push_back(convertedTempValue & 0xFF);
logInfo("light: %lu [0x%04X]", convertedTempValue, convertedTempValue);
send_data(tx_data);
+ break;
+ case 56: // 8
+ uint8_t MSB;
+ uint8_t LSB;
+
+
+ for(int i = 0; i < 15; ++i){
+
+ MSB = BitBangSPIRead(0x08); // XData MSB
+ LSB = BitBangSPIRead(0x09); // XData LSB
+ XData = ((MSB << 8) | LSB) >> 4;
+
+ MSB = BitBangSPIRead(0x0A); // YData MSB
+ LSB = BitBangSPIRead(0x0B); // YData LSB
+ YData = ((MSB << 8) | LSB) >> 4;
+
+ MSB = BitBangSPIRead(0x0C); // ZData MSB
+ LSB = BitBangSPIRead(0x0D); // ZData LSB
+ ZData = ((MSB << 8 ) | LSB) >> 4;
+
+ pc.printf("\n %d: X: 0x%x | Y: 0x%x | Z: 0x%x \n\r", i+1, XData, YData, ZData);
+ wait(0.2);
+ }
+ break;
+ case 57: // 9
+ int input;
+ char passRegister[1];
+ char passData[1];
+ pc.scanf("What register do you want(2 digit hex form)? \n\r"
+ "Ex: Register 0x01 enter 1"
+ "%d", &input);
+ itoa(input, passRegister, 16); // Convert number(input), into hexadecimal(16), and save in char buffer(passVariable)
+ pc.printf("0x%x register\n\r", passRegister[0]);
+ pc.scanf("What is the hex representation of your data? %d \n\r", input);
+ itoa(input, passData, 16);
+ pc.printf("0x%x is your data\n\r", passData[0]);
default:
printMenu();
break;
@@ -618,12 +658,12 @@
*
******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
-unsigned char BitBangSPIRead (const unsigned char regAddr)
+uint8_t BitBangSPIRead (uint8_t regAddr)
{
unsigned char SPICount; // Counter used to clock out the data
- unsigned char SPIData;
+ uint8_t SPIData;
SPI_CS = 0; // Make sure we start with active-low CS high
SPI_CLK = 0; // and CK low
@@ -653,7 +693,7 @@
}// and loop back for next bit
SPI_CS = 0; // Raise CS
- return ((unsigned char)SPIData); // Finally return the read data
+ return ((uint8_t)SPIData); // Finally return the read data
}
/*******************************************************************************
