Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *beginListening(void* blank) {
- #ifdef __linux__
- pthread_mutex_lock(&screenLock);
- mvprintw(2, 1, "Listening...");
- mvprintw(3, 1, "Awaiting addressing... ");
- mvprintw(4, 1, " ");
- refresh();
- pthread_mutex_unlock(&screenLock);
- #endif
- byte bits = 0;
- unsigned int bytes = 0, xfers = 0;
- byte rxByte = 0;
- byte rxData[512] = {};
- bool readCl = true, pReadCl = true, readDa = true, pReadDa = true;
- bool shouldListen = false;
- while(alive) {
- readDa = readPin(DataPin);
- readCl = readPin(ClockPin);
- // Need to look for a Start Bit
- if(!readDa && pReadDa & readCl) { // Falling Data during a High Clock, that's our Start.
- // Now wait for a Stop Bit
- while(alive) {
- readDa = readPin(DataPin);
- readCl = readPin(ClockPin);
- if(readDa & !pReadDa & readCl) { // Rising Data during a High Clock, that's the Stop.
- break;
- } if(!readDa && pReadDa & readCl) { // Falling Data during a High Clock, it's a Repeated Start.
- bits = 0; // Quickly clean up and reset
- rxByte = 0;
- bytes = 0; // Don't worry about zeroing the scratch half-a-kilo
- shouldListen = false;
- pReadCl = readCl;
- pReadDa = readDa;
- continue;
- } else if(!pReadCl && readCl) { // Rising Clock, Sample!
- if(bits < 8) { // Bits 0 - 7, should be recording
- rxByte = (rxByte << 1);
- if(readDa) {
- rxByte++;
- }
- } // Otherwise, this is bit 8, where we should be sending a specific value.
- // Master should be sampling our Acknolwedge.
- bits++;
- } else if(pReadCl && !readCl) { // Falling Clock, Change!
- if(bits = 8) { // This is bit 8. We got a whole byte.
- if(shouldListen || rxByte & 0xFE == 0x66) { // If the Master's trying to address us, 0x66... (or if we were addressed previously)
- driveDown(DataPin); // Send the Master an ACK.
- if(!shouldListen) { // We've just been addressed.
- shouldListen = true; // Always ACK until a Repeated Start or Stop Bit.
- } else {
- rxData[bytes++] = rxByte; // Save the byte we just got - overwrites any previous value held
- }
- } else {
- #ifdef __linux__
- pthread_mutex_lock(&screenLock);
- mvprintw(5, 1, "%3i: Message for 0x%02x.", ++xfers, rxByte & 0xFE);
- move(2, 13);
- refresh();
- pthread_mutex_unlock(&screenLock);
- #endif
- break; // Stop listening, this message isn't for us. Look for another Start Bit.
- }
- } else if(bits = 9) { // This is the end of bit 8. Reaching this code means that we've been addressed.
- release(DataPin); // We've sent our ACK, let it go.
- driveDown(ClockPin); // Hold the clock low until we're ready for another byte.
- #ifdef __linux__
- pthread_mutex_lock(&screenLock);
- mvprintw(3, 1, "Incoming data... ");
- mvprintw(4, 1, "%3i bytes given. ", bytes);
- mvprintw(5, 1, " ");
- move(2, 13);
- refresh();
- pthread_mutex_unlock(&screenLock);
- #endif
- release(ClockPin);
- } // Otherwise, the master should be changing bits now.
- }
- pReadCl = readCl;
- pReadDa = readDa;
- }
- shouldListen = false;
- bits = 0; rxByte = 0; bytes = 0; // Don't worry about zeroing the scratch half-a-kilo
- // Do things with the obtained data here.
- #ifdef __linux__
- pthread_mutex_lock(&screenLock);
- mvprintw(3, 1, "Awaiting addressing... ");
- move(2, 13);
- refresh();
- pthread_mutex_unlock(&screenLock);
- } else {
- usleep(500);
- #endif
- }
- pReadCl = readCl;
- pReadDa = readDa;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement