<commit_before>// Copyright (C) 2013-2017 <NAME>

#include "c8051F120.h"                  // Device-specific SFR Definitions
#include "c8051F120_io.h"               // Device-specific SFR Definitions

#include "bu_init.h"
#include "bu_watchdog.h"
#include "timer3int.h"

#include <stdint.h>

#define SAMPLE_RATE     50000           // Interrupt frequency in Hz
#define LOOP_RATE         100           // Loop semaphore frequency in Hz

void main(void)
{
    uint8_t mode    = 0;
    uint16_t  time_SW2_pressed  = 0;
    uint16_t  time_SW2_released = 0;

    // Set up watchdog timer (10.4ms at sysclk of 100MHz)
    WatchDog_set_10ms();

    PORT_Init ();
    SYSCLK_Init();

    Timer3_Init(SYSCLK, SAMPLE_RATE, LOOP_RATE);
    Timer3_setMode(2);

    EA = 1;                     // enable global interrupts

    while (1)
    {
        // wait for Timer3 interrupt, when it ends
        while( !Timer3_GetSemaphore() ) {
            // WatchDog_reset();    // optionally reset watchdog timer while waiting
                                    // for the right interrupt to be completed
            
            // sleep until an interrupt happens and then check [again]
            // if it is the one we are waiting for to be completed
            CPU_sleep();
        }
        // and immediately afterwards reset the interrupt done marker for the next time
        Timer3_ResetSemaphore();

        // Reset watchdog timer
        WatchDog_reset();

        // handle timing of switches
        if (!SW2)
        {
            if (time_SW2_released>10)             // check if pressed after released for at least 100ms
            {
                time_SW2_released = 0;
            }

            if (time_SW2_pressed<0xffff) time_SW2_pressed++;  // increment only if still within range
        }
        else
        {
            if (time_SW2_pressed>10)                // check if released after pressed for at least 100ms
            {
                if (time_SW2_pressed>60)            // int32_t press, int32_ter than 600ms
                {
                    if (mode==(MAX_MODES-1)) mode=0;
                    else mode=(MAX_MODES-1);        // we arranged for the very last mode to be off mode
                    Timer3_setMode(mode);
                }
                else                                // short press (but int32_ter than 100ms)
                {
                    // advance to the next mode but skip the "off mode"
                    mode++; if (mode>=(MAX_MODES-1)) mode=0;
                    Timer3_setMode(mode);
                }
                time_SW2_pressed = 0;               // reset the pressed value so that we handle pressed only once
            }

            if (time_SW2_released<0xffff) time_SW2_released++;  // increment only if still within range
        }

    }
}<commit_msg>Corrected comments that were damaged by the massive migration to *int*_t data types at the beginning of the sepecter. Improved comments that explain timing in the main program super loop.<commit_after>// Copyright (C) 2013-2017 <NAME>

#include "c8051F120.h"                  // Device-specific SFR Definitions
#include "c8051F120_io.h"               // Device-specific SFR Definitions

#include "bu_init.h"
#include "bu_watchdog.h"
#include "timer3int.h"

#include <stdint.h>

#define SAMPLE_RATE     50000           // Interrupt frequency in Hz
#define LOOP_RATE         100           // Loop semaphore frequency in Hz

void main(void)
{
    uint8_t mode    = 0;
    uint16_t  time_SW2_pressed  = 0;
    uint16_t  time_SW2_released = 0;

    // Set up watchdog timer (10.4ms at sysclk of 100MHz)
    WatchDog_set_10ms();

    PORT_Init ();
    SYSCLK_Init();

    Timer3_Init(SYSCLK, SAMPLE_RATE, LOOP_RATE);
    Timer3_setMode(2);

    EA = 1;                     // enable global interrupts

    while (1)
    {
        // wait for Timer3 interrupt, at the rate of LOOP_RATE
        // it will release the semaphore so that the loop does one iteration
        while( !Timer3_GetSemaphore() ) {
            WatchDog_reset();   // not every interupt releases the semaphore
                                // reset the watchdog timer while still waiting
            
            // sleep until the next interrupt happens and then check [again]
            // if it is the one we are waiting for to be completed
            CPU_sleep();
        }
        // and immediately afterwards reset the interrupt done marker for the next time
        Timer3_ResetSemaphore();

        // Reset the watchdog timer
        WatchDog_reset();

        
        // code below is executed at the frequency of LOOP_RATE 
        // all timing is expressed by loop counts times (1000ms/LOOP_RATE)
        // in this example the loop rate is 100 and hence the time period is 10ms
        // **********************************************************************

        // handle timing of switches
        if (!SW2)
        {
            if (time_SW2_released>10)               // check if pressed after released for at least 100ms
            {
                time_SW2_released = 0;
            }

            if (time_SW2_pressed<0xffff)
            {
                time_SW2_pressed++;                 // increment only if still within the range to prevent roll over
            }
        }
        else
        {
            if (time_SW2_pressed>10)                // check if was pressed for at least 10 loops - 100ms (debouncing)
            {
                if (time_SW2_pressed>60)            // check if was pressed for more than 60 loops - 600ms
                {
                    // code specific to the particular application
                    if (mode==(MAX_MODES-1)) mode=0;
                    else mode=(MAX_MODES-1);        // we arranged for the very last mode to be the off mode
                    Timer3_setMode(mode);
                }
                else                                // was pressed for not more than 60 loops (but more than debouncing)
                {
                    // code specific to the particular application
                    // advance to the next mode but skip the "off mode"
                    mode++; if (mode>=(MAX_MODES-1)) mode=0;
                    Timer3_setMode(mode);
                }
                time_SW2_pressed = 0;               // reset the pressed value so that we handle pressed only once
            }

            if (time_SW2_released<0xffff) {
                time_SW2_released++;                // increment only if still within the range to prevent roll over
            }
        }

    }
}
{'fraction_non_alphanumeric': 0.08133030990173848, 'fraction_numerical': 0.031746031746031744, 'mean_word_length': 1.5003779289493575, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7219356', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1706, 'n_words': 598}