<commit_before>#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "ui.h"
#include "out.h"
#include "store.h"

// The period in clock cycles from frequency in mHz
// is p = F_CPU / (f / 1000) i.e. p = (1000 * F_CPU) / f
// but 1000 * F_CPU may overflow a 32-bit integer
// so find the smallest power of 2 k such that 
// (1000/k) * F_CPU does not overflow
// i.e. (1000/k) * F_CPU < 0xFFFFFFFF
// or (1000/k) < 0xFFFFFFFF/F_CPU
// Then p = (1000/k * F_CPU) / (f/k)
#if 1000 < 0xFFFFFFFF/F_CPU
  #define F_CPU_MUL 1000
  #define F_OUT_DIV 1
#elif 500 < 0xFFFFFFFF/F_CPU 
  #define F_CPU_MUL 500
  #define F_OUT_DIV 2
#elif 250 < 0xFFFFFFFF/F_CPU
  #define F_CPU_MUL 250
  #define F_OUT_DIV 4
#elif 125 < 0xFFFFFFFF/F_CPU 
  #define F_CPU_MUL 125
  #define F_OUT_DIV 8
#else
  #error Cannot compute frequency scaling parameters
#endif

#define PWM_RATIO 64
#define MAX_NON_SQUARE_FREQUENCY_mHz ((uint32_t)(F_CPU / 2.0 * 1000.0 / PWM_RATIO + 0.5))

#define MIN_NON_SQUARE_PERIOD_NS ((uint32_t)(1e12 / MAX_NON_SQUARE_FREQUENCY_mHz + 0.5))

#define SIN__1_OVER_32_PI  0.09802 /* sin(pi *  1/32) */
#define SIN__3_OVER_32_PI  0.29028 /* sin(pi *  3/32) */
#define SIN__5_OVER_32_PI  0.47140 /* sin(pi *  5/32) */
#define SIN__7_OVER_32_PI  0.63439 /* sin(pi *  7/32) */
#define SIN__9_OVER_32_PI  0.77301 /* sin(pi *  9/32) */
#define SIN_11_OVER_32_PI  0.88192 /* sin(pi * 11/32) */
#define SIN_13_OVER_32_PI  0.95694 /* sin(pi * 13/32) */
#define SIN_15_OVER_32_PI  0.99518 /* sin(pi * 15/32) */

static uint8_t on;
static uint8_t freq_mode = OUT_SQUARE;
static uint8_t waveform;
static uint8_t duty_cycle = 50;
static uint8_t amplitude = 100;
static int8_t fine_cal;
static int8_t medium_cal;

static uint32_t period_ns;
static uint32_t freq_mHz = 1000000;

static uint8_t waveform_index;
static uint16_t waveform_data[PWM_RATIO];

static void range_limit(uint32_t* n);
static void recompute_waveform(void);

void OUT_init(void)
{
  fine_cal = STORE_get_fine_cal();
  medium_cal = STORE_get_medium_cal();

  /* Setup timer 1 in fast PWM mode with TOP=ICR1
     and force output compare */
  TCCR1A = (1<<FOC1A)|(1<<FOC1B)|(1<<WGM11)|(0<<WGM10);
  TCCR1B = (1<<WGM13)|(1<<WGM12);<commit_msg>Default to frequency mode<commit_after>#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "ui.h"
#include "out.h"
#include "store.h"

// The period in clock cycles from frequency in mHz
// is p = F_CPU / (f / 1000) i.e. p = (1000 * F_CPU) / f
// but 1000 * F_CPU may overflow a 32-bit integer
// so find the smallest power of 2 k such that 
// (1000/k) * F_CPU does not overflow
// i.e. (1000/k) * F_CPU < 0xFFFFFFFF
// or (1000/k) < 0xFFFFFFFF/F_CPU
// Then p = (1000/k * F_CPU) / (f/k)
#if 1000 < 0xFFFFFFFF/F_CPU
  #define F_CPU_MUL 1000
  #define F_OUT_DIV 1
#elif 500 < 0xFFFFFFFF/F_CPU 
  #define F_CPU_MUL 500
  #define F_OUT_DIV 2
#elif 250 < 0xFFFFFFFF/F_CPU
  #define F_CPU_MUL 250
  #define F_OUT_DIV 4
#elif 125 < 0xFFFFFFFF/F_CPU 
  #define F_CPU_MUL 125
  #define F_OUT_DIV 8
#else
  #error Cannot compute frequency scaling parameters
#endif

#define PWM_RATIO 64
#define MAX_NON_SQUARE_FREQUENCY_mHz ((uint32_t)(F_CPU / 2.0 * 1000.0 / PWM_RATIO + 0.5))

#define MIN_NON_SQUARE_PERIOD_NS ((uint32_t)(1e12 / MAX_NON_SQUARE_FREQUENCY_mHz + 0.5))

#define SIN__1_OVER_32_PI  0.09802 /* sin(pi *  1/32) */
#define SIN__3_OVER_32_PI  0.29028 /* sin(pi *  3/32) */
#define SIN__5_OVER_32_PI  0.47140 /* sin(pi *  5/32) */
#define SIN__7_OVER_32_PI  0.63439 /* sin(pi *  7/32) */
#define SIN__9_OVER_32_PI  0.77301 /* sin(pi *  9/32) */
#define SIN_11_OVER_32_PI  0.88192 /* sin(pi * 11/32) */
#define SIN_13_OVER_32_PI  0.95694 /* sin(pi * 13/32) */
#define SIN_15_OVER_32_PI  0.99518 /* sin(pi * 15/32) */

static uint8_t on;
static uint8_t freq_mode = OUT_FREQ_MODE;
static uint8_t waveform;
static uint8_t duty_cycle = 50;
static uint8_t amplitude = 100;
static int8_t fine_cal;
static int8_t medium_cal;

static uint32_t period_ns;
static uint32_t freq_mHz = 1000000;

static uint8_t waveform_index;
static uint16_t waveform_data[PWM_RATIO];

static void range_limit(uint32_t* n);
static void recompute_waveform(void);

void OUT_init(void)
{
  fine_cal = STORE_get_fine_cal();
  medium_cal = STORE_get_medium_cal();

  /* Setup timer 1 in fast PWM mode with TOP=ICR1
     and force output compare */
  TCCR1A = (1<<FOC1A)|(1<<FOC1B)|(1<<WGM11)|(0<<WGM10);
  TCCR1B = (1<<WGM13)|(1<<WGM12);
{'fraction_non_alphanumeric': 0.1279567470150935, 'fraction_numerical': 0.10678080648794773, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2411743', 'n_tokens_mistral': 2275, 'n_tokens_neox': 1932, 'n_words': 468}