-florian66-

main.c

May 11th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.61 KB | None | 0 0
  1. /*****************************************************************/
  2. /*                                                               */
  3. /*   CASIO fx-9860G SDK Library                                  */
  4. /*                                                               */
  5. /*   File name : [ProjectName].c                                 */
  6. /*                                                               */
  7. /*   Copyright (c) 2006 CASIO COMPUTER CO., LTD.                 */
  8. /*                                                               */
  9. /*****************************************************************/
  10.  
  11. #include "fxlib.h"
  12. #include "serial.h"
  13. #include "MonochromeLib.h"                                  // FICHIER DEFINISSANT LES FONCTIONS GRAPHIQUES
  14. #include "stdio.h"
  15. #include "stdlib.h"
  16. #include "string.h"
  17. #include "SH4comp.c"
  18. #include "test.h"
  19. #include "memory.h"
  20. #include "img.h"
  21. #include "EasyInput.h"
  22.  
  23. //****************************************************************************
  24. //  AddIn_main (Sample program main function)
  25. //
  26. //  param   :   isAppli   : 1 = This application is launched by MAIN MENU.
  27. //                        : 0 = This application is launched by a strip in eACT application.
  28. //
  29. //              OptionNum : Strip number (0~3)
  30. //                         (This parameter is only used when isAppli parameter is 0.)
  31. //
  32. //  retval  :   1 = No error / 0 = Error
  33. //
  34. //****************************************************************************
  35.  
  36.  // une fonction qui affiche la fequence=>son (faite) et autre qui fait note =>frequence (faite)
  37.  // *(array+octave*12+demi_ton)
  38.  
  39.  
  40. //#define NB_DEMI_TON 12
  41. #define r 1.059463094
  42.  
  43. #define octave(x) ((int)x/12)
  44. #define place_note(note)  ((note%12)== 0 ||  (note%12)== 2 || (note%12)== 4 || (note%12)== 5 || (note%12)== 7 || (note%12)== 9 || (note%12)== 11 ? note : note-1)
  45.  
  46. /*static int SysCallCode[] = {0xD201422B,0x60F20000,0x80010070};
  47. static int (*SysCall)(int R4, int R5, int R6, int R7, int FNo ) = (void*)&SysCallCode;
  48.  
  49. int RTC_getTicks()
  50. {
  51.      return (*SysCall)(0, 0, 0, 0, 0x3B);
  52. }*/
  53.  
  54. char *notes[12]=
  55. {
  56.         "do",
  57.         "do#",
  58.         "re",
  59.         "re#",
  60.         "mi",
  61.         "fa",
  62.         "fa#",
  63.         "sol",
  64.         "sol#",
  65.         "la",
  66.         "la#",
  67.         "si"
  68. };
  69.  
  70.  
  71.  
  72. //short note=9; //correspond au la 3
  73. //short octave=3;
  74. double tempo=160.0;
  75.  
  76. double length_rythme[]={
  77. 0.25, //double
  78. 0.5, // croche
  79. 0.75, //croche pointee
  80. 1, //noire
  81. 1.5, // noire pointee
  82. 2, //blanche
  83. 3, //blanche pointee
  84. 4 // ronde
  85. };
  86.  
  87.  
  88. void draw_part(unsigned char* notes, int decalx)
  89. {
  90. int i=0,j;
  91. unsigned int key;
  92. char buffer[50];
  93.     Bdisp_AllClr_DDVRAM();
  94.    
  95.     //for(j=0;j<5;j++)
  96.     //ML_horizontal_line(17+6*j, 0, 128, ML_BLACK); // on trace les lignes de la partition
  97.    
  98.     while(*(notes+i))
  99.     {
  100.     if (*(notes+i)>1)
  101.     ML_bmp_or_cl(*(rythme+(int)*(notes+i+1)-1), 4.5*i-decalx, 32-2*(place_note(*(notes+i))-49)-21, 8, 16);
  102.     else
  103.     ML_bmp_or_cl(*(silence+(int)*(notes+i+1)-1), 4.5*i-decalx, 21, 8, 16);
  104.     i+=2;
  105.     }
  106.    
  107.     for(j=0;j<5;j++)
  108.     ML_horizontal_line(17+6*j, 0, 128, ML_BLACK); // on trace les lignes de la partition
  109.     ML_display_vram();
  110.     return;
  111. }
  112.  
  113. void edit_part(unsigned char *notes_lect)
  114. {
  115. int i=0,j; // decalage des notes
  116. unsigned int key;
  117. unsigned char  note=45;
  118. unsigned char  ryth=3;
  119. char buffer[50];
  120. unsigned char  place_note;
  121.  
  122. while(*(notes_lect+i+2)) i+=2; // si on réedite on va à la fin de la partoche
  123.  
  124.  
  125. while(1)
  126.     {
  127.    
  128.     draw_part(notes_lect,  4.5*i-45);
  129.  
  130.     if (note>1)
  131.     ML_bmp_or_cl(*(rythme+ryth), 50, 32-2*(place_note(note)-49)-21, 8, 16);
  132.     else
  133.     ML_bmp_or_cl(*(silence+ryth), 50, 21, 8, 16);
  134.    
  135.    
  136.     //ML_bmp_or_cl(select, 49, 32-2*(note-49)-22, 11, 19);
  137.     Mini("swit", 0);
  138.     Mini("see", 5);
  139.     sprintf(buffer,"note : %s",*(notes+note%12));
  140.     PrintMini(80,15,buffer,0);
  141.    
  142.     //sprintf(buffer,"ryth : %d",ryth);
  143.    // PrintMini(80,25,buffer,0);
  144.    
  145.     sprintf(buffer,"tempo : %.0f",tempo);
  146.     PrintMini(80,25,buffer,0);
  147.  
  148.     GetKey(&key);
  149.     switch(key)
  150.         {
  151.         case KEY_CTRL_UP : note++; break;
  152.         case KEY_CTRL_DOWN : note--; break;
  153.         case KEY_CTRL_LEFT : ryth=(ryth <=0 ? 7 : ryth-1); break;
  154.         case KEY_CTRL_RIGHT : ryth=(ryth >=7 ? 0 : ryth+1); break;
  155.         case KEY_CHAR_PLUS : tempo++; break;
  156.         case KEY_CHAR_MINUS : tempo--; break;
  157.         case KEY_CTRL_EXE : *(notes_lect+i)=note; *(notes_lect+1+i)=ryth+1; i+=2; break;
  158.         case KEY_CTRL_F1 : note=(note >1 ? note = 1 : 45); break;
  159.         case KEY_CTRL_F2 : case KEY_CTRL_DEL : *(notes_lect+i)=0; i-=2; break;
  160.         case KEY_CTRL_F6 : return; // play
  161.         }
  162.     }
  163. }
  164.  
  165. unsigned char* user_select(unsigned char* ch)
  166. {
  167.   unsigned int key;
  168.  
  169.   int i,j,k,t;
  170.   int p=0, offset=0;
  171.   char x;
  172.   char **files = memory_alloc(20);
  173.   char *name_file=NULL;
  174.  
  175.  while(1)
  176.     {
  177.     x = memory_find("\\\\fls0\\*.cf",files,20);
  178.  
  179.    
  180.     Bdisp_AllClr_VRAM();
  181.  
  182.     for(t=0;t<(x>5?5:x);t++)
  183.         {
  184.         PrintMini(1,1,(const unsigned char *)"your files : ",0);  
  185.         PrintMini(2,10*t+9,(const unsigned char *)*(files+offset+t),0);
  186.         }
  187.        
  188.     if(x>0) Bdisp_AreaReverseVRAM(1,10*p+7,100,10*p+15);
  189.     else PrintMini(36,29,(const unsigned char *)"no files found",0);
  190.        
  191.     Mini("new", 0);
  192.     Mini("del", 1);
  193.     Mini("exit", 5);
  194.    
  195.  
  196.        
  197.     GetKey(&key);
  198.  
  199.     switch(key)
  200.         {
  201.         if(x>0)
  202.             {
  203.            
  204.             case KEY_CTRL_EXE :
  205.                 sprintf(ch,"\\\\fls0\\%s",*(files+(offset+p)));
  206.                 return ch;
  207.                
  208.                
  209.             case KEY_CTRL_UP :
  210.                 if(offset || p)
  211.                     {
  212.                     if(p==4) p--;
  213.                         else if(offset)  
  214.                         offset--;
  215.                         else p--;
  216.                         }
  217.                     break;
  218.                    
  219.                    
  220.             case KEY_CTRL_DOWN :
  221.                     if(offset+p+1<x)
  222.                         {
  223.                         if(p==0) p++;
  224.                         else if(offset+5<x) offset++;
  225.                         else p++;
  226.                         }
  227.                     break;
  228.                    
  229.             }
  230.            
  231.             case KEY_CTRL_F1 :
  232.                 Bdisp_AllClr_DDVRAM();
  233.                 PrintXY(2,1,"File name :",0);
  234.                
  235.                 EI_init();
  236.                 EI_manage_config(EI_SET_COLUMN, 1);
  237.                 EI_manage_config(EI_SET_ROW, 3);
  238.                 EI_manage_config(EI_SET_START_MODE, EI_ALPHA_LOCKED);
  239.                
  240.                 name_file=EI_input_string(8,(const char*)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  241.                 sprintf(ch,"\\\\fls0\\%s.cf",name_file);
  242.                 memory_createfile(ch,20);
  243.                 break;
  244.  
  245.             case KEY_CTRL_F2 :
  246.                 sprintf(ch,"\\\\fls0\\%s",*(files+(offset+p)));
  247.                 memory_deletefile(ch);
  248.                 break;
  249.                
  250.         //  case KEY_CTRL_F
  251.                    
  252.             case KEY_CTRL_F6 :  
  253.                 memory_free(files,20);
  254.                 return NULL;   
  255.         }
  256.     }
  257. }
  258.  
  259.  
  260. int AddIn_main(int isAppli, unsigned short OptionNum)
  261. {
  262.        // double length=1.0; // a 60 batt/minute
  263.         //double freq=440;
  264.         int i=0;
  265.         unsigned short taille_file;
  266.         unsigned int key;
  267.         char x;
  268.         char buffer[50];
  269.         unsigned char* ID_note; // On crée un pointeur sur int
  270.         unsigned char* ch;
  271.  
  272.    
  273.    
  274. //sprintf(ch,"\\\\crd0\\%s",files[memory_user_select(files,memory_find("\\\\crd0\\*sav*",files,20),1,0)]);
  275.  
  276.  
  277. ch= user_select(ch);
  278.  
  279.  
  280. /* while(*(data+taille_file)) taille_file++;
  281.  
  282. for(i=0;i<taille_file;i++)
  283.     *(ID_note+i)=*(data+i); */
  284. ID_note=memory_load(ch);
  285.  
  286. if(ID_note==NULL) ID_note=calloc(100,sizeof(unsigned char));
  287.  
  288. edit_part(ID_note);
  289.  
  290.  
  291.  
  292. while(1)
  293. {
  294.     draw_part(ID_note, 0);
  295.    
  296.     for(i=0;ID_note[i];i+=2);
  297.     memory_save(ch,ID_note,i);
  298.        
  299.    
  300.  
  301.     Mini("edit", 0);
  302.     Mini("save", 1);
  303.     Mini("clear", 2);
  304.     Mini("play", 5);
  305.    
  306.        
  307.     GetKey(&key);
  308.     switch(key)
  309.     {
  310.     case KEY_CTRL_EXIT : free(ID_note); return 1;
  311.     case KEY_CTRL_F1 : edit_part(ID_note); break;
  312.     case KEY_CTRL_F2 :
  313.         {
  314.        
  315.         break;
  316.         }
  317.     case KEY_CTRL_F3 :
  318.         {
  319.         for(i=0;*(ID_note+i);i++)
  320.             {
  321.             *(ID_note+i)=0;
  322.             }
  323.             edit_part(ID_note);
  324.             break;
  325.         }
  326.     case KEY_CTRL_F6 :jouer_partition(ID_note); break;
  327.     }
  328.  
  329. }
  330. return 1;      
  331. }
  332.  
  333. void jouer_partition(unsigned char* ID_note)
  334. {  
  335. int i=0;
  336.  
  337. //for(i=0;*(ID_note+i)<21;i+=2);
  338.  
  339. while(*(ID_note+i))
  340.     {
  341.     //freq=440*power(r,*(ID_note+i)-45);
  342.     draw_part(ID_note, 4.5*i-40);
  343.    
  344.     if(*(ID_note+i)>1) // on verifie que ce n'est pas un silence
  345.     play_freq(440*power(r,*(ID_note+i)-45), (60/tempo**(length_rythme+*(ID_note+1+i))));
  346.    
  347.     else play_silence(60/tempo**(length_rythme+*(ID_note+1+i)));
  348.    
  349.     i+=2;
  350.     }
  351. }
  352.    
  353.  
  354. float power(float x, int y)
  355. {
  356.     float temp;
  357.     if( y == 0)
  358.        return 1;
  359.     temp = power(x, y/2);      
  360.     if (y%2 == 0)
  361.         return temp*temp;
  362.     else
  363.     {
  364.         if(y > 0)
  365.             return x*temp*temp;
  366.         else
  367.             return (temp*temp)/x;
  368.     }
  369. }  
  370.  
  371. void Mini(char* chaine, int n ) // fonc defini le numero de la fonction
  372. {
  373.  
  374.     int i;
  375.     short j;
  376.  
  377.     for(i=56 ; i<64 ; i++)
  378.         Bdisp_DrawLineVRAM(n*21+2, i, n*21+20, i);
  379.  
  380.     while(chaine[j]) j++;  // on calcul la longeur de la chaine
  381.  
  382.     if(j<4) i=4;
  383.     else i=3;
  384.     PrintMini(n*21+i, 58, chaine, 18);
  385.     //ML_display_vram();
  386. }
  387.  
  388. void play_silence(double length)
  389. {
  390. int i;
  391. double j;
  392. for(i=0;i<(int)(length*496);i++)
  393.     {
  394.     Sleep(2);
  395.     }
  396.     return;
  397. }
  398.  
  399. void play_freq(double frequence , double length)
  400. {
  401. int i;
  402. double j;
  403. unsigned char config[] = {0, 5, 0, 0, 0, 0};
  404.  
  405. Serial_Open(config);
  406.  
  407. for(i=0;i<(int)(frequence*length*0.653);i++)
  408.         {
  409.         Serial_WriteByte(255);
  410.         for (j=0;j<17.5;j+=0.211892619*frequence/440); // 0.211892619 => 2*((12)/square(2))
  411.         }
  412.        
  413. Serial_Close(1);
  414. return;
  415.  
  416. }
  417.  
  418. void lecteur_son(int *ID, int *length)
  419. {
  420. short i=0;
  421. //double freq;
  422.  
  423. while(*(ID+i))
  424.     {
  425.  
  426.     if(*(ID+i)>1)
  427.     play_freq(440*power(r,*(ID+i)-45), (60/tempo**(length_rythme+*(length+i))));
  428.    
  429.     else play_silence(60/tempo**(length_rythme+*(length+i)));
  430.    
  431.     i++;
  432.     }
  433.     return;
  434. }
  435.  
  436. /*double calcul_frequence()
  437. {
  438. double frequence;
  439. //short nb_de_demi_ton=0;
  440. char buffer[50];
  441. unsigned int key;
  442.  
  443. while(1)
  444. {
  445.      Bdisp_AllClr_DDVRAM();
  446.        
  447.     sprintf(buffer,"note : %s",notes[note]);
  448.     PrintMini(10,30,buffer,0);
  449.        
  450.     sprintf(buffer,"%d",octave);
  451.     PrintMini(37+5*strlen(notes[note]),32,buffer,0);
  452.        
  453.     sprintf(buffer,"freq : %.2f",440*power(r,12*octave+note-45));
  454.     PrintMini(10,20,buffer,0);
  455.    
  456.     sprintf(buffer,"tempo : %.0f",tempo);
  457.     PrintMini(10,40,buffer,0);
  458.  
  459.     GetKey(&key);
  460.  
  461.                
  462.  
  463.      switch(key)
  464.         {
  465.         case KEY_CTRL_UP :
  466.             {
  467.             if (note<11)
  468.                 {
  469.                 note++;
  470.                 }
  471.             else
  472.                 {
  473.                 note=0;
  474.                 octave=(octave<7 ? octave+1 : 0);
  475.                 }
  476.             break;
  477.             }
  478.         case KEY_CTRL_DOWN :
  479.             {
  480.             if(note>0)
  481.                 {
  482.                 note--;
  483.                 }
  484.             else
  485.                 {
  486.                 note=11;
  487.                 octave=(octave>0 ? octave-1 : 7);
  488.                 }
  489.             break;
  490.             }
  491.  
  492.         case KEY_CHAR_PLUS : tempo+=10; break;
  493.         case KEY_CHAR_MINUS : tempo-=10; break;
  494.        
  495.        
  496.         case KEY_CTRL_EXE : return 440*power(r,12*octave+note-45);
  497.         }
  498.     }
  499. }*/
  500.  
  501. //****************************************************************************
  502. //**************                                              ****************
  503. //**************                 Notice!                      ****************
  504. //**************                                              ****************
  505. //**************  Please do not change the following source.  ****************
  506. //**************                                              ****************
  507. //****************************************************************************
  508.  
  509.  
  510. #pragma section _BR_Size
  511. unsigned long BR_Size;
  512. #pragma section
  513.  
  514.  
  515. #pragma section _TOP
  516.  
  517. //****************************************************************************
  518. //  InitializeSystem
  519. //
  520. //  param   :   isAppli   : 1 = Application / 0 = eActivity
  521. //              OptionNum : Option Number (only eActivity)
  522. //
  523. //  retval  :   1 = No error / 0 = Error
  524. //
  525. //****************************************************************************
  526. int InitializeSystem(int isAppli, unsigned short OptionNum)
  527. {
  528.     return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  529. }
  530.  
  531. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment