void setupConnection(Connection *conn, const int sock, const struct sockaddr_in paddr, const uint32_t sndwndb, const uint32_t rcvwndb, const long double sampleRTT) { conn->sock.fd = sock; setSocketConnected(conn->sock.fd, paddr); initializeTimeout(&(conn->timeout), sampleRTT); initializeWindow(&(conn->sndwnd),sndwndb, sndwndb + (RUSP_PLDS * RUSP_WNDS)); initializeWindow(&(conn->rcvwnd), rcvwndb, rcvwndb + (RUSP_PLDS * RUSP_WNDS)); initializeStrBuff(&(conn->sndusrbuff)); initializeStrBuff(&(conn->rcvusrbuff)); initializeSgmBuff(&(conn->sndsgmbuff)); initializeSgmBuff(&(conn->rcvsgmbuff)); setConnectionState(conn, RUSP_ESTABL); conn->sender = createThread(senderLoop, conn, THREAD_JOINABLE); conn->receiver = createThread(receiverLoop, conn, THREAD_JOINABLE); }
int main(void) { // initialize // short int testcode[9] = {'3','2','1','\0','\0','\0','\0','\0','\0'}; int rows[] = {ROW1, ROW2, ROW3, ROW4}; int cols[] = {COL1, COL2, COL3}; volatile int count_queue; // Keep track of how many characters are // in the queue int code_is_correct; initializeMotorPins(); initializeKeypadInterrupts(rows); sei(); // global interrupt enable initRows(rows); // set keypad rows as inputs initColumns(cols); // set keypad columns as outputs initializeLCD(); // set up LCD and initialize in 4 bit mode BacklightLCD(1); clearKeyQueue(); //writeLCDcharacter('x'); // Ensure we're locked to start with. while (lock_state != 1) { lock_state = lock(2); } initializeTimeout(); // wait loop while(1) { // Print greeting clearLCD(); writeLCDline(enter_code,1); cursorPosition(2); readFROMeeprom(current_code); /* // TESTING MOTOR while(1){ lock(1); _delay_ms(100); unlock(1); _delay_ms(100); } // END TESTING MOTOR */ // While box is in locked state while (lock_state == 1) { // Wait for a key press while(key_queue[count_queue] == '\0'); // Now that we have a key press we need to look at what was pressed. // Is the first key in the queue a '#'... if(key_queue[0] == '#'){ clearLCD(); writeLCDline(enter_code,1); cursorPosition(2); clearKeyQueue(); count_queue = 0; //testfun(1); // ...or is the first key in the queue a digit?... } else if((key_queue[0] >= '0') && (key_queue[0] <= '9') && (count_queue < 8)){ writeLCDcharacter(key_queue[0]); count_queue++; // ...or if first key in the queue isn't a digit or '#' then // it must be the '*' } else if(key_queue[0] == '*') { count = 0; code_is_correct = 1; popKey(); clearLCD(); cursorPosition(1); for(int i = 0; i < 10; i++){ writeLCDcharacter(key_queue[i]); } cursorPosition(2); for(int i = 0; i < 9; i++){ writeLCDcharacter(current_code[i]); } _delay_ms(1000); // Checks the current queue code with the correct code while(key_queue[count] != '\0'){ if(key_queue[count] != current_code[count]) code_is_correct = 0; ++count; } if(!code_is_correct && count > 0){ clearLCD(); writeLCDline(incorrect_code,1); _delay_ms(5000); clearKeyQueue(); count_queue = 0; clearLCD(); writeLCDline(enter_code,1); cursorPosition(2); }else if(code_is_correct && count > 0){ // Unlock the box lock_state = unlock(lock_state); } } } // End while (lock_state == 1) // While box is in unlocked state while (lock_state == 0 ) { // Write unlocked menu clearLCD(); writeLCDline(unlocked_menu_1, 1); writeLCDline(unlocked_menu_2, 2); clearKeyQueue(); count_queue = 0; // Wait for key press while(key_queue[count_queue] == '\0'); // Now that we have a key press we need to look at what was pressed. // Is the first key in the queue a '#'... if(key_queue[0] == '#'){ lock_state = lock(lock_state); clearKeyQueue(); count_queue = 0; writeLCDline(enter_code,1); cursorPosition(2); // ...or is the first key in the queue a '*'? } else if(key_queue[0] == '*'){ clearLCD(); clearKeyQueue(); count_queue = 0; writeLCDline(new_code_menu, 1); cursorPosition(2); // Now we start entering the new code. An '*' indicates that we're // done entering the code. while(key_queue[0] != '*') { // Wait for key press while(key_queue[count_queue] == '\0'); // Was the latest key pressed between 0 and 9, AND has the user // entered less than the max number (8) of digits allowed for the // code. if(key_queue[0] >= '0' && key_queue[0] <= '9' && count_queue < 9){ // QUESTION: DO WE NEED TO MANUALLY SHIFT THE QUEUE?? writeLCDcharacter(key_queue[0]); ++count_queue; // If the latest key pressed is '*' then lets write the new // code to eeprom } else if (key_queue[0] == '*') { popKey(); writeTOeeprom(key_queue); break; // Otherwise the only key left is '#', so that's what must've been // pressed. :) } else if(key_queue[0] == '#') { clearLCD(); writeLCDline(cancel_code_change,1); _delay_ms(5000); clearKeyQueue(); count_queue = 0; clearLCD(); break; } } } } // End while (lock_state == 0 ) } // End while(1); } // End main();