git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/gb/mbc5_rumble/src/mbc5_rumble.c

      1 #include <gbdk/platform.h>
      2 #include <stdint.h>
      3 #include <stdbool.h>
      4 #include "mbc5_rumble.h"
      5 
      6 // Power up defaults
      7 uint8_t rumble_count = 0u;
      8 uint8_t rumble_intensity = RUMBLE_INTENSITY_MAX;
      9 uint8_t rumble_mask      = RUMBLE_DUTY_MASK_MAX;
     10 
     11 // Lookup table for bitwise duty cycle masks based on intensity setting
     12 const uint8_t rumble_masks[RUMBLE_INTENSITY_MAX + 1] = {
     13     RUMBLE_DUTY_MASK_LOW,
     14     RUMBLE_DUTY_MASK_MED,
     15     RUMBLE_DUTY_MASK_HI,
     16     RUMBLE_DUTY_MASK_MAX,
     17 };
     18 
     19 
     20 void rumble_start(uint8_t duration) {
     21     rumble_count = duration;
     22 }
     23 
     24 
     25 void rumble_cancel(void) {
     26     rumble_count = RUMBLE_COUNT_DONE;
     27     MBC5_RUMBLE_OFF;
     28 }
     29 
     30 
     31 void rumble_set_duty_cycle(void) {
     32     rumble_mask = rumble_masks[rumble_intensity];
     33 }
     34 
     35 
     36 void rumble_intensity_set(uint8_t intensity) {    
     37 
     38     if (intensity > RUMBLE_INTENSITY_MAX)
     39         intensity = RUMBLE_INTENSITY_MAX;
     40     rumble_intensity = intensity;
     41     rumble_set_duty_cycle();
     42 }
     43 
     44 
     45 uint8_t rumble_intensity_increase(void) {
     46 
     47     if (rumble_intensity <  RUMBLE_INTENSITY_MAX)
     48         rumble_intensity++;
     49     rumble_set_duty_cycle();
     50 
     51     return rumble_intensity;
     52 }
     53 
     54 
     55 uint8_t rumble_intensity_decrease(void) {
     56 
     57     if (rumble_intensity > RUMBLE_INTENSITY_MIN)
     58         rumble_intensity--;
     59     rumble_set_duty_cycle();
     60 
     61     return rumble_intensity;
     62 }
     63 
     64 
     65 // Update cart rumble, should be called once per frame such as in a VBlank ISR
     66 void rumble_update(void) {
     67 
     68     if (rumble_count) {
     69         rumble_count--;
     70 
     71         // For max intensity do a 100% duty cycle (always on).
     72         // For lower intensities use bit masked partial duty cycles.
     73         //
     74         // When the counter reaches the end (zero) it will get turned
     75         // off due to no bits matching any of the masks
     76         if (rumble_count & rumble_mask)
     77             MBC5_RUMBLE_ON;
     78         else
     79             MBC5_RUMBLE_OFF;
     80     }
     81 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.