NoSloppy

Untitled

Mar 14th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.46 KB | None | 0 0
  1. // sa22c props file, includes 1,2 and 3 button modes. Incorporates multi-blast,
  2. // battle mode and gesture ignitions from fett263 plus on-the-fly volume
  3. // controls and full access to all features with 1,2 or 3 button sabers
  4. //
  5. // New #define SA22C_NO_LOCKUP_HOLD
  6. // reverts to lockup being triggered only by clash + aux in 2-button mode
  7. // Also sets multi-blast to trigger while holding aux and swinging, rather than
  8. // double click and hold
  9. //
  10. // Gesture Controls
  11. // There are four gesture types: swing, stab, twist and thrust. For simplicity,
  12. // using gesture ignition will automatically skip the preon effect.
  13. // Below are the options to add to the config to enable
  14. // the various gestures
  15. //
  16. // #define SA22C_STAB_ON
  17. // #define SA22C_SWING_ON
  18. // #define SA22C_TWIST_ON
  19. // #define SA22C_THRUST_ON
  20. // #define SA22C_TWIST_OFF
  21. // #define SA22C_FORCE_PUSH
  22. //
  23. // #define SA22C_FORCE_PUSH_LENGTH 5
  24. // Allows for adjustment to Push gesture length in millis needed to trigger Force Push
  25. // Recommended range 1 ~ 10, 1 = shortest, easiest to trigger, 10 = longest
  26. //
  27. // If you want the gesture ignition to ALSO enter battle mode automatically
  28. // on ignition, add this define
  29. //
  30. // #define GESTURE_AUTO_BATTLE_MODE
  31. //
  32. // Battle mode by fett263
  33. //
  34. // Once you enter battle mode, button functions will be disabled for lockup
  35. // stab, melt, etc. Blaster blocks and lightning block will continue to be
  36. // triggered by button controls. Automatic lockup/clash detection works
  37. // by measuring delay of the saber blade pulling back from the clash.
  38. // If you clash the blade and it does not pull back during the delay period,
  39. // it is assumed to be a lockup and the lockup effect will show on the blade.
  40. // If you clash the blade and pull away, only the bgn/end lockup effects will show.
  41. //
  42. // You can adjust the threshold of this detection by using
  43. // #define SA22C_LOCKUP_DELAY (insert number here)
  44. // Default value is 200
  45. //
  46. // Battle mode features automatic clash and lockup detection plus a new
  47. // force push mode that will play a force or force push sound with a controlled
  48. // pushing gesture. Automatic clash/lockup uses the pre and post lock effects
  49. // so your blade style and font MUST have those capabilities to support
  50. // battle mode. Kudos to fett263 for his very impressive additions for OS 5
  51. //
  52. // Tightened click timings
  53. // I've shortened the timeout for short and double click detection from 500ms
  54. // to 300ms. I think it feels more responsive this way but still gives enough
  55. // timeout to ensure all button actions can be achieved consistently
  56. // I've included all button timings so they can be easily tweaked to suit
  57. // individual tastes.
  58. //
  59. // Button configs:
  60.  
  61. //0 Button
  62. //make sure you have the following in your top config
  63. //#define SA22C_TWIST_OFF
  64. //#define SA22C_THRUST_ON
  65. //#define SA22C_TWIST_ON
  66. //#define MOTION_TIMEOUT 60 * 12000 * 1000
  67. //#define SA22C_STAB_ON
  68.  
  69.  
  70. // Activate Muted - none
  71. // Activate - thurst while point up for normal activation, thurst while pointing down for fast activation
  72. // Play/Stop Music - clash while saber is off and pointing up
  73. // Turn off blade - point down and twist while ON
  74. // Next Preset - twist while OFF and point down
  75. // Prev Preset - twist while OFF and point up
  76. // Lockup - none
  77. // Stab - thrust forward
  78. // Lightning Block - none
  79. // Force - shake the saber up and down a bit while pointing up and saber on
  80. // Melt - none
  81. // Drag - none
  82. // Blaster Blocks - point up twist while on
  83. // Multi-Blast - none
  84.  
  85. // Color Change mode - shake the saber up and down a bit while pointing down and saber on, shake again to exit while pointing down
  86. // Volume up - shake the saber up and down a bit while pointing up and saber off
  87. // Volume down - shake the saber up and down a bit while pointing down and saber off
  88. // Battery Level - stab while saber is off and pointing up
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. // 1 Button:
  107. // Activate Muted - double click and hold while OFF
  108. // Activate - short click while OFF
  109. // Play/Stop Music - double click while OFF
  110. // Turn off blade - hold and wait till blade is off while ON
  111. // Next Preset - hold and release while OFF
  112. // Prev Preset - hold and wait while OFF
  113. // Lockup - hold + hit clash while ON
  114. // Stab - thrust forward clash while ON
  115. // Lightning Block - double click and hold while ON
  116. // Melt - hold + thust forward clash while ON
  117. // Drag - hold + hit clash while ON pointing the blade tip down
  118. // Blaster Blocks - short click/double click/triple click while on
  119. // Multi-Blast - hold while swinging for one second and release
  120. // to trigger blaster block, swing saber while in multi-blast mode
  121. // to exit, hold while swinging for one second and release
  122. // Battle Mode - triple-click and hold while on
  123. // Force Effects - hold + twist the hilt while ON (while pointing up)
  124. // Color Change mode - hold + twist the hilt while ON (pointing down)
  125. // Enter Volume - Menu hold + clash while OFF
  126. // Volume UP - hold and release while in Volume Menu
  127. // Volume DOWN - click while in Volume Menu
  128. // Exit Volume Menu - Menu hold + clash while OFF
  129. // Battery Level - triple click while OFF
  130. //
  131. //
  132. // 2 Button:
  133. // POWER
  134. // Activate Muted - double click and hold while OFF
  135. // Activate - short click while OFF
  136. // Play/Stop Music - hold and release while OFF
  137. // Turn off blade - hold and wait till blade is off while ON
  138. // Force Effects - double click while ON
  139. // Volume UP - short click while OFF and in VOLUME MENU
  140. // Prev Preset - hold and wait while OFF
  141. // Color Change mode - hold + toggle AUX while ON
  142. // Lightning Block - double click and hold while ON
  143. // Melt - hold while stabbing (clash with forward motion, horizontal)
  144. // Battle Mode - triple-click and hold for half a second while on
  145.  
  146. // AUX
  147. // Blaster blocks - short click/double click/triple click while ON
  148. // Multi-Blast - double-click and hold for half a second
  149. // to trigger blaster block, swing saber while in multi-blast mode
  150. // to exit, double-click and hold for half a second
  151. // Next Preset - short click while OFF
  152. // Lockup - hold while ON
  153. // Drag - hold while ON pointing the blade tip down
  154. // Enter VOLUME MENU - long click while OFF
  155. // Volume down - short click while OFF and in VOLUME MENU
  156. // Battery level - hold while off
  157. //
  158. // 3 Button: Same as two button except for the following
  159. //
  160. // AUX2
  161. // Lightning Block - hold while ON
  162. // Battle Mode - double-click and hold while on
  163. // Previous Preset - short click while OFF
  164.  
  165.  
  166. #ifndef PROPS_SABER_SA22C_BUTTONS_H
  167. #define PROPS_SABER_SA22C_BUTTONS_H
  168.  
  169. #include "prop_base.h"
  170. #include "../sound/hybrid_font.h"
  171.  
  172. #undef PROP_TYPE
  173. #define PROP_TYPE SaberSA22CButtons
  174.  
  175. #ifndef MOTION_TIMEOUT
  176. #define MOTION_TIMEOUT 60 * 3000 * 1000
  177. #endif
  178.  
  179. #ifndef SA22C_SWING_ON_SPEED
  180. #define SA22C_SWING_ON_SPEED 1000
  181. #endif
  182.  
  183. #ifndef SA22C_LOCKUP_DELAY
  184. #define SA22C_LOCKUP_DELAY 200
  185. #endif
  186.  
  187. #ifndef SA22C_FORCE_PUSH_LENGTH
  188. #define SA22C_FORCE_PUSH_LENGTH 5
  189. #endif
  190.  
  191. #ifndef BUTTON_DOUBLE_CLICK_TIMEOUT
  192. #define BUTTON_DOUBLE_CLICK_TIMEOUT 300
  193. #endif
  194.  
  195. #ifndef BUTTON_SHORT_CLICK_TIMEOUT
  196. #define BUTTON_SHORT_CLICK_TIMEOUT 300
  197. #endif
  198.  
  199. #ifndef BUTTON_HELD_TIMEOUT
  200. #define BUTTON_HELD_TIMEOUT 300
  201. #endif
  202.  
  203. #ifndef BUTTON_HELD_MEDIUM_TIMEOUT
  204. #define BUTTON_HELD_MEDIUM_TIMEOUT 1000
  205. #endif
  206.  
  207. #ifndef BUTTON_HELD_LONG_TIMEOUT
  208. #define BUTTON_HELD_LONG_TIMEOUT 2000
  209. #endif
  210.  
  211. #ifdef SA22C_SWING_ON
  212. #define SWING_GESTURE
  213. #endif
  214.  
  215. #ifdef SA22C_STAB_ON
  216. #define STAB_GESTURE
  217. #endif
  218.  
  219. #ifdef SA22C_TWIST_ON
  220. #define TWIST_GESTURE
  221. #endif
  222.  
  223. #ifdef SA22C_THRUST_ON
  224. #define THRUST_GESTURE
  225. #endif
  226.  
  227. #define FORCE_PUSH_CONDITION battle_mode_
  228.  
  229. EFFECT(dim); // for EFFECT_POWERSAVE
  230. EFFECT(battery); // for EFFECT_BATTERY_LEVEL
  231. EFFECT(bmbegin); // for Begin Battle Mode
  232. EFFECT(bmend); // for End Battle Mode
  233. EFFECT(vmbegin); // for Begin Volume Menu
  234. EFFECT(vmend); // for End Volume Menu
  235. EFFECT(volup); // for increse volume
  236. EFFECT(voldown); // for decrease volume
  237. EFFECT(volmin); // for minimum volume reached
  238. EFFECT(volmax); // for maximum volume reached
  239. EFFECT(faston); // for EFFECT_FAST_ON
  240. EFFECT(blstbgn); // for Begin Multi-Blast
  241. EFFECT(blstend); // for End Multi-Blast
  242. EFFECT(push); // for Force Push gesture in Battle Mode
  243.  
  244. // The Saber class implements the basic states and actions
  245. // for the saber.
  246. class SaberSA22CButtons : public virtual PropBase {
  247. public:
  248. SaberSA22CButtons() : PropBase() {}
  249. const char* name() override { return "SaberSA22CButtons"; }
  250.  
  251. void Loop() override {
  252. PropBase::Loop();
  253. DetectTwist();
  254. DetectShake();
  255.  
  256. Vec3 mss = fusor.mss();
  257. if (SaberBase::IsOn()) {
  258. DetectSwing();
  259. if (auto_lockup_on_ &&
  260. !swinging_ &&
  261. fusor.swing_speed() > 120 &&
  262. millis() - clash_impact_millis_ > SA22C_LOCKUP_DELAY &&
  263. SaberBase::Lockup()) {
  264. SaberBase::DoEndLockup();
  265. SaberBase::SetLockup(SaberBase::LOCKUP_NONE);
  266. auto_lockup_on_ = false;
  267. }
  268. if (auto_melt_on_ &&
  269. !swinging_ &&
  270. fusor.swing_speed() > 60 &&
  271. millis() - clash_impact_millis_ > SA22C_LOCKUP_DELAY &&
  272. SaberBase::Lockup()) {
  273. SaberBase::DoEndLockup();
  274. SaberBase::SetLockup(SaberBase::LOCKUP_NONE);
  275. auto_melt_on_ = false;
  276. }
  277.  
  278. // EVENT_PUSH
  279. if (fabs(mss.x) < 3.0 &&
  280. mss.y * mss.y + mss.z * mss.z > 70 &&
  281. fusor.swing_speed() < 30 &&
  282. fabs(fusor.gyro().x) < 10) {
  283. if (millis() - push_begin_millis_ > SA22C_FORCE_PUSH_LENGTH) {
  284. Event(BUTTON_NONE, EVENT_PUSH);
  285. push_begin_millis_ = millis();
  286. }
  287. } else {
  288. push_begin_millis_ = millis();
  289. }
  290.  
  291. } else {
  292. // EVENT_SWING - Swing On gesture control to allow fine tuning of speed needed to ignite
  293. if (millis() - saber_off_time_ < MOTION_TIMEOUT) {
  294. SaberBase::RequestMotion();
  295. if (swinging_ && fusor.swing_speed() < 90) {
  296. swinging_ = false;
  297. }
  298. if (!swinging_ && fusor.swing_speed() > SA22C_SWING_ON_SPEED) {
  299. swinging_ = true;
  300. Event(BUTTON_NONE, EVENT_SWING);
  301. }
  302. }
  303. // EVENT_THRUST
  304. if (mss.y * mss.y + mss.z * mss.z < 16.0 &&
  305. mss.x > 14 &&
  306. fusor.swing_speed() < 150) {
  307. if (millis() - thrust_begin_millis_ > 15) {
  308. Event(BUTTON_NONE, EVENT_THRUST);
  309. thrust_begin_millis_ = millis();
  310. }
  311. } else {
  312. thrust_begin_millis_ = millis();
  313. }
  314. }
  315. }
  316.  
  317. // Fast On Gesture Ignition
  318. virtual void FastOn() {
  319. if (IsOn()) return;
  320. if (current_style() && current_style()->NoOnOff())
  321. return;
  322. activated_ = millis();
  323. STDOUT.println("Ignition.");
  324. MountSDCard();
  325. EnableAmplifier();
  326. SaberBase::RequestMotion();
  327. // Avoid clashes a little bit while turning on.
  328. // It might be a "clicky" power button...
  329. IgnoreClash(500);
  330. SaberBase::TurnOn();
  331. // Optional effects
  332. SaberBase::DoEffect(EFFECT_FAST_ON, 0);
  333. }
  334.  
  335. // Volume Menu
  336. void VolumeUp() {
  337. STDOUT.println("Volume up");
  338. if (dynamic_mixer.get_volume() < VOLUME) {
  339. dynamic_mixer.set_volume(std::min<int>(VOLUME + VOLUME * 0.1,
  340. dynamic_mixer.get_volume() + VOLUME * 0.10));
  341. if (SFX_volup) {
  342. hybrid_font.PlayCommon(&SFX_volup);
  343. } else {
  344. beeper.Beep(0.5, 2000);
  345. }
  346. STDOUT.print("Volume Up - Current Volume: ");
  347. STDOUT.println(dynamic_mixer.get_volume());
  348. } else {
  349. // Cycle through ends of Volume Menu option
  350. #ifdef VOLUME_MENU_CYCLE
  351. if (!max_vol_reached_) {
  352. if (SFX_volmax) {
  353. hybrid_font.PlayCommon(&SFX_volmax);
  354. } else {
  355. beeper.Beep(0.5, 3000);
  356. }
  357. STDOUT.print("Maximum Volume: ");
  358. max_vol_reached_ = true;
  359. } else {
  360. dynamic_mixer.set_volume(std::max<int>(VOLUME * 0.1,
  361. dynamic_mixer.get_volume() - VOLUME * 0.90));
  362. if (SFX_volmin) {
  363. hybrid_font.PlayCommon(&SFX_volmin);
  364. } else {
  365. beeper.Beep(0.5, 1000);
  366. }
  367. STDOUT.print("Minimum Volume: ");
  368. max_vol_reached_ = false;
  369. }
  370. #else
  371. if (SFX_volmax) {
  372. hybrid_font.PlayCommon(&SFX_volmax);
  373. } else {
  374. beeper.Beep(0.5, 3000);
  375. }
  376. STDOUT.print("Maximum Volume: ");
  377. #endif
  378. }
  379. }
  380.  
  381. void VolumeDown() {
  382. STDOUT.println("Volume Down");
  383. if (dynamic_mixer.get_volume() > (0.10 * VOLUME)) {
  384. dynamic_mixer.set_volume(std::max<int>(VOLUME * 0.1,
  385. dynamic_mixer.get_volume() - VOLUME * 0.10));
  386. if (SFX_voldown) {
  387. hybrid_font.PlayCommon(&SFX_voldown);
  388. } else {
  389. beeper.Beep(0.5, 2000);
  390. }
  391. STDOUT.print("Volume Down - Current Volume: ");
  392. STDOUT.println(dynamic_mixer.get_volume());
  393. } else {
  394. #ifdef VOLUME_MENU_CYCLE
  395. if (!min_vol_reached_) {
  396. if (SFX_volmin) {
  397. hybrid_font.PlayCommon(&SFX_volmin);
  398. } else {
  399. beeper.Beep(0.5, 1000);
  400. }
  401. STDOUT.print("Minimum Volume: ");
  402. min_vol_reached_ = true;
  403. } else {
  404. dynamic_mixer.set_volume(VOLUME);
  405. if (SFX_volmax) {
  406. hybrid_font.PlayCommon(&SFX_volmax);
  407. } else {
  408. beeper.Beep(0.5, 3000);
  409. }
  410. STDOUT.print("Maximum Volume: ");
  411. min_vol_reached_ = false;
  412. }
  413. #else
  414. if (SFX_volmin) {
  415. hybrid_font.PlayCommon(&SFX_volmin);
  416. } else {
  417. beeper.Beep(0.5, 1000);
  418. }
  419. STDOUT.print("Minimum Volume: ");
  420. #endif
  421.  
  422. }
  423. }
  424.  
  425. bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) override {
  426. switch (EVENTID(button, event, modifiers)) {
  427. case EVENTID(BUTTON_POWER, EVENT_PRESSED, MODE_ON):
  428. case EVENTID(BUTTON_AUX, EVENT_PRESSED, MODE_ON):
  429. case EVENTID(BUTTON_AUX2, EVENT_PRESSED, MODE_ON):
  430. if (accel_.x < -0.15) {
  431. pointing_down_ = true;
  432. } else {
  433. pointing_down_ = false;
  434. }
  435. return true;
  436.  
  437. // Battle Mode
  438. #if NUM_BUTTONS == 3
  439. case EVENTID(BUTTON_AUX2, EVENT_SECOND_HELD, MODE_ON):
  440. #else
  441. case EVENTID(BUTTON_POWER, EVENT_THIRD_HELD, MODE_ON):
  442. #endif
  443. if (!battle_mode_) {
  444. STDOUT.println("Entering Battle Mode");
  445. battle_mode_ = true;
  446. if (SFX_bmbegin) {
  447. hybrid_font.PlayCommon(&SFX_bmbegin);
  448. } else {
  449. hybrid_font.DoEffect(EFFECT_FORCE, 0);
  450. }
  451. } else {
  452. STDOUT.println("Exiting Battle Mode");
  453. battle_mode_ = false;
  454. if (SFX_bmend) {
  455. hybrid_font.PlayCommon(&SFX_bmend);
  456. } else {
  457. beeper.Beep(0.5, 3000);
  458. }
  459. }
  460. return true;
  461.  
  462. // Auto Lockup Mode
  463. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_ON):
  464. // pointing up
  465. if (fusor.angle1() > M_PI / 3) {
  466. if (!lb_on_) {
  467. SaberBase::SetLockup(SaberBase::LOCKUP_LIGHTNING_BLOCK);
  468. lb_on_ = true;
  469. SaberBase::DoBeginLockup();
  470. return true;
  471. } else {
  472. lb_on_ = false;
  473. SaberBase::DoEndLockup();
  474. SaberBase::SetLockup(SaberBase::LOCKUP_NONE);
  475. return true;
  476. }
  477.  
  478. } else {
  479. if (!battle_mode_) return false;
  480. clash_impact_millis_ = millis();
  481. swing_blast_ = false;
  482. if (swinging_) return false;
  483. SaberBase::SetLockup(SaberBase::LOCKUP_NORMAL);
  484. auto_lockup_on_ = true;
  485. SaberBase::DoBeginLockup();
  486. return true;
  487. }
  488.  
  489.  
  490. case EVENTID(BUTTON_NONE, EVENT_STAB, MODE_ON):
  491. if (!battle_mode_) return false;
  492. clash_impact_millis_ = millis();
  493. swing_blast_ = false;
  494. if (!swinging_) {
  495. if (fusor.angle1() < - M_PI / 4) {
  496. SaberBase::SetLockup(SaberBase::LOCKUP_DRAG);
  497. } else {
  498. SaberBase::SetLockup(SaberBase::LOCKUP_MELT);
  499. }
  500. auto_melt_on_ = true;
  501. SaberBase::DoBeginLockup();
  502. }
  503. return true;
  504.  
  505. // Gesture Controls
  506. #ifdef SA22C_SWING_ON
  507. case EVENTID(BUTTON_NONE, EVENT_SWING, MODE_OFF):
  508. // Due to motion chip startup on boot creating false ignition we delay Swing On at boot for 3000ms
  509. if (millis() > 3000) {
  510. FastOn();
  511.  
  512.  
  513.  
  514.  
  515. #ifdef GESTURE_AUTO_BATTLE_MODE
  516. STDOUT.println("Entering Battle Mode");
  517. battle_mode_ = true;
  518. #endif
  519. }
  520. return true;
  521. #endif
  522.  
  523.  
  524.  
  525.  
  526.  
  527. #ifdef SA22C_TWIST_ON
  528. case EVENTID(BUTTON_NONE, EVENT_TWIST, MODE_OFF):
  529. // Delay twist events to prevent false trigger from over twistig
  530. if (millis() - last_twist_ > 500 &&
  531. millis() - saber_off_time_ > 500) {
  532.  
  533.  
  534. // pointing down
  535. if (fusor.angle1() < - M_PI / 4) {
  536. next_preset();
  537. return true;
  538. }
  539. // pointing up
  540. if (fusor.angle1() > M_PI / 3) {
  541. previous_preset();
  542. }
  543. return true;
  544.  
  545.  
  546. #ifndef SA22C_THRUST_ON
  547. case EVENTID(BUTTON_NONE, EVENT_THRUST, MODE_ON):
  548. if (fusor.angle1() > M_PI / 4) {
  549. ToggleColorChangeMode();
  550. }
  551. break;
  552. #endif
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561. // FastOn();
  562. #ifdef GESTURE_AUTO_BATTLE_MODE
  563. STDOUT.println("Entering Battle Mode");
  564. battle_mode_ = true;
  565. #endif
  566. last_twist_ = millis();
  567. }
  568. return true;
  569. #endif
  570.  
  571.  
  572. #ifdef SA22C_TWIST_OFF
  573. //case EVENTID(BUTTON_NONE, EVENT_TWIST, MODE_ON):
  574. // Delay twist events to prevent false trigger from over twisting
  575. // if (millis() - last_twist_ > 3000) {
  576. // Off();
  577. // last_twist_ = millis();
  578. // saber_off_time_ = millis();
  579. // battle_mode_ = false;
  580. // }
  581. // return true;
  582.  
  583.  
  584. case EVENTID(BUTTON_NONE, EVENT_TWIST, MODE_ON):
  585. // pointing down
  586. if (fusor.angle1() < - M_PI / 4) {
  587. Off();
  588. //StartOrStopTrack();
  589. return true;
  590. }
  591. // pointing up
  592. if (fusor.angle1() > M_PI / 3) {
  593. SaberBase::DoBlast();
  594. }
  595. return true;
  596.  
  597.  
  598.  
  599. #endif
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606. #ifdef SA22C_STAB_ON
  607. case EVENTID(BUTTON_NONE, EVENT_STAB, MODE_OFF):
  608. if (millis() - saber_off_time_ > 1000) {
  609. talkie.Say(spBATTERYLEVEL);
  610. talkie.SayNumber((int)floorf(battery_monitor.battery_percent()));
  611. talkie.Say(spPERCENT);
  612. STDOUT.println(battery_monitor.battery_percent());
  613.  
  614.  
  615.  
  616. //if (millis() - saber_off_time_ > 1000) {
  617. //talkie.SayDigit((int)floorf(battery_monitor.battery()));
  618. // talkie.Say(spPOINT);
  619. // talkie.SayDigit(((int)floorf(battery_monitor.battery() * 10)) % 10);
  620. // talkie.SayDigit(((int)floorf(battery_monitor.battery() * 100)) % 10);
  621. // talkie.Say(spVOLTS);
  622.  
  623. }
  624.  
  625. return true;
  626.  
  627. #endif
  628.  
  629.  
  630.  
  631.  
  632.  
  633. //FastOn();
  634. #ifdef GESTURE_AUTO_BATTLE_MODE
  635. STDOUT.println("Entering Battle Mode");
  636. battle_mode_ = true;
  637.  
  638. return true;
  639. #endif
  640.  
  641.  
  642.  
  643.  
  644. #ifdef SA22C_THRUST_ON
  645. case EVENTID(BUTTON_NONE, EVENT_THRUST, MODE_OFF):
  646.  
  647. // pointing down
  648. if (fusor.angle1() < - M_PI / 4) {
  649. FastOn();
  650.  
  651. return true;
  652. }
  653. // pointing up
  654. if (fusor.angle1() > M_PI / 3) {
  655. On();
  656.  
  657. }
  658. return true;
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667. // if (millis() - saber_off_time_ > 1000) {
  668. //StartOrStopTrack();
  669. // FastOn();
  670.  
  671.  
  672. //#ifdef GESTURE_AUTO_BATTLE_MODE
  673. // STDOUT.println("Entering Battle Mode");
  674. // battle_mode_ = true;
  675. //#endif
  676.  
  677. #endif
  678.  
  679. #ifdef SA22C_FORCE_PUSH
  680. case EVENTID(BUTTON_NONE, EVENT_PUSH, MODE_ON):
  681.  
  682.  
  683. if (FORCE_PUSH_CONDITION &&
  684. millis() - last_push_ > 2000) {
  685. SaberBase::DoForce();
  686.  
  687.  
  688.  
  689.  
  690. // if (SFX_push) {
  691. // hybrid_font.PlayCommon(&SFX_push);
  692. // } else {
  693. // hybrid_font.DoEffect(EFFECT_FORCE, 0);
  694. // }
  695. //last_push_ = millis();
  696.  
  697. }
  698. return true;
  699.  
  700. #endif
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719. // Saber ON AND Volume Up
  720. //0 button
  721. case EVENTID(BUTTON_NONE, EVENT_SHAKE, MODE_OFF):
  722. // pointing down
  723. if (fusor.angle1() < - M_PI / 4) {
  724. VolumeDown();
  725. return true;
  726. }
  727. // pointing up
  728. if (fusor.angle1() > M_PI / 3) {
  729. VolumeUp();
  730. }
  731. return true;
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738. case EVENTID(BUTTON_POWER, EVENT_FIRST_SAVED_CLICK_SHORT, MODE_OFF):
  739. if (!mode_volume_) {
  740.  
  741. On();
  742. } else {
  743. VolumeUp();
  744. }
  745. return true;
  746.  
  747. case EVENTID(BUTTON_POWER, EVENT_FIRST_CLICK_LONG, MODE_OFF):
  748. // 1-button: Next Preset AND Volume Down
  749. #if NUM_BUTTONS == 1
  750. if (!mode_volume_) {
  751. next_preset();
  752. } else {
  753. VolumeDown();
  754. }
  755. return true;
  756. #else
  757. // 2 and 3 button: Start or Stop Track
  758. StartOrStopTrack();
  759. return true;
  760. #endif
  761.  
  762. // Color Change mode
  763. //#ifndef DISABLE_COLOR_CHANGE
  764. // case EVENTID(BUTTON_NONE, EVENT_SHAKE, MODE_ON):
  765. // if (fusor.angle1() > M_PI / 4) {
  766. // ToggleColorChangeMode();
  767. // }
  768. // break;
  769. //#endif
  770.  
  771.  
  772.  
  773.  
  774. // 2 and 3 button: Next Preset and Volume Down
  775. case EVENTID(BUTTON_AUX, EVENT_FIRST_CLICK_SHORT, MODE_OFF):
  776. if (!mode_volume_) {
  777. next_preset();
  778. } else {
  779. VolumeDown();
  780. }
  781. return true;
  782.  
  783. #if NUM_BUTTONS == 3
  784. // 3 button: Previous Preset
  785. case EVENTID(BUTTON_AUX2, EVENT_FIRST_CLICK_SHORT, MODE_OFF):
  786. #else
  787. // 1 and 2 button: Previous Preset
  788. case EVENTID(BUTTON_POWER, EVENT_FIRST_HELD_LONG, MODE_OFF):
  789. #endif
  790. if (!mode_volume_) {
  791. previous_preset();
  792. }
  793. return true;
  794.  
  795. // Activate Muted
  796. case EVENTID(BUTTON_POWER, EVENT_SECOND_HELD, MODE_OFF):
  797. if (SetMute(true)) {
  798. unmute_on_deactivation_ = true;
  799. On();
  800. }
  801. return true;
  802.  
  803. // Turn Blade OFF
  804. #if NUM_BUTTONS > 1
  805. // 2 and 3 button
  806. case EVENTID(BUTTON_POWER, EVENT_FIRST_HELD_MEDIUM, MODE_ON):
  807. #else
  808. // 1 button
  809. case EVENTID(BUTTON_POWER, EVENT_FIRST_HELD_LONG, MODE_ON):
  810. #endif
  811. if (!SaberBase::Lockup()) {
  812. #ifndef DISABLE_COLOR_CHANGE
  813. if (SaberBase::GetColorChangeMode() != SaberBase::COLOR_CHANGE_MODE_NONE) {
  814. // Just exit color change mode.
  815. // Don't turn saber off.
  816. ToggleColorChangeMode();
  817. return true;
  818. }
  819. #endif
  820. if (!battle_mode_) {
  821. Off();
  822. }
  823. }
  824. saber_off_time_ = millis();
  825. battle_mode_ = false;
  826. swing_blast_ = false;
  827. return true;
  828.  
  829.  
  830. // 0 button Force and Color Change mode
  831. case EVENTID(BUTTON_NONE, EVENT_SHAKE, MODE_ON):
  832. if (fusor.angle1() < - M_PI / 4) {
  833. ToggleColorChangeMode();
  834. return true;
  835. }
  836. if (fusor.angle1() > M_PI / 3) {
  837. SaberBase::DoForce();
  838.  
  839. }
  840. return true;
  841.  
  842.  
  843.  
  844.  
  845.  
  846. //1 button Force and Color Change mode
  847. #if NUM_BUTTONS == 0
  848. case EVENTID(BUTTON_NONE, EVENT_TWIST, MODE_ON | BUTTON_POWER):
  849.  
  850. if (accel_.x < -0.15) {
  851. ToggleColorChangeMode();
  852. } else{
  853. SaberBase::DoForce();
  854. }
  855.  
  856. #endif
  857. return true;
  858.  
  859. #if NUM_BUTTONS == 2
  860. //2 and 3 button Force effect
  861. case EVENTID(BUTTON_POWER, EVENT_SECOND_SAVED_CLICK_SHORT, MODE_ON):
  862. SaberBase::DoForce();
  863. return true;
  864. //2 and 3 button color change
  865. #ifndef DISABLE_COLOR_CHANGE
  866. case EVENTID(BUTTON_AUX, EVENT_FIRST_CLICK_SHORT, MODE_ON | BUTTON_POWER):
  867. ToggleColorChangeMode();
  868. return true;
  869. #endif
  870. #endif
  871.  
  872. // Blaster Deflection
  873. #if NUM_BUTTONS == 1
  874. // 1 button
  875. case EVENTID(BUTTON_POWER, EVENT_FIRST_SAVED_CLICK_SHORT, MODE_ON):
  876. case EVENTID(BUTTON_POWER, EVENT_SECOND_SAVED_CLICK_SHORT, MODE_ON):
  877. case EVENTID(BUTTON_POWER, EVENT_THIRD_CLICK_SHORT, MODE_ON):
  878. #else
  879. // 2 and 3 button
  880. case EVENTID(BUTTON_AUX, EVENT_FIRST_SAVED_CLICK_SHORT, MODE_ON):
  881. case EVENTID(BUTTON_AUX, EVENT_SECOND_SAVED_CLICK_SHORT, MODE_ON):
  882. case EVENTID(BUTTON_AUX, EVENT_THIRD_CLICK_SHORT, MODE_ON):
  883. #endif
  884. swing_blast_ = false;
  885. SaberBase::DoBlast();
  886. return true;
  887.  
  888. // Multi-Blaster Deflection mode
  889. #if NUM_BUTTONS == 1
  890. // 1 button
  891. case EVENTID(BUTTON_NONE, EVENT_SWING, MODE_ON | BUTTON_POWER):
  892. #else
  893. // 2 and 3 button
  894. #ifndef SA22C_NO_LOCKUP_HOLD
  895. case EVENTID(BUTTON_AUX, EVENT_SECOND_HELD, MODE_ON):
  896. #else
  897. // in SA22C_NO_LOCKUP_HOLD mode, multi-blaster is triggered by holding aux
  898. // while swinging
  899. case EVENTID(BUTTON_NONE, EVENT_SWING, MODE_ON | BUTTON_AUX):
  900. #endif
  901. #endif
  902. swing_blast_ = !swing_blast_;
  903. if (swing_blast_) {
  904. if (SFX_blstbgn) {
  905. hybrid_font.PlayCommon(&SFX_blstbgn);
  906. } else {
  907. hybrid_font.SB_Effect(EFFECT_BLAST, 0);
  908. }
  909. } else {
  910. if (SFX_blstend) {
  911. hybrid_font.PlayCommon(&SFX_blstend);
  912. } else {
  913. hybrid_font.SB_Effect(EFFECT_BLAST, 0);
  914. }
  915. }
  916. return true;
  917.  
  918. case EVENTID(BUTTON_NONE, EVENT_SWING, MODE_ON):
  919. if (swing_blast_) {
  920. SaberBase::DoBlast();
  921. }
  922. return true;
  923.  
  924. // Lockup
  925. #if NUM_BUTTONS == 1
  926. // 1 button lockup
  927. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_ON | BUTTON_POWER):
  928. #else
  929. #ifndef SA22C_NO_LOCKUP_HOLD
  930. // 2 and 3 button lockup
  931. case EVENTID(BUTTON_AUX, EVENT_FIRST_HELD, MODE_ON):
  932. #else
  933. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_ON | BUTTON_AUX):
  934. #endif
  935. #endif
  936. if (!SaberBase::Lockup) {
  937. if (pointing_down_) {
  938. SaberBase::SetLockup(SaberBase::LOCKUP_DRAG);
  939. } else {
  940. SaberBase::SetLockup(SaberBase::LOCKUP_NORMAL);
  941. }
  942. swing_blast_ = false;
  943. SaberBase::DoBeginLockup();
  944. return true;
  945. }
  946. break;
  947.  
  948. // Lightning Block
  949. #if NUM_BUTTONS < 3
  950. // 1 and 2 button
  951. case EVENTID(BUTTON_POWER, EVENT_SECOND_HELD, MODE_ON):
  952. #else
  953. // 3 button
  954. case EVENTID(BUTTON_AUX2, EVENT_FIRST_HELD, MODE_ON):
  955. #endif
  956.  
  957.  
  958.  
  959. if (!battle_mode_) {
  960. SaberBase::SetLockup(SaberBase::LOCKUP_LIGHTNING_BLOCK);
  961. swing_blast_ = false;
  962. SaberBase::DoBeginLockup();
  963. return true;
  964. }
  965. break;
  966.  
  967. // Melt
  968. case EVENTID(BUTTON_NONE, EVENT_STAB, MODE_ON | BUTTON_POWER):
  969. if (!SaberBase::Lockup() && !battle_mode_) {
  970. SaberBase::SetLockup(SaberBase::LOCKUP_MELT);
  971. swing_blast_ = false;
  972. SaberBase::DoBeginLockup();
  973. return true;
  974. }
  975. break;
  976.  
  977. // Start or Stop Track
  978. #if NUM_BUTTONS == 1
  979. // 0 button
  980. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_OFF):
  981.  
  982. if (fusor.angle1() > M_PI / 3) {
  983.  
  984. StartOrStopTrack();
  985. }
  986. return true;
  987. #else
  988.  
  989.  
  990. // 1 button
  991. case EVENTID(BUTTON_POWER, EVENT_SECOND_SAVED_CLICK_SHORT, MODE_OFF):
  992. StartOrStopTrack();
  993. return true;
  994. #endif
  995.  
  996.  
  997. #if NUM_BUTTONS == 2
  998. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_OFF):
  999.  
  1000. if (fusor.angle1() > M_PI / 3) {
  1001.  
  1002. StartOrStopTrack();
  1003. }
  1004. return true;
  1005. #endif
  1006.  
  1007.  
  1008.  
  1009. case EVENTID(BUTTON_POWER, EVENT_PRESSED, MODE_OFF):
  1010. case EVENTID(BUTTON_AUX, EVENT_PRESSED, MODE_OFF):
  1011. case EVENTID(BUTTON_AUX2, EVENT_PRESSED, MODE_OFF):
  1012. SaberBase::RequestMotion();
  1013. return true;
  1014.  
  1015. // Enter Volume MENU
  1016. #if NUM_BUTTONS == 1
  1017. // 1 button
  1018. case EVENTID(BUTTON_NONE, EVENT_CLASH, MODE_OFF | BUTTON_POWER):
  1019. #else
  1020. // 2 and 3 button
  1021. case EVENTID(BUTTON_AUX, EVENT_FIRST_CLICK_LONG, MODE_OFF):
  1022. #endif
  1023. if (!mode_volume_) {
  1024. mode_volume_ = true;
  1025. if (SFX_vmbegin) {
  1026. hybrid_font.PlayCommon(&SFX_vmbegin);
  1027. } else {
  1028. beeper.Beep(0.5, 3000);
  1029. }
  1030. STDOUT.println("Enter Volume Menu");
  1031. } else {
  1032. mode_volume_ = false;
  1033. if (SFX_vmend) {
  1034. hybrid_font.PlayCommon(&SFX_vmend);
  1035. } else {
  1036. beeper.Beep(0.5, 3000);
  1037. }
  1038. STDOUT.println("Exit Volume Menu");
  1039. }
  1040. return true;
  1041.  
  1042. // Battery level
  1043. #if NUM_BUTTONS == 1
  1044. // 1 button
  1045. case EVENTID(BUTTON_POWER, EVENT_THIRD_SAVED_CLICK_SHORT, MODE_OFF):
  1046. #else
  1047. // 2 and 3 button
  1048. case EVENTID(BUTTON_AUX, EVENT_FIRST_HELD_LONG, MODE_OFF):
  1049. #endif
  1050.  
  1051. //if (millis() - saber_off_time_ > 1000) {
  1052. talkie.Say(spBATTERYLEVEL);
  1053. talkie.SayNumber((int)floorf(battery_monitor.battery_percent()));
  1054. talkie.Say(spPERCENT);
  1055. STDOUT.println(battery_monitor.battery_percent());
  1056.  
  1057.  
  1058.  
  1059. //talkie.SayDigit((int)floorf(battery_monitor.battery()));
  1060. // talkie.Say(spPOINT);
  1061. // talkie.SayDigit(((int)floorf(battery_monitor.battery() * 10)) % 10);
  1062. // talkie.SayDigit(((int)floorf(battery_monitor.battery() * 100)) % 10);
  1063. // talkie.Say(spVOLTS);
  1064. return true;
  1065.  
  1066. #ifdef BLADE_DETECT_PIN
  1067. case EVENTID(BUTTON_BLADE_DETECT, EVENT_LATCH_ON, MODE_ANY_BUTTON | MODE_ON):
  1068. case EVENTID(BUTTON_BLADE_DETECT, EVENT_LATCH_ON, MODE_ANY_BUTTON | MODE_OFF):
  1069. // Might need to do something cleaner, but let's try this for now.
  1070. blade_detected_ = true;
  1071. FindBladeAgain();
  1072. SaberBase::DoBladeDetect(true);
  1073. return true;
  1074.  
  1075. case EVENTID(BUTTON_BLADE_DETECT, EVENT_LATCH_OFF, MODE_ANY_BUTTON | MODE_ON):
  1076. case EVENTID(BUTTON_BLADE_DETECT, EVENT_LATCH_OFF, MODE_ANY_BUTTON | MODE_OFF):
  1077. // Might need to do something cleaner, but let's try this for now.
  1078. blade_detected_ = false;
  1079. FindBladeAgain();
  1080. SaberBase::DoBladeDetect(false);
  1081. return true;
  1082. #endif
  1083.  
  1084. // Events that needs to be handled regardless of what other buttons
  1085. // are pressed.
  1086. case EVENTID(BUTTON_POWER, EVENT_RELEASED, MODE_ANY_BUTTON | MODE_ON):
  1087. case EVENTID(BUTTON_AUX, EVENT_RELEASED, MODE_ANY_BUTTON | MODE_ON):
  1088. case EVENTID(BUTTON_AUX2, EVENT_RELEASED, MODE_ANY_BUTTON | MODE_ON):
  1089. if (SaberBase::Lockup()) {
  1090. SaberBase::DoEndLockup();
  1091. SaberBase::SetLockup(SaberBase::LOCKUP_NONE);
  1092. return true;
  1093. }
  1094. }
  1095. return false;
  1096. }
  1097.  
  1098. void SB_Effect(EffectType effect, float location) override {
  1099. switch (effect) {
  1100. case EFFECT_POWERSAVE:
  1101. if (SFX_dim) {
  1102. hybrid_font.PlayCommon(&SFX_dim);
  1103. } else {
  1104. beeper.Beep(0.5, 3000);
  1105. }
  1106. return;
  1107. case EFFECT_BATTERY_LEVEL:
  1108. if (SFX_battery) {
  1109. hybrid_font.PlayCommon(&SFX_battery);
  1110. } else {
  1111. beeper.Beep(0.5, 3000);
  1112. }
  1113. return;
  1114. case EFFECT_FAST_ON:
  1115. if (SFX_faston) {
  1116. hybrid_font.PlayCommon(&SFX_faston);
  1117. }
  1118. return;
  1119.  
  1120. default: break; // avoids compiler warning
  1121. }
  1122. }
  1123.  
  1124. private:
  1125. bool pointing_down_ = false;
  1126. bool swing_blast_ = false;
  1127. bool mode_volume_ = false;
  1128. bool auto_lockup_on_ = false;
  1129. bool auto_melt_on_ = false;
  1130. bool battle_mode_ = false;
  1131. bool max_vol_reached_ = false;
  1132. bool min_vol_reached_ = false;
  1133. uint32_t thrust_begin_millis_ = millis();
  1134. uint32_t push_begin_millis_ = millis();
  1135. uint32_t clash_impact_millis_ = millis();
  1136. uint32_t last_twist_ = millis();
  1137. uint32_t last_push_ = millis();
  1138. uint32_t saber_off_time_ = millis();
  1139. bool lb_on_ = false;
  1140. };
  1141.  
  1142. #endif
Advertisement
Add Comment
Please, Sign In to add comment