/* StepperTable.java creates a text file containg a formated int array The array values correspond to the slope of 90 deg of a sine wave */ import javax.swing.*; import java.io.*; import java.util.*; public class DiffTable{ public static void main(String[] args){ Scanner in = new Scanner(System.in); // console input PrintWriter out = null; // file output stream File file = new File( "./sinescale.txt" ); // file name if( file == null ) System.exit(1); // error else System.out.println(file.getName()); // print file name to console try { out = new PrintWriter(file); } catch( FileNotFoundException e ) { System.err.println( e ); System.exit(1); } int table_size = 64; // default table size String table_name = "sine"; // default array name try { // get user input // System.out.print("Enter table name: "); // table_name = in.next(); // System.out.println(table_name); System.out.print("Enter table size: "); table_size = in.nextInt(); System.out.println(table_size); } catch( InputMismatchException e ) { System.err.println( e ); System.exit(1); } catch( NoSuchElementException e ) { System.err.println( e ); System.exit(1); } // internal array: table[i] = {angle, sin(angle), slope} double table[][] = new double[table_size][3]; // calculate 90 degrees of a sine table for(int i = 0; i < table_size; i++) { table[i][0] = Math.toRadians(i * 90 / (table_size - 1)); // angle in degrees table[i][1] = Math.sin( table[i][0] ); if( i > 0 ) { table[i][2] = (table[i][1] - table[i-1][1]) / (table[i][0] - table[i-1][0]); if( Double.isNaN( table[i][2] ) ) table[i][2] = table[i-1][2]; } else table[i][2] = 1; } //print the table out.print("PROGMEM prog_uint16_t " + table_name + "[] = " + "{"); for( int i = 0; i < table_size; i++) { int x = (int)Math.round( (1 / table[i][2]) ); System.out.println( table[i][0] +"\t"+ table[i][1] +"\t"+ table[i][2] + "\t" + (1 / table[i][2]) +"\t"+ x ); out.print( x ); if( table_size - i > 1 ) out.print(", "); } out.println("};"); out.close(); } // not used // static File fileChooser() // { // JFileChooser fc = new JFileChooser("./init"); // int v = fc.showSaveDialog(null); // if( v == fc.APPROVE_OPTION) // return fc.getSelectedFile(); // else // return null; // } }