int main(void) { // Initialize hardware initHW(); // Display greeting putsUart0("Serial Example\r\n"); putsUart0("Press '0' or '1'\r\n"); putcUart0('a'); BLUE_LED = 1; // Wait for PB press waitPbPress(); // For each received character, toggle the red LED // For each received "1", set the green LED // For each received "0", clear the green LED while(1) { char c = getcUart0(); RED_LED ^= 1; if (c == '1') GREEN_LED = 1; if (c == '0') GREEN_LED = 0; } }
main(int argc, char **argv) { pthread_t ctlThr, evThr; char *hostname; int port; // if (argc != 3) { fprintf(stderr, "Usage: %s host-name port\n", argv[0]); fflush(stderr); exit(-1); } hostname = argv[1]; if ((port = atoi(argv[2])) <= 0) { fprintf(stderr, "Bad port number: %s\n", argv[2]); fflush(stderr); exit(-1); } // initHW(hostname, port); fprintf(stdout, "singe thread ...\n"); fprintf(stdout, "waiting for 5 seconds .. \n"); sleep(5); fprintf(stdout, "opening all doors\n"); handleDoor(0, DoorOpen); fprintf(stdout, "waiting for 3 seconds .. \n"); sleep(3); fprintf(stdout, "closing all doors\n"); handleDoor(0, DoorClose); fprintf(stdout, "asking \"where is cabin #1?\"\n"); whereIs(1); { EventDesc ed; fprintf(stdout, "output: type=%d\n", (int) waitForEvent(&ed)); } fprintf(stdout, "waiting for 3 seconds .. \n"); sleep(3); fprintf(stdout, "starting threads ...\n"); fflush(stdout); if (pthread_mutex_init(&mutex, NULL) < 0) { perror("pthread_mutex_init"); exit(1); } if (pthread_create(&ctlThr, NULL, ctlProc, (void *) 0) != 0) { perror("pthread_create"); exit(-1); } if (pthread_create(&evThr, NULL, evProc, (void *) 0) != 0) { perror("pthread_create"); exit(-1); } (void) pthread_join(ctlThr, NULL); (void) pthread_join(evThr, NULL); return 0; }
// With this setup it would seem like main() must be the first function in this file, otherwise // the wrong function gets called on reset. int main(void) { unsigned long ulLoop; char buffer[32]; SecondsCount = 0; minutesCount = 0; initHW(); // Start the OLED display and write a message on it RIT128x96x4Init(ulSSI_FREQUENCY); RIT128x96x4StringDraw("Hi :)", 0, 0, mainFULL_SCALE); RIT128x96x4StringDraw("Running Timers...", 0, 10, mainFULL_SCALE); // // Loop forever. // while (1) { // // Turn on the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0); // GPIO_PORTF_DATA_R |= 0x01; RIT128x96x4StringDraw(" ", 50, 50, mainFULL_SCALE); itoa(SecondsCount, buffer, 10); RIT128x96x4StringDraw(buffer, 50, 50, mainFULL_SCALE); RIT128x96x4StringDraw(" ", 50, 60, mainFULL_SCALE); itoa(minutesCount, buffer, 10); RIT128x96x4StringDraw(buffer, 50, 60, mainFULL_SCALE); SysCtlSleep(); // // Turn off the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0); // // Delay for a bit. // for (ulLoop = 0; ulLoop < 200000; ulLoop++) { } } return 0; }
//! With this setup it would seem like main() must be the first function in this file, otherwise //! the wrong function gets called on reset. void main(void) { volatile INT32U ulLoop; volatile INT16U event; volatile INT16U push; //Hardware upstarts initHW(); //! Start the OLED display and write a message on it RIT128x96x4Init(ulSSI_FREQUENCY); RIT128x96x4StringDraw("EMP", 15, 42, mainFULL_SCALE); RIT128x96x4StringDraw("enter the code.....", 5, 49, mainFULL_SCALE); RIT128x96x4StringDraw("SW2 SW3 SW4 SW5 SW6", 15, 57, mainFULL_SCALE); // Entry Password see under inputs // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)); // Clean the OLED display. RIT128x96x4Clear(); // // Loop forever. // while (1) { // Statmashine function // This is where a statemachine could be added event = GetKeyEvents(); push = select_button(); statemashine(event , push); //all functions the } }
void IOI2CLM6x::processPowerEvent ( UInt32 eventType ) { switch ( eventType ) { case kI2CPowerEvent_OFF: case kI2CPowerEvent_SLEEP: { require_success( saveRegisters(), IOI2CLM6x_processPowerEvent_saveRegistersErr ); fRegistersAreSaved = true; break; } case kI2CPowerEvent_ON: case kI2CPowerEvent_WAKE: { if ( fRegistersAreSaved ) { // Full Power State require_success( restoreRegisters(), IOI2CLM6x_processPowerEvent_restoreRegistersErr ); fRegistersAreSaved = false; } break; } case kI2CPowerEvent_STARTUP: { require_success( initHW(), IOI2CLM6x_processPowerEvent_initHWErr ); break; } } IOI2CLM6x_processPowerEvent_initHWErr: IOI2CLM6x_processPowerEvent_saveRegistersErr: return; IOI2CLM6x_processPowerEvent_restoreRegistersErr: fRegistersAreSaved = false; return; }
//! With this setup it would seem like main() must be the first function in this file, otherwise //! the wrong function gets called on reset. int main(void) { volatile unsigned long ulLoop; volatile int event; //Hardware upstarts initHW(); //! Start the OLED display and write a message on it RIT128x96x4Init(ulSSI_FREQUENCY); RIT128x96x4StringDraw("Home App Control", 5, 42, mainFULL_SCALE); RIT128x96x4StringDraw("enter the code.....", 5, 49, mainFULL_SCALE); // Entry Password see under inputs // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)); // Clean the OLED display. RIT128x96x4Clear(); // // Loop forever. // while (1) { // This is where a statemachine could be added // Statmashine function // This is where a statemachine could be added // event = GetKeyEvents(); statemashine(GetKeyEvents()); //all functions the } }
bool AppleLM8x::start(IOService *provider) { mach_timespec_t WaitTimeOut; OSData *t; int *reg; IOReturn status; // Proper etiquette if ( !(super::start(provider)) ) return false; // Set flag to reflect non-restart state systemIsRestarting = FALSE; // Try and get the reg property if ( !(t = OSDynamicCast(OSData, provider->getProperty("reg"))) ) { IOLog( "AppleLM8x::start couldn't find 'reg' property in registry.\n"); return false; } // We have the reg property, lets make local copy if ( !(reg = (int*)t->getBytesNoCopy()) ) { IOLog( "AppleLM8x::start 'reg' property is present but empty.\n"); return false; } kLM8xBus = (UInt8)(*reg >> 8); // Determine I2C bus kLM8xAddr = (UInt8)(*reg >> 1); // Determine true address of device // Use a 30 second timeout when calling waitForService() WaitTimeOut.tv_sec = 30; WaitTimeOut.tv_nsec = 0; // Create some symbols for later use sOpenI2Cbus = OSSymbol::withCStringNoCopy(kOpenI2Cbus); sCloseI2Cbus = OSSymbol::withCStringNoCopy(kCloseI2Cbus); sSetPollingMode = OSSymbol::withCStringNoCopy(kSetPollingMode); sSetStandardSubMode = OSSymbol::withCStringNoCopy(kSetStandardSubMode); sSetCombinedMode = OSSymbol::withCStringNoCopy(kSetCombinedMode); sWriteI2Cbus = OSSymbol::withCStringNoCopy(kWriteI2Cbus); sReadI2Cbus = OSSymbol::withCStringNoCopy(kReadI2Cbus); sGetSensorValueSym = OSSymbol::withCString("getSensorValue"); interface = OSDynamicCast(IOService, provider->getParentEntry(gIOServicePlane)); if(interface == NULL) { IOLog("AppleLM8x::start(0x%x) failed to get i2c interface\n", kLM8xAddr<<1); return(false); } DLOG("AppleLM8x::start(0x%x) got i2c interface %s\n", kLM8xAddr<<1, interface->getName()); // Configure the LM87 if( initHW(provider) != kIOReturnSuccess ) { IOLog("AppleLM8x::start(0x%x) failed to initialize sensor.\n", kLM8xAddr<<1); return false; } // Save registers for PM if ( saveRegisters() != kIOReturnSuccess ) IOLog("AppleLM8x::powerStateWillChangeTo(0x%x) failed to save registers.\n", kLM8xAddr<<1); // Initialize Power Management superclass variables from IOService.h PMinit(); // Register as controlling driver from IOService.h status = registerPowerDriver( this, (IOPMPowerState *) ourPowerStates, kLM8xNumStates ); if (status != kIOReturnSuccess) { IOLog("%s: Failed to registerPowerDriver.\n", getName()); } // Join the Power Management tree from IOService.h provider->joinPMtree( this); // Install power change handler (for restart notification) registerPrioritySleepWakeInterest(&sysPowerDownHandler, this, 0); // Register so others can find us with a waitForService() registerService(); // Parse sensor properties and create nubs publishChildren(provider); return true; }
int main(int argc, const char *argv[]) { poptContext optCon; /* context for parsing command-line options */ int rc, version = 0, debugMask = 0, pipeFD[2] = {-1, -1}, magic = FORKMAGIC; char *logdest = NULL, *configfile = "/etc/parastation.conf"; FILE *logfile = NULL; struct poptOption optionsTable[] = { { "debug", 'd', POPT_ARG_INT, &debugMask, 0, "enable debugging with mask <mask>", "mask"}, { "configfile", 'f', POPT_ARG_STRING, &configfile, 0, "use <file> as config-file (default is /etc/parastation.conf)", "file"}, { "logfile", 'l', POPT_ARG_STRING, &logdest, 0, "use <file> for logging (default is syslog(3))." " <file> may be 'stderr' or 'stdout'", "file"}, { "version", 'v', POPT_ARG_NONE, &version, 0, "output version information and exit", NULL}, POPT_AUTOHELP { NULL, '\0', 0, NULL, 0, NULL, NULL} }; optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); rc = poptGetNextOpt(optCon); /* Store arguments for later modification in forwarders, etc. */ PSID_argc = argc; PSID_argv = argv; if (version) { printVersion(); return 0; } if (logdest) { if (strcasecmp(logdest, "stderr")==0) { logfile = stderr; } else if (strcasecmp(logdest, "stdout")==0) { logfile = stdout; } else { logfile = fopen(logdest, "a+"); if (!logfile) { char *errstr = strerror(errno); fprintf(stderr, "Cannot open logfile '%s': %s\n", logdest, errstr ? errstr : "UNKNOWN"); exit(1); } } } if (!logfile) { openlog("psid", LOG_PID|LOG_CONS, LOG_DAEMON); } PSID_initLogs(logfile); printWelcome(); if (rc < -1) { /* an error occurred during option processing */ poptPrintUsage(optCon, stderr, 0); PSID_log(-1, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); if (!logfile) fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); return 1; } /* Save some space in order to modify the cmdline later on */ PSC_saveTitleSpace(PSID_argc, PSID_argv, 1); if (logfile!=stderr && logfile!=stdout) { /* Daemonize only if neither stdout nor stderr is used for logging */ if (pipe(pipeFD) < 0) { PSID_exit(errno, "unable to create pipe"); } /* Start as daemon */ switch (fork()) { case -1: PSID_exit(errno, "unable to fork server process"); break; case 0: /* I'm the child (and running further) */ close (pipeFD[0]); break; default: /* I'm the parent and exiting */ close (pipeFD[1]); /* Wait for child's magic data */ rc = read(pipeFD[0], &magic, sizeof(magic)); if (rc != sizeof(magic) || magic != (FORKMAGIC)) return -1; return 0; } } #define _PATH_TTY "/dev/tty" /* First disconnect from the old controlling tty. */ { int fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); if (fd >= 0) { if (ioctl(fd, TIOCNOTTY, NULL)) { PSID_warn(-1, errno, "%s: ioctl(TIOCNOTTY)", __func__); } close(fd); } } /* * Disable stdin,stdout,stderr and install dummy replacement * Take care if stdout/stderr is used for logging */ { int dummy_fd; dummy_fd=open("/dev/null", O_WRONLY , 0); dup2(dummy_fd, STDIN_FILENO); if (logfile!=stdout) dup2(dummy_fd, STDOUT_FILENO); if (logfile!=stderr) dup2(dummy_fd, STDERR_FILENO); close(dummy_fd); } /* Forget about inherited window sizes */ unsetenv("LINES"); unsetenv("COLUMNS"); if (debugMask) { PSID_setDebugMask(debugMask); PSC_setDebugMask(debugMask); PSID_log(-1, "Debugging mode (mask 0x%x) enabled\n", debugMask); } /* Init the Selector facility as soon as possible */ if (!Selector_isInitialized()) Selector_init(logfile); PSID_registerLoopAct(Selector_gc); /* Initialize timer facility explicitely to ensure correct logging */ if (!Timer_isInitialized()) Timer_init(logfile); /* * Create the Local Service Port as early as possible. Actual * handling is enabled later. This gives psiadmin the chance to * connect. Additionally, this will guarantee exclusiveness */ PSID_createMasterSock(PSmasterSocketName); PSID_checkMaxPID(); /* read the config file */ PSID_readConfigFile(logfile, configfile); /* Now we can rely on the config structure */ { in_addr_t addr; PSID_log(-1, "My ID is %d\n", PSC_getMyID()); addr = PSIDnodes_getAddr(PSC_getMyID()); PSID_log(-1, "My IP is %s\n", inet_ntoa(*(struct in_addr *) &addr)); } if (!logfile && PSID_config->logDest!=LOG_DAEMON) { PSID_log(-1, "Changing logging dest from LOG_DAEMON to %s\n", PSID_config->logDest==LOG_KERN ? "LOG_KERN": PSID_config->logDest==LOG_LOCAL0 ? "LOG_LOCAL0" : PSID_config->logDest==LOG_LOCAL1 ? "LOG_LOCAL1" : PSID_config->logDest==LOG_LOCAL2 ? "LOG_LOCAL2" : PSID_config->logDest==LOG_LOCAL3 ? "LOG_LOCAL3" : PSID_config->logDest==LOG_LOCAL4 ? "LOG_LOCAL4" : PSID_config->logDest==LOG_LOCAL5 ? "LOG_LOCAL5" : PSID_config->logDest==LOG_LOCAL6 ? "LOG_LOCAL6" : PSID_config->logDest==LOG_LOCAL7 ? "LOG_LOCAL7" : "UNKNOWN"); closelog(); openlog("psid", LOG_PID|LOG_CONS, PSID_config->logDest); printWelcome(); } /* call startupScript, if any */ if (PSID_config->startupScript && *PSID_config->startupScript) { int ret = PSID_execScript(PSID_config->startupScript, NULL, NULL, NULL); if (ret > 1) { PSID_log(-1, "startup script '%s' failed. Exiting...\n", PSID_config->startupScript); PSID_finalizeLogs(); exit(1); } } /* Setup handling of signals */ initSigHandlers(); if (PSID_config->coreDir) { if (chdir(PSID_config->coreDir) < 0) { PSID_warn(-1, errno, "Unable to chdir() to coreDirectory '%s'", PSID_config->coreDir); } } PSIDnodes_setProtoV(PSC_getMyID(), PSProtocolVersion); PSIDnodes_setDmnProtoV(PSC_getMyID(), PSDaemonProtocolVersion); PSIDnodes_setHWStatus(PSC_getMyID(), 0); PSIDnodes_setKillDelay(PSC_getMyID(), PSID_config->killDelay); PSIDnodes_setAcctPollI(PSC_getMyID(), PSID_config->acctPollInterval); /* Bring node up with correct numbers of CPUs */ declareNodeAlive(PSC_getMyID(), PSID_getPhysCPUs(), PSID_getVirtCPUs()); /* Initialize timeouts, etc. */ PSID_initStarttime(); /* initialize various modules */ PSIDcomm_init(); /* This has to be first since it gives msgHandler hash */ PSIDclient_init(); initState(); initOptions(); initStatus(); initSignal(); PSIDspawn_init(); initPartition(); initHW(); initAccount(); initInfo(); initEnvironment(); /* Plugins shall be last since they use most of the ones before */ initPlugins(); /* Now we start all the hardware -- this might include the accounter */ PSID_log(PSID_LOG_HW, "%s: starting up the hardware\n", __func__); PSID_startAllHW(); /* * Prepare hostlist to initialize RDP and MCast */ { in_addr_t *hostlist; int i; hostlist = malloc(PSC_getNrOfNodes() * sizeof(unsigned int)); if (!hostlist) { PSID_exit(errno, "Failed to get memory for hostlist"); } for (i=0; i<PSC_getNrOfNodes(); i++) { hostlist[i] = PSIDnodes_getAddr(i); } if (PSID_config->useMCast) { /* Initialize MCast */ int MCastSock = initMCast(PSC_getNrOfNodes(), PSID_config->MCastGroup, PSID_config->MCastPort, logfile, hostlist, PSC_getMyID(), MCastCallBack); if (MCastSock<0) { PSID_exit(errno, "Error while trying initMCast"); } setDeadLimitMCast(PSID_config->deadInterval); PSID_log(-1, "MCast and "); } else { setStatusTimeout(PSID_config->statusTimeout); setMaxStatBCast(PSID_config->statusBroadcasts); setDeadLimit(PSID_config->deadLimit); setTmOutRDP(PSID_config->RDPTimeout); } /* Initialize RDP */ RDPSocket = RDP_init(PSC_getNrOfNodes(), PSIDnodes_getAddr(PSC_getMyID()), PSID_config->RDPPort, logfile, hostlist, PSIDRDP_handleMsg, RDPCallBack); if (RDPSocket<0) { PSID_exit(errno, "Error while trying initRDP"); } PSID_log(-1, "RDP (%d) initialized.\n", RDPSocket); free(hostlist); } /* Now start to listen for clients */ PSID_enableMasterSock(); /* Once RDP and the master socket are ready parents might be released */ if (pipeFD[1] > -1) { if (write(pipeFD[1], &magic, sizeof(magic)) <= 0) { /* We don't care */ } close(pipeFD[1]); } PSID_log(-1, "SelectTime=%d sec DeadInterval=%d\n", PSID_config->selectTime, PSID_config->deadInterval); /* Trigger status stuff, if necessary */ if (PSID_config->useMCast) { declareMaster(PSC_getMyID()); } else { int id = 0; while (id < PSC_getMyID() && (send_DAEMONCONNECT(id) < 0 && errno == EHOSTUNREACH)) { id++; } if (id == PSC_getMyID()) declareMaster(id); } /* * Main loop */ while (1) { int res = Swait(PSID_config->selectTime * 1000); if (res < 0) PSID_warn(-1, errno, "Error while Swait()"); /* Handle actions registered to main-loop */ PSID_handleLoopActions(); } }
// With this setup it would seem like main() must be the first function in this file, otherwise // the wrong function gets called on reset. void main(void) { volatile unsigned long ulLoop; volatile int event; initHW(); // Start the OLED display and write a message on it RIT128x96x4Init(ulSSI_FREQUENCY); RIT128x96x4StringDraw("Hi :)", 0, 0, mainFULL_SCALE); RIT128x96x4StringDraw("enter the code ...", 0, 5, mainFULL_SCALE); // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_0)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_1)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_2)) ; // Wait for the select key to be pressed while (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_3)) ; // // Loop forever. // while (1) { // This is where a statemachine could be added event = GetKeyEvents(); if (event == KEY_ENTER) RIT128x96x4StringDraw("Enter Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_UP) RIT128x96x4StringDraw("up Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_DOWN) RIT128x96x4StringDraw("down Pressed", 0, 0, mainFULL_SCALE); if (event == KEY_CANCEL) RIT128x96x4StringDraw("cancel Pressed", 0, 0, mainFULL_SCALE); // // Turn on the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0); // GPIO_PORTF_DATA_R |= 0x01; // // Delay for a bit. // This is BAD STYLE (tm) any embedded system should be either free-running or timer based for (ulLoop = 0; ulLoop < 200000; ulLoop++) { } // // Turn off the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0); // // Delay for a bit. // for (ulLoop = 0; ulLoop < 200000; ulLoop++) { } } }