void CFan::run() { CTimeout tm; // time count for RPM tm.reset(); while(isAlive()) { // check thread alive // // wait for sense pin trigger (interrupt) // if ( m_sense.wait(FAN_RPM_TIMEOUT) ) { if ( m_sense==LOW ) { // filter noise (for Falling only) // // RPM calculate // m_rpmcount++; if ( m_rpmcount>=20 ) { // Update RPM every 20 counts, increase this for better RPM resolution, // decrease for faster update m_rpm = (FAN_RPM_RESOLUTION * 1000 * m_rpmcount) / tm.elapsed(); tm.reset(); m_rpmcount = 0; } // end of if ( m_rpmcount>=20 ) } } else { // // timeout // m_ctrl.dutyCycle(0.0f); // turn off fan m_rpm = 0; } } }
// // Calculate Echo Pulse width ( // return: centimeter (CM) // uint32_t HCSR04::distance(uint32_t timeout) { CTick tick; CTimeout to; to.reset(); // trigger m_trig = HIGH; tick.delay(10); // 10us m_trig = LOW; // wait echo LOW->HIGH while (m_echo != HIGH) { if (to.isExpired(timeout)) return 0; } // wait echo HIGH->LOW tick.reset(); while (m_echo == HIGH) { if (to.isExpired(timeout)) return 0; } return (tick.elapsed() * 3432 / 20000); // unit (millimeter) }
// // iProbe run loop // void iProbe::run() { CTimeout tm; CString msg; // uint32_t m_lstCPU = 0; uint32_t m_lstNET = 0; while(1) { tm.wait(1000); tm.reset(); // // Make JSON string // msg = "{"; msg += "\"cpu\":"; msg += map(CThread::getIdleTickCount(), 0, 2000, 0, 100); msg += ", \"net\":"; msg += constrain(OFFSET(CSocket::getRxPackageCount(), m_lstNET, MAX_UINT32), 0, 100); msg += ", \"mem\":"; msg += map(heapAvailableSize(), 0, MAX_USERS_MEMORY, 100, 0); msg += "}"; // // Send JSON message to All mail receiver // CMailBox::post(xMAIL_PROBE, msg.getBuffer()); // // m_lstCPU = CThread::getIdleTickCount(); m_lstNET = CSocket::getRxPackageCount(); } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // PWM (Using Timer1) // hwPWM pwm1(TIMER_1, 5, 6, 7); // set pwm1 pins on P0.5 (CH1), P0.6 (CH2) and P0.7 (CH3) pwm1.period(0.0002); // period time = 200us pwm1.enable(); // enable PWM module // update pwm2 channels duty-cycle (can be updated in any-time) pwm1.dutycycle(PWM_CH_1, 0.8f); // CH1 duty-cycle = 80% pwm1.dutycycle(PWM_CH_2, 0.6f); // CH2 duty-cycle = 60% pwm1.dutycycle(PWM_CH_3, 0.2f); // CH3 duty-cycle = 20% // // PWM (Using Timer2) // hwPWM pwm2(TIMER_2, LED_PIN_1, LED_PIN_2); // set pwm2 pins on LED1 (CH1) and LED2 pwm2.period(0.0005); // period time = 500us pwm2.enable(); // enable PWM module // update pwm2 channels duty-cycle (can be updated in any-time) pwm2.dutycycle(PWM_CH_1, 0.8f); // CH1 duty-cycle = 80% pwm2.dutycycle(PWM_CH_2, 0.1f); // CH2 duty-cycle = 10% // // LED // CPin led(LED_PIN_0); led.output(); CTimeout tmLED; // // Enter main loop. // while(1) { // // FireFly loop // if ( tmLED.isExpired(500) ) { tmLED.reset(); led.toggle(); } } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Your setup code here // CButton btn(BUTTON_PIN_0); CBuzzer buz(15); // buzzer on P0.15 buz.enable(); CPin led(LED_PIN_0); led.output(); CTimeout tmLED; // // Enter main loop. // while(1) { // // Your loop code here // switch(btn.isPressed()) { case BTN_PRESSED: buz.post(3); // turn on buzzer x 3 break; case BTN_RELEASED: break; case BTN_NOTHING: break; } if ( tmLED.isExpired(500) ) { tmLED.reset(); led.toggle(); } } }
void CButton::run() { CTimeout bounce; while(1) { if ( bit_chk(m_flag, KEY_FLAG_DOWN) ) { if ( m_pin==HIGH ){ if ( bounce.isExpired(10) ) { bit_clr(m_flag, KEY_FLAG_DOWN); onKeyUp(); // call virtual function "onKeyUp()" } } else { bounce.reset(); } } else { if ( m_pin==LOW ){ if ( bounce.isExpired(5) ) { bit_set(m_flag, KEY_FLAG_DOWN); onKeyDown(); // call virtual function "onKeyDown()" } } else { bounce.reset(); } } } }
uint32_t pulseIn(int pin, PIN_LEVEL_T value, uint32_t timeout) { CPin p(unoPIN[pin]); p.input(); CTick tick; CTimeout to; to.reset(); while( p!=value ) { if ( timeout && to.isExpired(timeout) ) return 0; } tick.reset(); while( p==value ) { if ( timeout && to.isExpired(timeout) ) return 0; } return tick.elapsed(); }
/* ============================================== main task routine ============================================== */ int main(void) { pool_memadd((uint32_t) pool, sizeof(pool)); #ifdef DEBUG dbg.start(); #endif eMBErrorCode eStatus; // dbg.waitToDebugMode(); #if MB_TCP_ENABLED == 1 eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ); #endif if (eStatus != MB_ENOERR) dbg.println("can't initialize modbus stack!"); /* Enable the Modbus Protocol Stack. */ eStatus = eMBEnable(); if (eStatus != MB_ENOERR) dbg.println("can't enable modbus stack!"); // Initialise some registers usRegInputBuf[1] = 0x1234; usRegInputBuf[2] = 0x5678; usRegInputBuf[3] = 0x9abc; // debug LED CPin led(LED1); CTimeout tm; // Enter an endless loop while (1) { if ( tm.read()>0.5 ) { led = !led; tm.reset(); } eStatus = eMBPoll( ); /* Here we simply count the number of poll cycles. */ usRegInputBuf[0]++; } return 0; }
void CButtons::run() { CTimeout tm; uint32_t newval; while(1) { newval = m_pins; if ( newval != m_flag ) { if ( tm.isExpired(10) ) { // wait for bounce time m_down = newval; for (int i=0; i<m_pins.count(); i++) { if ( bit_chk((m_flag ^ newval), i) ) { // is different? Key Even caused... if ( bit_chk(newval, i) ) { // HIGH is key up onKeyUp(i); } else { // LOW is key down onKeyDown(i); } } } m_flag = newval; } } else { tm.reset(); } } }