int clock(){ int i2cHandle; printf("We're doing things!\n"); int deviceHandle; initiateGPIO(GP_I2C); i2cHandle = openGPIO(GP_I2C, GPIO_DIRECTION_OUT); writeGPIO(i2cHandle, 0); close(i2cHandle); int readStatus; int status; char buffer[7]; buffer[0] = 0x00; int deviceI2CAddress = 0x68; if ((deviceHandle = open("/dev/i2c-0", O_RDWR)) < 0){ printf("Turns out we're not.\n"); return 1; } if(ioctl(deviceHandle, I2C_SLAVE, deviceI2CAddress) < 0){ printf("Error in ioctl\n"); return 1; } time_t result = time(NULL); int sysSeconds, sysMinutes, sysHours, sysYears, sysDays, sysMonths; if(result != -1) { struct tm *timeInfo; timeInfo = gmtime(&result); sysSeconds = timeInfo->tm_sec; sysMinutes = timeInfo->tm_min; sysHours = (timeInfo->tm_hour)%24;//HOUR sysYears = (timeInfo->tm_year+1900) %100 ; sysDays = timeInfo->tm_mday; sysMonths = (timeInfo->tm_mon+1); } int lowSec = sysSeconds % 10; lowSec &= 0x0f; int highSec = (sysSeconds/10) << 4; highSec &= 0x70; int lowMin = sysMinutes % 10; lowMin &= 0x0f; int highMin = (sysMinutes/10) << 4; highMin &= 0x70; int miltime = 0b10111111; int lowHour = sysHours % 10; lowHour &= 0x0f; int highHour = (sysHours /10) << 4; highHour &= 0x30; int lowDay = sysDays % 10; lowDay &= 0x0f; int highDay = (sysDays /10) <<4; highDay &= 0x30; int lowMonth = sysMonths % 10; lowMonth &= 0x0f; int highMonth = (sysMonths / 10) << 4; highMonth &= 0x10; int lowYear = sysYears % 10; lowYear &= 0x0f; int highYear = (sysYears / 10 ) << 4; highYear &= 0xf0; buffer[0] = 0x00; //first word, before we start writing data buffer[1] = ( highSec | lowSec ); buffer[2] = ( highMin | lowMin); // buffer[3] = ( highHour | lowHour); buffer[4] = 0x01; buffer[5] = (lowDay | highDay); buffer[6] = (lowMonth | highMonth); buffer[7] = (lowYear | highYear); //Enable these lines to see time data while storing it //printf("Date: %d - %d - %d\n", sysYears, sysMonths, sysDays); //printf("Time: %d : %d : %d\n", sysHours, sysMinutes, sysSeconds); status = write(deviceHandle, buffer, 7); if(status != 7){ printf("Error: more error! (no ack bit)\n"); return 0; } buffer[0] = 0x00; status = write (deviceHandle, buffer, 1); if(status != 1){ printf("information!\n"); return -1; }else{ status = read(deviceHandle, buffer, 7); if(status != 7){ printf("Something's gone wrong again.\n"); return -1; } } int year, month, day, hours, minutes, seconds; highSec = (0x70 & buffer[0])>>4; lowSec = 0x0f & buffer[0]; lowMin = 0x0f & buffer[1]; highMin = (0x70 & buffer[1])>>4; lowHour = 0x0f & buffer[2]; highHour = (0x30 & buffer[2])>>4; lowDay = 0x0f & buffer[4]; highDay = (0x30 & buffer[4])>>4; lowMonth = 0x0f & buffer[5]; highMonth = (0x10 & buffer[5])>>4; lowYear = 0x0f & buffer[6]; highYear = (0xf0 & buffer[6])>>4; printf("Date: %d%d - %d%d - %d%d\n", highYear,lowYear,highMonth,lowMonth, highDay,lowDay); printf("Time: %d%d : %d%d : %d%d\n", highHour,lowHour, highMin,lowMin, highSec, lowSec); //More basic data to possibly print //printf("Date: %d - %d - %d\n", year, month, day); //printf("Time: %d : %d : %d\n", hours, minutes, seconds); /* int i; for(i = 0; i < 7; i++){ printf("buffer[%d]: %d\n", i, buffer[i]); } */ }
/* Function: main Purpose: main program performs UI controls and forwards cmd through function calls Input: void Returns: o if clean termnation */ int main (void) { char cmd; message msgOut; message msgIn; int i; int strobeHandle; int adcValue; float voltage; int dataPath[DATA_PATH_SIZE] = {GP_4, GP_5, GP_6, GP_7}; pthread_t displayThread; pthread_mutex_init (&cGmutex, NULL); initiateGPIO(Strobe); initiateGPIOArray(dataPath, DATA_PATH_SIZE); strobeHandle = openGPIO(Strobe, GPIO_DIRECTION_OUT); writeGPIO(strobeHandle,1); close(strobeHandle); clearPAGE(); while (1) { printMenu(); cmd = getchar(); clearInputBuffer(); clearBelowLine(MSG_Y); saveCursor(); switch (cmd) { case 'L': { msgOut.data = 0xD; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t LED Always On"); } else if (msgIn.data == 0xC) { printf("[\033[0;32m OK \033[m]\t LED Triggered By Sensor"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'P' : { msgOut.data = 0x1; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t Ping Successful"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'R' : { msgOut.data = 0x0; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t Reset Successful"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'G' : { adcValue = 0; voltage = 0; msgOut.data = 0x2; sendMessage(msgOut, dataPath); for (i = 2; i >= 0; i--) { msgIn = receiveMessage(dataPath); adcValue |= (msgIn.data << (i * DATA_PATH_SIZE)); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); } msgIn = receiveMessage(dataPath); if (msgIn.data == 0xE) { adcValue &= 0x3FF; voltage = (float) ((adcValue/1024.0) * 5.0); setColor(YELLOW); gotoXY(MSG_X,MSG_Y + 1); printf("ADC Value: 0x%X", adcValue); gotoXY(MSG_X,MSG_Y + 2); printf("Voltage: %lf \033[u", voltage); gotoXY(STATUS_X,STATUS_Y); setColor(RESET); printf("[\033[0;32m OK \033[m]\t ADC Read Successful \033[u"); } else { gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed \033[u"); } break; } case 'T' : { msgOut.data = 0x3; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t ADC Toggled On"); } else if (msgIn.data == 0xC) { printf("[\033[0;32m OK \033[m]\t ADC Toggled Off"); } break; } case 'D': { pthread_mutex_lock (&cGmutex); continueGraphing = true; pthread_mutex_unlock (&cGmutex); pthread_create(&displayThread, NULL, graphVoltage, &dataPath); getchar(); pthread_mutex_lock (&cGmutex); continueGraphing = false; pthread_mutex_unlock (&cGmutex); pthread_join(displayThread, NULL); clearPAGE(); break; } case 'Q': { gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("[\033[0;32m OK \033[m]\t ThankYou For UsingLightSensor"); unexport(Strobe); unexportArray(dataPath, DATA_PATH_SIZE); printf("\033[2J\033[0;0H\033[m"); exit(0); } } recallCursor(); } return (0); }