using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LightFadingCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                float basis = float.Parse(textBoxBasis.Text);
                int modulo = int.Parse(textBoxModulo.Text);
                int steps = int.Parse(textBoxSteps.Text);

                textBoxOutput.Clear();
                textBoxOutput.Text += "unsigned int pwmTable[] = \r\n{\r\n\t";
                int charCount = 0;

                for (int i = 0; i < steps; i++)
                {
                    float b = basis;
                    //int value = (int)(modulo * ((Math.Exp(i * b)-1) / (Math.Exp((steps-1) * b) - 1)));

                    int value = (int)(((Math.Pow(basis, i / (float)(steps - 1)) - 1) / (basis - 1)) * modulo);
                    textBoxOutput.Text += value + ", ";

                    charCount += value.ToString().Length + 2;
                    if (charCount > 50)
                    {
                        charCount = 0;
                        textBoxOutput.Text += "\r\n\t";
                    }
                }

                textBoxOutput.Text = textBoxOutput.Text.Trim(',', ' ') + "\r\n};";
            }
            catch (Exception ex)
            {
                textBoxOutput.Text += ex.Message;
            }
        }

        private void textBoxBasis_TextChanged(object sender, EventArgs e)
        {
            buttonCalculate_Click(null, EventArgs.Empty);
        }

        private void textBoxSteps_TextChanged(object sender, EventArgs e)
        {
            buttonCalculate_Click(null, EventArgs.Empty);
        }

        private void textBoxModulo_TextChanged(object sender, EventArgs e)
        {
            buttonCalculate_Click(null, EventArgs.Empty);
        }
    }
}
