Пример #1
0
int main(void) {
	// initialize
	int rows[] = {ROW1, ROW2, ROW3, ROW4};
	int cols[] = {COL1, COL2, COL3};
	int count;											// counter variable used for loops
	int count_queue;									// Keep track of how many characters are
														// in the queue
	int code_is_correct;								 
	int lock_state = 2;									// initialized lock variable to intermediate status		
	
	// initialize interrupts
	PCICR |= (1 << PCIE0);								// set bit 0 of PCICR
	for(int i = 0; i < NUM_ROWS; i++){
		PCMSK0 |= (1 << rows[i]);						// allow row pins to trigger interrupts
	}
	sei();												// global interrupt enable
	
	// Initializing arrays 
	int enter_code[16] = {'E','N','T','E','R',' ','C','O','D','E',' ',' ',' ',' ',' ',' '};
	int incorrect_code[16] = {'I','N','C','O','R','R','E','C','T',' ','C','O','D','E',' ',' '};
	int unlocked_menu_1[16] = {'#',' ','-',' ','T','O',' ','L','O','C','K',' ',' ',' ',' ',' ',};
	int unlocked_menu_2[16] = {'*',' ','-',' ','S','E','T',' ','N','E','W',' ','C','O','D','E',};
	int new_code_menu[16] = {'S','E','T',' ','N','E','W',' ','C','O','D','E',' ',' ',' ',' ',};
	int cancel_code_change[16] = {' ',' ','-','-','C','A','N','C','E','L','E','D','-','-',' ',' ',};
	int current_code[9];
	
	initRows(rows);										// set keypad rows as inputs
	initColumns(cols);									// set keypad columns as outputs
	initializeLCD();									// set up LCD and initialize in 4 bit mode
	clearKeyQueue();
	count_queue = 0;


	// Ensure we're locked to start with.
	while (lock_state != 1) {
			lock_state = lock(2);
	}

	
	// wait loop
	while(1) {

		// Print greeting
		clearLCD();
		writeLCDline(enter_code,1);
		cursorPosition(2);

		// 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();
					clearKeyQueue();
					count_queue = 0;
				
				// ...or is the first key in the queue a digit?...
				} else if(key_queue[0] >= '0' && key_queue[0] <= '9'){
					// QUESTION: DO WE NEED TO MANUALLY SHIFT THE QUEUE??
					writeLCDcharacter(key_queue[0]);
					++count_queue;
				
				// ...or if first key in the queue isn't a digit or '#' then
				// it must be the '*'
				} else {
					count = 0;
					code_is_correct = 1;
					// 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;				
					}
					if(!code_is_correct){
						clearLCD();
						writeLCDline(incorrect_code,1);
						_delay_ms(5000);
						clearKeyQueue();
						count_queue = 0;
						clearLCD();
					}else{

						// 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 keypress
				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;

				// ...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] != '*') {
						
						clearKeyQueue();

						// Wait for keypress
						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] == '*') {
							writeTOeeprom();


						// Otherwise the only key left is '#', so that's what must've been
						// pressed. :)
						} else {
							clearLCD();
							writeLCDline(cancel_code_change,1);
							_delay_ms(5000);
							clearKeyQueue();
							count_queue = 0;
							clearLCD();
						}
				
					}

					
				}		
			} // End while (lock_state == 0 )

	} // End while(1);
} // End main();
Пример #2
0
bool QSQLiteResultPrivate::fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch)
{
    int res;
    int i;

    if (skipRow) {
        // already fetched
        Q_ASSERT(!initialFetch);
        skipRow = false;
        for(int i=0;i<firstRow.count();i++)
            values[i]=firstRow[i];
        return skippedStatus;
    }
    skipRow = initialFetch;

    if(initialFetch) {
        firstRow.clear();
        firstRow.resize(sqlite3_column_count(stmt));
    }

    if (!stmt) {
        q->setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult", "Unable to fetch row"),
                                  QCoreApplication::translate("QSQLiteResult", "No query"), QSqlError::ConnectionError));
        q->setAt(QSql::AfterLastRow);
        return false;
    }
    res = sqlite3_step(stmt);

    switch(res) {
    case SQLITE_ROW:
        // check to see if should fill out columns
        if (rInf.isEmpty())
            // must be first call.
            initColumns(false);
        if (idx < 0 && !initialFetch)
            return true;
        for (i = 0; i < rInf.count(); ++i) {
            switch (sqlite3_column_type(stmt, i)) {
            case SQLITE_BLOB:
                values[i + idx] = QByteArray(static_cast<const char *>(
                            sqlite3_column_blob(stmt, i)),
                            sqlite3_column_bytes(stmt, i));
                break;
            case SQLITE_INTEGER:
                values[i + idx] = sqlite3_column_int64(stmt, i);
                break;
            case SQLITE_FLOAT:
                switch(q->numericalPrecisionPolicy()) {
                    case QSql::LowPrecisionInt32:
                        values[i + idx] = sqlite3_column_int(stmt, i);
                        break;
                    case QSql::LowPrecisionInt64:
                        values[i + idx] = sqlite3_column_int64(stmt, i);
                        break;
                    case QSql::LowPrecisionDouble:
                    case QSql::HighPrecision:
                    default:
                        values[i + idx] = sqlite3_column_double(stmt, i);
                        break;
                };
                break;
            case SQLITE_NULL:
                values[i + idx] = QVariant(QVariant::String);
                break;
            default:
                values[i + idx] = QString(reinterpret_cast<const QChar *>(
                            sqlite3_column_text16(stmt, i)),
                            sqlite3_column_bytes16(stmt, i) / sizeof(QChar));
                break;
            }
        }
        return true;
    case SQLITE_DONE:
        if (rInf.isEmpty())
            // must be first call.
            initColumns(true);
        q->setAt(QSql::AfterLastRow);
        sqlite3_reset(stmt);
        return false;
    case SQLITE_CONSTRAINT:
    case SQLITE_ERROR:
        // SQLITE_ERROR is a generic error code and we must call sqlite3_reset()
        // to get the specific error message.
        res = sqlite3_reset(stmt);
        q->setLastError(qMakeError(access, QCoreApplication::translate("QSQLiteResult",
                        "Unable to fetch row"), QSqlError::ConnectionError, res));
        q->setAt(QSql::AfterLastRow);
        return false;
    case SQLITE_MISUSE:
    case SQLITE_BUSY:
    default:
        // something wrong, don't get col info, but still return false
        q->setLastError(qMakeError(access, QCoreApplication::translate("QSQLiteResult",
                        "Unable to fetch row"), QSqlError::ConnectionError, res));
        sqlite3_reset(stmt);
        q->setAt(QSql::AfterLastRow);
        return false;
    }
    return false;
}
Пример #3
0
 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();