@@ -11,24 +11,34 @@ enum PowerStatus {
1111    Battery
1212};
1313
14- uint8_t  checkTakaoBasePowerStatus (m5::Power_Class* power, StackchanSERVO* servo ) {
15-   bool  last_ext_output = power->getExtOutput ();
16-   if  (power->Axp192 .getACINVoltage () > 1 .0f ) {
14+ PowerStatus checkTakaoBasePowerStatus (m5::Power_Class* power, int16_t  battery_threshold = 3200 ) {
15+   if  (!power->getExtOutput () && power->getBatteryCurrent () < 0 ) {
16+     //  TakaoBase使用時(ExtOutput=falseの場合)、バッテリー駆動の時はtrueに切り替える。 
17+     //  切り替えないとサーボが動きません。
1718    power->setExtOutput (true );
19+     power->setLed (0 );
20+     return  PowerStatus::Battery;  
21+   }
22+ 
23+   if  (power->getExtOutput () && (power->getBatteryCurrent () >= 0 )) {
24+     //  M5StackのUSB-C端子から給電されている状態。
1825    power->setLed (80 );
19-     return  PowerStatus::SidePower;   
26+     return  PowerStatus::SidePower;
2027  }
21-   while  (servo->isMoving ()) {delay (1 );} //  サーボが動いている間は待機(そうしないとサーボの動きが乱れる。)
22-   power->setExtOutput (false ); //  後側のUSB-Cの状態を把握するためにfalseにする必要があります。
23-   delay (500 );
24-   if  (power->Axp192 .getBatteryDischargeCurrent () > 3 .0f ) {
25-     power->setExtOutput (true );
26-     power->setLed (0 );
27-     return  PowerStatus::Battery;
28+ 
29+   if  (power->getBatteryLevel () < battery_threshold) {
30+     //  Batteryの電圧が閾値よりも下がったときの処理
31+     power->setExtOutput (false ); //  後側のUSB-Cの給電状態を把握するためにfalseにする必要があります。
32+     if  (power->getBatteryCurrent () > 0 ) {
33+       //  後ろから給電されている状態。
34+       power->setLed (80 );
35+       return  PowerStatus::BackPower; 
36+     } else  {
37+       //  給電されていない場合は電源OFF
38+       M5.Power .powerOff ();
39+     }
2840  }
29-   power->setExtOutput (false );
30-   power->setLed (80 );
31-   return  PowerStatus::BackPower; 
41+   return  PowerStatus::BackPower;
3242}
3343
3444#endif  //  STACKCHAN_TAKAO_BASE_HPP
0 commit comments