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.
Revision 5:df53c6d9caeb, committed 2019-11-10
- Comitter:
- sev2000
- Date:
- Sun Nov 10 16:01:46 2019 +0000
- Parent:
- 4:fcfcce37657c
- Commit message:
- Lib X2D
Changed in this revision
diff -r fcfcce37657c -r df53c6d9caeb X2D.cpp --- a/X2D.cpp Sun Nov 10 15:24:32 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,224 +0,0 @@ -#include "mbed.h" -#include "X2D.h" - -#define BUF_SIZE 1000 -#define BIT_SIZE 145 - -DigitalOut Reg_Data(PC_1); -DigitalOut RxTx(PB_0); -DigitalOut Tx(PA_9); -DigitalOut UART(PA_0, 1); - -DigitalIn CD(PC_0); -//DigitalIn Rx(PB_7); -DigitalIn BU(PA_4); -DigitalIn CLR(PA_1); -DigitalIn RSTO(PC_2); - -int processData(void); - -long startedAt=0; -long endedAt=0; - -bool dataBits[BIT_SIZE]={0}; // 18 Nibbles +1 - -typedef struct { - long v; - bool pin; -}pulse_t; - -pulse_t timeDiff; - -CircularBuffer<pulse_t, BUF_SIZE> PulseWidth; - -InterruptIn DataPin(PB_7, PullUp); - -Serial pc(SERIAL_TX, SERIAL_RX); -Timer xTime; - -void getPulseF(void) -{ - endedAt = xTime.read_us(); // set timer end for last pin - timeDiff.v = endedAt - startedAt; - timeDiff.pin = 1; - PulseWidth.push(timeDiff); - startedAt= endedAt; // set timer start for this pin -} - -void getPulseR(void) -{ - endedAt = xTime.read_us(); // set timer end for last pin - timeDiff.v = endedAt - startedAt; - timeDiff.pin = 0; - PulseWidth.push(timeDiff); - startedAt= endedAt; // set timer start for this pin -} - -int detectPreamble(void) -{ -pulse_t pulse; -int cnt = 0; -char tmp[32]={0}; -char timing[8*BUF_SIZE]={0}; -char state=0; -char s=0, l=0, bit_ptr=0; - - while (!PulseWidth.empty()) - { - PulseWidth.pop(pulse); - sprintf(tmp, "%d, %ld|", pulse.pin, pulse.v); - strcat(timing, tmp); - - if ((pulse.v > 700) && (pulse.v < 1000)) - { // short off detected - s++; - l=0; - } - else if ((pulse.v > 1500) && (pulse.v < 1800)) - { // long off detected - l++; - s=0; - } - else - { - l=0; - s=0; - bit_ptr=0; - state=0; - } - switch(state) - { - case 0: // Detect preamble - if(s >= 12) // out of 12 - state=1; - //pc.printf("%d ", s); - break; - case 1: // wait start bit (first long) - //pc.printf("OK2"); - s=0; - if (l==1) - { - state = 2; - //bit_ptr++; inculde start bit in payload - } - l=0; - break; - case 2: - //pc.printf(" %d", pulse.v); - - if (s == 2) - { - dataBits[bit_ptr] = 1; - l=0; - s=0; - bit_ptr++; - } - if (l == 1 && s==0) - { - dataBits[bit_ptr] = 0; - l=0; - s=0; - bit_ptr++; - } - if(bit_ptr > BIT_SIZE) - { - state=0; - bit_ptr=0; - printf("Frame too long ; dropped"); - } - - break; - } - - if(pulse.v > 30000 && cnt>0) // End of frame - { - processData(); - //timing[0]=0; - state=0; - bit_ptr=0; - //PulseWidth.reset(); -// WARN(" Waiting..."); - } - - cnt++; - } - if (cnt>0) // if buffer wasn't empty - { - processData(); - pc.printf("%s\r\n", timing); - } - - return(0); -} - -int processData(void) -{ -int x=0; -int i = 0; -int j= 0; -char nibble[18]={0}, cnt=0; -int chksum=0, etx_ptr=16; - -/* pc.printf("\r\n"); - for (x=0;x<128;x++) - { - if(x%8==0) - pc.printf(" "); - pc.printf("%d", dataBits[x]); - } -*/ -x=0; - - for (i=0; i<etx_ptr; i++) - { - for (j=0;j<8;j++) - { - if ( dataBits[x]) - { - nibble[i] |= 1<<j; - cnt++; - } - else - { - if (cnt == 5) - j--; - cnt=0; - } - dataBits[x] =0; // clean variable - x++; - } - if (cnt >= 8) // End of Frame detection - etx_ptr=i; - } - - - for (i=0; i<etx_ptr-2; i++) // Calculate Checksum - chksum += nibble[i]; - chksum = ~chksum +1; - -#ifdef __DEBUG__ - for (i=0; i<etx_ptr; i++) - pc.printf("%0.2X ",nibble[i]); - if ( (char)(chksum>>8) != nibble[etx_ptr-2] || (char)chksum != nibble[etx_ptr-1] ) - pc.printf(" CRC Error"); - pc.printf("\r\n"); -#endif - - return 0; -} - -void Init_X2D() -{ - DataPin.fall(&getPulseF); - DataPin.rise(&getPulseR); - UART = 1; - RxTx = 1; //set pin rxtx to Rx - Reg_Data = 0; //set pin reg_data - Tx = 0; - - xTime.start(); - xTime.reset(); - startedAt = xTime.read_us(); // set initial timer end - - //thread.start(getData); -} \ No newline at end of file
diff -r fcfcce37657c -r df53c6d9caeb X2D.h --- a/X2D.h Sun Nov 10 15:24:32 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -#include "mbed.h" - -extern Serial pc; - -#define __DEBUG__ -#define Level 3 -#define DBG(x, ...) if (Level>=3) pc.printf("[DBG]"x"\r\n", ##__VA_ARGS__); -#define WARN(x, ...) if (Level>=2) pc.printf("[WARN]"x"\r\n", ##__VA_ARGS__); -#define ERR(x, ...) if (Level>=1) pc.printf("[ERR]"x"\r\n", ##__VA_ARGS__); - -void Init_X2D(void); -int detectPreamble(void); \ No newline at end of file
diff -r fcfcce37657c -r df53c6d9caeb X2D.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X2D.lib Sun Nov 10 16:01:46 2019 +0000 @@ -0,0 +1,1 @@ +X2D#9ef8edfe21bc
diff -r fcfcce37657c -r df53c6d9caeb main.cpp --- a/main.cpp Sun Nov 10 15:24:32 2019 +0000 +++ b/main.cpp Sun Nov 10 16:01:46 2019 +0000 @@ -1,17 +1,17 @@ #include "mbed.h" #include "X2D.h" +extern Timer xTime; extern long startedAt; -extern Timer xTime; Serial pc(SERIAL_TX, SERIAL_RX); - + int main() { - pc.baud(115200); pc.printf("Start\r\n"); Init_X2D(); + while (1) { if (xTime.read_us()- startedAt > 32000)