いろいろなテクニック.Nucleo と DISCO-F746 用.

Dependencies:   Array_Matrix mbed

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Wed Apr 10 06:32:33 2019 +0000
Parent:
0:bb939e0bc6e2
Commit message:
2

Changed in this revision

ArrayExampleInClass.hpp Show annotated file Show diff for this revision Revisions of this file
FunctionObject.hpp Show annotated file Show diff for this revision Revisions of this file
FunctionPointer.hpp Show annotated file Show diff for this revision Revisions of this file
InitializeStruct.hpp Show annotated file Show diff for this revision Revisions of this file
MyNew.hpp Show annotated file Show diff for this revision Revisions of this file
MySort.hpp Show annotated file Show diff for this revision Revisions of this file
PeripheralAddress.hpp Show annotated file Show diff for this revision Revisions of this file
PolyMorphism.hpp Show annotated file Show diff for this revision Revisions of this file
StructCopy.hpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ArrayExampleInClass.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -0,0 +1,36 @@
+//--------------------------------------------
+//  クラス中の Array クラスのオブジェクトの初期化
+//--------------------------------------------
+
+#include "Array.hpp"
+using namespace Mikami;
+
+class IncludeArray
+{
+public:
+    IncludeArray(int size)
+        : x1_(size, -1),    // サイズと配列の内容を初期化
+          x2_(size) {}      // サイズのみ初期化,この場合配列の初期状態の内容が         
+                            // どうなっているかは不定
+
+    void PrintOutX1() { PrintOut(x1_); }
+    void PrintOutX2() { PrintOut(x2_); }
+private:
+    Array<float> x1_, x2_;
+    void PrintOut(Array<float> x)
+    {
+        for (int n=0; n<x.Length(); n++)
+            printf("    %f\r\n", x[n]);
+    }
+};
+ 
+void ArrayInClass()
+{
+    printf("\r\nクラス中の Array クラスのオブジェクトの初期化\r\n");
+
+    IncludeArray myArray(4);
+    printf("x1:\r\n");
+    myArray.PrintOutX1();
+    printf("x2\r\n");
+    myArray.PrintOutX2();
+}
--- a/FunctionObject.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/FunctionObject.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -34,10 +34,10 @@
 void MyFunctionObject()
 {
     DerivFunc fx1(aFunc1);
-    float area = Integrate(fx1, 0.0, 1.0);
+    float area = Integrate(fx1, 0.0, 1.0);  // y = x の 区間 [0, 1] の定積分
     printf("\r\nArea = %f\r\n", area);
 
     DerivFunc fx2(aFunc2);
-    area = Integrate(fx2, 0.0, 1.0);
+    area = Integrate(fx2, 0.0, 1.0);        // y = x*x の 区間 [0, 1] の定積分
     printf("Area = %f\r\n", area);
 }
--- a/FunctionPointer.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/FunctionPointer.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -23,7 +23,7 @@
 
 void SwPointer(int k)
 {
-    printf("k = %d\r\n", k);
+    printf("\r\nk = %d\r\n", k);
 
     if (k==0) fp1 = printA;
     else      fp1 = printB;
--- a/InitializeStruct.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/InitializeStruct.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -11,8 +11,9 @@
 //    x4 = {-7, -8};    // エラー,代入なので使えない
     x4 = (myStruct){-7, -8};    // 複合リテラル(compound literal)を利用 
     
-    printf("\r\nx1.a1 = %d, x1.a2 = %d\r\n", x1.a1, x1.a2);
-    printf("\r\nx2.a1 = %d, x2.a2 = %d\r\n", x2.a1, x2.a2);
-    printf("\r\nx3.a1 = %d, x3.a2 = %d\r\n", x3.a1, x3.a2);
-    printf("\r\nx4.a1 = %d, x4.a2 = %d\r\n", x4.a1, x4.a2);
+    printf("\r\n");
+    printf("x1.a1 = %2d,  x1.a2 = %2d\r\n", x1.a1, x1.a2);
+    printf("x2.a1 = %2d,  x2.a2 = %2d\r\n", x2.a1, x2.a2);
+    printf("x3.a1 = %2d,  x3.a2 = %2d\r\n", x3.a1, x3.a2);
+    printf("x4.a1 = %2d,  x4.a2 = %2d\r\n", x4.a1, x4.a2);
 }
--- a/MyNew.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/MyNew.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -2,7 +2,7 @@
 //  See "https://developer.mbed.org/users/okini3939/notebook/mbed256_memory/"
 //
 //  このプログラムは,STM 社のマイコンを使用し,mbed ライブラリが
-//  リビジョン 153 の場合に有効なことを確認している.それ以外は未確認
+//  リビジョン 153, 172 の場合に有効なことを確認している.それ以外は未確認
 //
 //  new 演算子でメモリ割り当てに失敗した場合はターミナルに
 //  "Operator new[] out of memory" というメッセージが表示される
@@ -12,21 +12,19 @@
 //
 //  set_new_handler() は働いていないようである
 //
-//  2017/10/15, Copyright (c) 2017 MIKAMI, Naoki
+//  2019/04/10, Copyright (c) 2019 MIKAMI, Naoki
 //------------------------------------------------------------------------------
 //#include <new>    // set_new_handler() を使う場合でも不要
 
-/*
 void MemoryAssignError()
 {
     printf("Can't allocate to memory!\r\n");
     while(true);
 }
-*/
 
 void MyNew()
 {
-//    set_new_handler(MemoryAssignError);
+    set_new_handler(MemoryAssignError);
     
     uint8_t *ptr;
 //    ptr = new uint8_t[100000];      // OK
--- a/MySort.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/MySort.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -13,7 +13,8 @@
     for (int n=0; n<N; n++)
         printf("    %s\r\n", data0[n].c_str());
 
-    string *data1 = new string[N];
+//    string *data1 = new string[N];
+    string data1[N];    // これでもよい
     for (int n=0; n<N; n++) data1[n] = data0[n];
     sort(data1, data1+N); // 配列
     printf("ソート後:\r\n");
@@ -21,7 +22,7 @@
         printf("    %s\r\n", data1[n].c_str());
 
     printf("\"MySorth\" の終了\r\n");
-    delete [] data1;
+//    delete [] data1;  // data1 に new で確保した領域を割り当てない場合は,これは不要
 }
 
 // array, vector を昇順にソートする
--- a/PeripheralAddress.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/PeripheralAddress.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -2,9 +2,10 @@
 //  mbed で定義されているピンの名前に対応するペリフェラルのアドレスを取得
 //      SPI, TIM, GPIO, ADC の場合
 //
-//  修正:mbed rev.153 用に修正 2017/10/14
+//  2017/10/14 修正:mbed rev.153 用に修正
+//  2019/04/10 Mbed rev.172 でも問題ないことを確認
 //
-//  2016/12/06, Copyright (c) 2016 MIKAMI, Naoki
+//  2019/04/10, Copyright (c) 2019 MIKAMI, Naoki
 //-----------------------------------------------------------------
 //      デバイスは PeripheralPins.h の中で以下のように定義されている
 /*
@@ -91,7 +92,15 @@
 {
 #if defined(STM32F4)
     AddressSPI x1(D4, NC);
-    printf("SPI,  D4:    0x%08X\r\n", x1.GetAddress());
+    printf("SPI1/3, D4:    0x%08X\r\n", x1.GetAddress());
+    printf("上の表示されるアドレスは SPI1 のもの\r\n");
+    printf("SPI,    D4:    0x%08X\r\n", pinmap_peripheral(D4, PinMap_SPI_MOSI));
+
+    // 次の printf() 文のように機能がピン名と異なる場合(D4 は MISO ではない),実行時エラーが
+    // ターミナルに表示され,プログラムが終了する
+    //printf("SPI3,  D4:    0x%08X\r\n", pinmap_peripheral(D4, PinMap_SPI_MISO));
+    SPI mySpi(D4, NC, D3);
+    printf("    GPIOB->AFR[0]: 0x%08X\r\n", GPIOB->AFR[0]);
 #endif  // STM32F4
 
 //#ifdef __STM32F746xx_H
@@ -104,15 +113,15 @@
 #endif  // STM32F7
     
     AddressTIM x2(D12);
-    printf("PWM,  D12:   0x%08X\r\n", x2.GetAddress());
-    printf("PWM,  D8:    0x%08X\r\n", pinmap_peripheral(D8, PinMap_PWM));
+    printf("PWM3,   D12:   0x%08X\r\n", x2.GetAddress());
+    printf("PWM1,   D8:    0x%08X\r\n", pinmap_peripheral(D8, PinMap_PWM));
 
     AddressDigitalOut x3(D6);
-    printf("GPIO, D6:    0x%08X\r\n", x3.GetAddress());
+    printf("GPIOB,  D6:    0x%08X\r\n", x3.GetAddress());
 
     PinName adc = A0;
     ADCName addresAdc = (ADCName)pinmap_peripheral(adc, PinMap_ADC);
-    printf("ADC,  A0:    0x%08X\r\n", addresAdc);
-    printf("ADC,  A1:    0x%08X\r\n", pinmap_peripheral(A1, PinMap_ADC));
-    printf("ADC,  A2:    0x%08X\r\n", pinmap_peripheral(A2, PinMap_ADC));
+    printf("ADC,   A0:    0x%08X\r\n", addresAdc);
+    printf("ADC,   A1:    0x%08X\r\n", pinmap_peripheral(A1, PinMap_ADC));
+    printf("ADC,   A2:    0x%08X\r\n", pinmap_peripheral(A2, PinMap_ADC));
 }
--- a/PolyMorphism.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/PolyMorphism.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -10,21 +10,21 @@
 {
 public:
     Base() {}
-    virtual void Execute() { printf("Base class\r\n"); }
+    virtual void Execute() { printf("    Base class\r\n"); }
 };
 
 class Derived1 : public Base
 {
 public:
     Derived1() {}
-    virtual void Execute() { printf("Derived1 class\r\n"); }
+    virtual void Execute() { printf("    Derived1 class\r\n"); }
 };
 
 class Derived2 : public Base
 {
 public:
     Derived2() {}
-    virtual void Execute() { printf("Derived2 class\r\n"); }
+    virtual void Execute() { printf("    Derived2 class\r\n"); }
 };
 
 void Print(Base *obj) { obj->Execute(); }
@@ -36,12 +36,12 @@
     Derived2 drv2;
     
     // 例 1
-    printf("\r\n");
+    printf("例 1:\r\n");
     Base *func[3] = { &base, &drv1, &drv2 };
     for (int n=0; n<3; n++) func[n]->Execute();
     
     // 例 2
-    printf("\r\n");
+    printf("例 2:\r\n");
     Base *ptr;
     ptr = &drv2;
     ptr->Execute();
@@ -50,6 +50,6 @@
     ptr->Execute();
     
     // 例 3
-    printf("\r\n");
+    printf("例 3:\r\n");
     Print(&drv2);
 }
\ No newline at end of file
--- a/StructCopy.hpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/StructCopy.hpp	Wed Apr 10 06:32:33 2019 +0000
@@ -16,8 +16,16 @@
     printf("x1: %2d, %d\r\n", x1.a1, x1.a2);    // x1.a1 は書き換わっていないので
                                                 // y1 への代入はシャロ―・コピーではなく
                                                 // ディープ・コピーであることが確認できる
+    
+    // 同じ構造体へのコピー
+    myStructB y2;
+    y2 = y1;
+    printf("\r\ny1: %2d, %d\r\n", y1.a1, y1.a2);
+    y2.a1 = 9;
+    printf("y1: %2d, %d\r\n", y1.a1, y1.a2);
+    printf("y2: %2d, %d\r\n", y2.a1, y2.a2);
+
     printf("\r\n");
-    
     myStructA u1[] = { {3, 6}, {7, 2}, {6, 1}};
     myStructB *u2;
     u2 = (myStructB *)(&u1);
--- a/main.cpp	Sun Oct 15 11:41:48 2017 +0000
+++ b/main.cpp	Wed Apr 10 06:32:33 2019 +0000
@@ -2,7 +2,9 @@
 //  使用例:Nucleo と DISCO-746 が対象
 //
 //  mbed 公式ライブラリリビジョン 153 で OK
-//  2017/10/15, Copyright (c) 2017 MIKAMI, Naoki
+//  mbed 公式ライブラリリビジョン 172 で OK
+//
+//  2019/04/10, Copyright (c) 2019 MIKAMI, Naoki
 //------------------------------------------------------------
 
 #include "mbed.h"
@@ -11,21 +13,22 @@
 //------------------------------------------------------------------------------
 //  以下で,行頭の「//」を削除して有効にした define 文 に対応する処理が実行される
 //
-//#define MY_MATH                 // 1. 算術関数の例
-//#define MY_SORT                 // 2. STL のソートの例
-//#define DYNAMIC_POLYMORPHISM    // 3. 実行時ポリモーフィズム
-//#define STL_VECTOR              // 4. STL の vecotr の例
-//#define MY_NEW                  // 5. new 演算子のメモリ割り当てエラーを検出の例
-//#define FUNCTION_OBJECT         // 6. function object の例
-//#define FUNCTION_POINTER        // 7. 関数ポインタの例
+//#define MY_MATH                     // 1. 算術関数の例
+//#define MY_SORT                     // 2. STL のソートの例
+//#define DYNAMIC_POLYMORPHISM        // 3. 実行時ポリモーフィズム
+//#define STL_VECTOR                  // 4. STL の vecotr の例
+//#define MY_NEW                      // 5. new 演算子のメモリ割り当てエラーを検出の例
+//#define FUNCTION_OBJECT             // 6. function object の例
+//#define FUNCTION_POINTER            // 7. 関数ポインタの例
 //#define FUNCTION_POINTER_IN_CLASS   // 8. クラス内で関数ポインタを使う例
-//#define PASS_ARRAY_CONSTRUCTOR  // 9. コンストラクタに渡した Array<> を使う例
-//#define REFERENCE_ARRAY         // 10. 異なる型の Array クラスの参照の代入の例
-//#define GET_PERIPHERAL_ADDRESS  // 11. ピンの名前に対応するペリフェラルのアドレスを取得
-//#define UNION_EXAMPLE           // 12. union の使用例
-//#define INITIALIZE_STRUCT       // 13. 構造体の初期化
-//#define STRUCT_COPY             // 14. 同じ構造で異なる構造体のコピー
-#define COMPOOUND_LITERAL       // 15. 複合リテラルの使用例
+//#define PASS_ARRAY_CONSTRUCTOR      // 9. コンストラクタに渡した Array<> を使う例
+//#define REFERENCE_ARRAY             // 10. 異なる型の Array クラスの参照の代入の例
+//#define GET_PERIPHERAL_ADDRESS      // 11. ピンの名前に対応するペリフェラルのアドレスを取得
+//#define UNION_EXAMPLE               // 12. union の使用例
+//#define INITIALIZE_STRUCT           // 13. 構造体の初期化
+//#define STRUCT_COPY                 // 14. 同じ構造で異なる構造体のコピー
+//#define COMPOOUND_LITERAL           // 15. 複合リテラルの使用例
+#define ARRAY_IN_CLASS              // 16. クラス中の Array クラスのオブジェクトの初期化
 //
 //  有効にすべき define 文はここまで
 //------------------------------------------------------------------------------
@@ -45,10 +48,10 @@
 #include "InitializeStruct.hpp"         // 13. 構造体の初期化
 #include "StructCopy.hpp"               // 14. 同じ構造で異なる構造体のコピー
 #include "CompoundLiteral.hpp"          // 15. 複合リテラルの使用例
-
+#include "ArrayExampleInClass.hpp"      // 16. クラス中の Array クラスのオブジェクトの初期化
 int main()
 {
-    printf("\r\n2017/10/15 12:54\r\n");
+    printf("\r\n2019/04/10 10:11\r\n");
 //------------------------------------------------------------
 // 1 : 算術関数の例
 #ifdef MY_MATH
@@ -138,5 +141,12 @@
 #ifdef COMPOOUND_LITERAL
     CompoundLiteral();
 #endif
+
+//------------------------------------------------------------
+// 16. クラス中の Array クラスのオブジェクトの初期化
+#ifdef ARRAY_IN_CLASS
+    ArrayInClass();
+#endif
+
     while (true) {}
 }
--- a/mbed.bld	Sun Oct 15 11:41:48 2017 +0000
+++ b/mbed.bld	Wed Apr 10 06:32:33 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/b484a57bc302
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file