/// revoke access to GPIO and PWM bool GPIO_teardown() { for (size_t i = 0; i < SIZE_OF_ARRAY(gpio_info); ++i) { if (NULL == gpio_info[i].name) { continue; } if (gpio_info[i].fd >= 0) { close(gpio_info[i].fd); gpio_info[i].fd = -1; } if (NULL != gpio_info[i].state) { free(gpio_info[i].state); gpio_info[i].state = NULL; } if (NULL != gpio_info[i].number) { unexport(gpio_info[i].number); free(gpio_info[i].number); gpio_info[i].number = NULL; } if (NULL != gpio_info[i].direction) { free(gpio_info[i].direction); gpio_info[i].direction = NULL; } if (NULL != gpio_info[i].active_low) { free(gpio_info[i].active_low); gpio_info[i].active_low = NULL; } if (NULL != gpio_info[i].value) { free(gpio_info[i].value); gpio_info[i].value = NULL; } } for (size_t i = 0; i < SIZE_OF_ARRAY(pwm); ++i) { if (NULL != pwm[i].name) { close(pwm[i].fd); pwm[i].fd = -1; free(pwm[i].name); pwm[i].name = NULL; } } if (NULL != slots) { // should unload the loaded firmware here, // but any attempt to do this crashes the process // probably a kernel bug, which may cause system instablility free(slots); slots = NULL; } if (NULL != ocp) { free(ocp); ocp = NULL; } // all data cleared so calling setup again will work return true; }
JObjectInformation* JObjectInformations::unexport(JObject* object){ JObjectInformation* objInfo; lock(); objInfo = getFromMap(objectMap,object); unlock(); if (objInfo != NULL) { return unexport(objInfo->getId()); } return NULL; }
/// revoke access to GPIO and PWM bool GPIO_teardown() { // finalise SPI multiplexor for (int i = 0; i < SIZE_OF_ARRAY(spi_state_file); ++i) { write_file(spi_state_file[i], MUX_default "\n", CONST_STRLEN(MUX_default "\n")); } for (size_t pin = 0; pin < SIZE_OF_ARRAY(gpio_info); ++pin) { if (Mode_NONE == gpio_info[pin].active) { continue; } if (gpio_info[pin].fd >= 0) { close(gpio_info[pin].fd); gpio_info[pin].fd = -1; } switch(gpio_info[pin].active) { case Mode_NONE: break; case Mode_GPIO: unexport(pin); break; case Mode_PWM: { int chip = gpio_info[pin].pwm_chip; int channel = gpio_info[pin].pwm_channel; write_pwm_file(PWM_DUTY_CYCLE, chip, channel, "0\n", 2); write_pwm_file(PWM_ENABLE, chip, channel, "0\n", 2); unexport_pwm(chip, channel); write_file(pwm_state_file[gpio_info[pin].pwm_state], MUX_default "\n", CONST_STRLEN(MUX_default "\n")); //unexport(pin); break; } } gpio_info[pin].active = Mode_NONE; } return true; }
/* 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); }