/*
 ============================================================================
 Name        : test.c
 Author      : 
 Version     :
 Copyright   : Gnu GPl V3
 Description : sinus generator
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

FILE * pFile;
void writeDAC(int32_t value)
{
	printf("%d \n",value);
	fprintf(pFile,"%d \n",value);
}

#define KONST 1
#define SCALE 0x10000L // scale for fractional number 16bit.16bit
#define AMPLITUDE 1000*SCALE

void loop(){

  int32_t s=AMPLITUDE; // initial excursion ( mass position ) of the spring
  int32_t q=0;
  int32_t w=0;    // delta velocity

  int n;

  for(n=0;n<2000;n++)
  {
    //********** sine wave generation *************
    w=(s*KONST)/SCALE;
    q=q-w;
    s=s+q;
    //*********************************************

    writeDAC(s/SCALE);         // write sin wave to DAC
  }
}

int main(void) {
	puts("minimalistic integer base sine wave algorithmn\n"); /* prints !!!Hello World!!! */
	pFile = fopen ("data.txt","w");
	if (pFile!=NULL)
	{
		fputs ("open data file to write\n",pFile);
		  loop();
		fclose (pFile);
	}else printf("error could not write file\n");
	return EXIT_SUCCESS;
}
